How to get the current view dates in the Flutter Date Range Picker (SfDateRangePicker)?
In Flutter Date Range Picker, you can get the current month start and end dates using the `onViewChanged` callback.
Step 1:
Inside the state, initialize the controller for the date range picker and set the default values for start and end date of the current month.
final DateRangePickerController _controller = DateRangePickerController(); String _startDate = '', _endDate = '';
Step 2:
Place the date picker inside the body of the Scaffold widget and trigger the `onViewChanged` callback of the date picker.
body: Column( children: <Widget>[ Container( height:50, child: Text('StartDate:''$_startDate')), Container(height:50 ,child: Text('EndDate:''$_endDate')), Card( margin: const EdgeInsets.fromLTRB(40, 100, 50, 40), child: SfDateRangePicker( controller: _controller, view: DateRangePickerView.month, onViewChanged: viewChanged, ), ) ], ),
Step 3:
Using the `onViewChanged` callback of the date picker, get the start and dates of the current month.
void viewChanged(DateRangePickerViewChangedArgs args) { _startDate = DateFormat('dd, MMMM yyyy') .format(args.visibleDateRange.startDate!) .toString(); _endDate=DateFormat('dd, MMMM yyyy') .format(args.visibleDateRange.endDate!) .toString(); SchedulerBinding.instance!.addPostFrameCallback((duration) { setState(() {}); }); }
Screenshots:
| |
Conclusion
I hope you enjoyed learning about how to get the current view dates in the Flutter Date Range Picker.
You can refer to our Flutter Date Range Picker feature tour page to learn about its other groundbreaking feature representations. You can also explore our 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!