|
1 | | -# dialog-date-range-picker-flutter |
2 | | -How to add a date range picker (SfDateRangePicker) in the Flutter dialog window? |
| 1 | +# How to add a date range picker (SfDateRangePicker) in the Flutter dialog window |
| 2 | + |
| 3 | +In an application, the date range picker can be displayed in a dialog window by using the `onPressed` event of the button. |
| 4 | + |
| 5 | +## Step 1: |
| 6 | +To host a date range picker in a pop-up, you can use the 'AlertDialog' window to achieve this add the date range picker inside the alert dialog and open the dialog on the 'onpressed' event of a button. Here, a flat button is used. |
| 7 | + |
| 8 | +```xml |
| 9 | +body: Column( |
| 10 | + mainAxisAlignment: MainAxisAlignment.center, |
| 11 | + crossAxisAlignment: CrossAxisAlignment.stretch, |
| 12 | + children: <Widget>[ |
| 13 | + FlatButton( |
| 14 | + child: Container( |
| 15 | + child: _selectedDate ==null |
| 16 | + ? Text('Select a date'):Text(_selectedDate), |
| 17 | + ), |
| 18 | + onPressed: () { |
| 19 | + showDialog( |
| 20 | + context: context, |
| 21 | + builder: (BuildContext context) { |
| 22 | + return AlertDialog( |
| 23 | + title: Text(''), |
| 24 | + content: Container( |
| 25 | + height: 350, |
| 26 | + child: Column( |
| 27 | + children: <Widget>[ |
| 28 | + getDateRangePicker(), |
| 29 | + FlatButton( |
| 30 | + child: Text("OK"), |
| 31 | + onPressed: () { |
| 32 | + Navigator.pop(context); |
| 33 | + }, |
| 34 | + ) |
| 35 | + ], |
| 36 | + ), |
| 37 | + )); |
| 38 | + }); |
| 39 | + }, |
| 40 | + ), |
| 41 | + ], |
| 42 | +)); |
| 43 | + |
| 44 | +Widget getDateRangePicker() { |
| 45 | + return Container( |
| 46 | + height: 250, |
| 47 | + child: Card( |
| 48 | + child: SfDateRangePicker( |
| 49 | + view: DateRangePickerView.month, |
| 50 | + selectionMode: DateRangePickerSelectionMode.single, |
| 51 | + onSelectionChanged: selectionChanged, |
| 52 | + ))); |
| 53 | +} |
| 54 | +``` |
| 55 | + |
| 56 | + |
| 57 | +## Step 2: |
| 58 | +Using the `onSelectionChanged` event, you can show the selected date of the picker. |
| 59 | + |
| 60 | +```xml |
| 61 | +void selectionChanged(DateRangePickerSelectionChangedArgs args) { |
| 62 | + _selectedDate = DateFormat('dd MMMM, yyyy').format(args.value); |
| 63 | + |
| 64 | + SchedulerBinding.instance.addPostFrameCallback((duration) { |
| 65 | + setState(() {}); |
| 66 | + }); |
| 67 | +} |
| 68 | +``` |
0 commit comments