|
1 | | -# visible-dates-date-range-picker-flutter |
2 | | -How to get the current view dates in flutter date range picker (SfDateRangePicker)? |
| 1 | +# How to get the current view dates in Flutter date range picker (SfDateRangePicker)? |
| 2 | + |
| 3 | +In flutter date range picker, you can get the current month start and end dates using the `onViewChanged` callback. |
| 4 | + |
| 5 | +## Step 1: |
| 6 | +In initState(), initialize the controller for the date range picker and set the default values for start and end dates of the current month. |
| 7 | + |
| 8 | +```xml |
| 9 | +DateRangePickerController _controller; |
| 10 | +String _startDate, _endDate; |
| 11 | + |
| 12 | +@override |
| 13 | +void initState() { |
| 14 | + // TODO: implement initState |
| 15 | + _controller = DateRangePickerController(); |
| 16 | + _startDate = ''; |
| 17 | + _endDate = ''; |
| 18 | + super.initState(); |
| 19 | +} |
| 20 | +``` |
| 21 | + |
| 22 | +## Step 2: |
| 23 | +Place the date picker inside the body of the Scaffold widget and trigger the `onViewChanged` callback of the date picker. |
| 24 | + |
| 25 | +```xml |
| 26 | +body: Column( |
| 27 | + children: <Widget>[ |
| 28 | + Container( height:50, |
| 29 | + child: Text('StartDate:''$_startDate')), |
| 30 | + Container(height:50 |
| 31 | + ,child: Text('EndDate:''$_endDate')), |
| 32 | + Card( |
| 33 | + margin: const EdgeInsets.fromLTRB(40, 100, 50, 40), |
| 34 | + child: SfDateRangePicker( |
| 35 | + controller: _controller, |
| 36 | + view: DateRangePickerView.month, |
| 37 | + onViewChanged: viewChanged, |
| 38 | + ), |
| 39 | + ) |
| 40 | + ], |
| 41 | +), |
| 42 | +``` |
| 43 | + |
| 44 | +## Step 3: |
| 45 | +Using the `onViewChanged` callback of the date picker get the start and dates of the current month. |
| 46 | + |
| 47 | +```xml |
| 48 | +void viewChanged(DateRangePickerViewChangedArgs args) { |
| 49 | + |
| 50 | + _startDate = DateFormat('dd, MMMM yyyy') |
| 51 | + .format(args.visibleDateRange.startDate) |
| 52 | + .toString(); |
| 53 | + _endDate=DateFormat('dd, MMMM yyyy') |
| 54 | + .format(args.visibleDateRange.endDate) |
| 55 | + .toString(); |
| 56 | + SchedulerBinding.instance.addPostFrameCallback((duration) { |
| 57 | + setState(() {}); |
| 58 | + }); |
| 59 | +} |
| 60 | +``` |
0 commit comments