Articles in this section
Category / Section

How to get the current view dates in the Flutter Date Range Picker (SfDateRangePicker)?

1 min read

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(() {});
    });
}

 

View sample in Github.

Screenshots:

monthview

 

yearview

decade

centuryview

 

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 know about its other groundbreaking feature representations documentation
and how to quickly get started for configuration specifications.  You can also explore our Flutter Date RangePicker example

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 forumsDirect-Trac, or feedback portal. We are always happy to assist you!

Did you find this information helpful?
Yes
No
Help us improve this page
Please provide feedback or comments
Comments (0)
Please sign in to leave a comment
Access denied
Access denied