Range selection using multiple view picker in Flutter Date Range Picker
In the Flutter Date Range Picker, you can select the dates ranges from different pickers.
Step 1:
In initState(), set the default values for the date range picker.
@override void initState() { _dateRangePickerController = DateRangePickerController(); _dateRangePickerController2 = DateRangePickerController(); rangeStart = ''; rangeEnd = ''; super.initState(); }
Step 2:
Using the `onPressed` callback of the buttons, show the `AlertDialog` window and place the pickers inside the alert window. Set the `initialDisplayDate` for second picker.
child: FlatButton( padding: const EdgeInsets.only( left: 20, top: 10, right: 20, bottom: 10), child: (rangeStart.isEmpty) ? const Text('Start Range') : Text( rangeStart, ), onPressed: () { showDialog( context: context, builder: (BuildContext context) { return AlertDialog( content: Container( height: 300, child: Column(children: <Widget>[ Row( mainAxisAlignment: MainAxisAlignment.spaceEvenly, children: <Widget>[ getDateRangePicker( _dateRangePickerController, null), getDateRangePicker( _dateRangePickerController2, DateTime(date.year, date.month + 1, date.day)), ], ), ButtonBarTheme( child: ButtonBar( children: <Widget>[ FlatButton( child: Text( 'Cancel', textAlign: TextAlign.right, ), onPressed: () { Navigator.pop(context, null); }, ), FlatButton( child: Text( 'Ok', textAlign: TextAlign.right, ), onPressed: () { Navigator.pop(context); setState(() {}); }, ), ], )), ]))); }); }, )),
Step 3:
Implement the Flutter calendar `onSelectionChanged` callback to get the selected ranges of the picker and using the `onPressed` event of the button, update selected ranges to button texts using the `DateRangePickerController` selectedRange property.
void selectionChanged(DateRangePickerSelectionChangedArgs args) { if (args.value is PickerDateRange) { rangeStart = DateFormat('dd MMMM, yyyy').format(args.value.startDate); rangeEnd = args.value.endDate == null ? rangeStart : DateFormat('dd MMMM, yyyy').format(args.value.endDate); _dateRangePickerController2.selectedRange = PickerDateRange(args.value.startDate, args.value.endDate); _dateRangePickerController.selectedRange = PickerDateRange(args.value.startDate, args.value.endDate); } SchedulerBinding.instance.addPostFrameCallback((duration) { }); }
Screenshots:
Conclusion
I hope you enjoyed learning about range selection using multiple view picker in Flutter Date Range Picker.
You can refer to our Flutter Date Range Picker feature tour page to know about its other groundbreaking feature representations documentation to understand how to create and manipulate data.
For current customers, you can check out our components from the License and Downloads page. If you are new to Syncfusion®, you can try our 30-day free trial to check out our other controls.
If you have any queries or require clarifications, please let us know in the comments section below. You can also contact us through our support forums, Direct-Trac, or feedback portal. We are always happy to assist you!