We use cookies to give you the best experience on our website. If you continue to browse, then you agree to our privacy policy and cookie policy. Image for the cookie policy date
Unfortunately, activation email could not send to your email. Please try again.
Syncfusion Feedback

How to get the start and end date of the selected range in the Flutter Date Range Picker (SfDateRangePicker)

Platform: Flutter |
Control: SfDateRangePicker |
Published Date: January 5, 2021 |
Last Revised Date: April 22, 2021

In the Flutter Date Range Picker, you can get the start and end date of the selected range by using the startDate and endDate property of the onSelectionChanged callback args.

In initState(), set the default values.

late String _startDate, _endDate;
final DateRangePickerController _controller = DateRangePickerController();
 
@override
void initState() {
  final DateTime today = DateTime.now();
  _startDate = DateFormat('dd, MMMM yyyy').format(today).toString();
  _endDate = DateFormat('dd, MMMM yyyy')
      .format(today.add(Duration(days: 3)))
      .toString();
  _controller.selectedRange = PickerDateRange(today, today.add(Duration(days: 3)));
  super.initState();
}

Using the onSelectionChanged callback get the startDate and endDate of the selected range.

void selectionChanged(DateRangePickerSelectionChangedArgs args) {
  setState(() {
    _startDate =
        DateFormat('dd, MMMM yyyy').format(args.value.startDate).toString();
    _endDate =
        DateFormat('dd, MMMM yyyy').format(args.value.endDate ?? args.value.startDate).toString();
  });
}

Assign _startDate and _endDate values to the Text properties to show the values.

Column(
  children: <Widget>[
    Container(margin: const EdgeInsets.fromLTRB(0, 20, 0, 0),
        height: 50, child: Text('StartRangeDate:' '$_startDate')),
    Container(height: 50, child: Text('EndRangeDate:' '$_endDate')),
    Card(
      margin: const EdgeInsets.fromLTRB(50, 40, 50, 100),
      child: SfDateRangePicker(
        controller: _controller,
        selectionMode: DateRangePickerSelectionMode.range,
        onSelectionChanged: selectionChanged,
        allowViewNavigation: false,
      ),
    )
  ],
),

View sample in GitHub

              Month view

month

 

                  Year view

year

 

 

            Decade view

decade

 

              Century view

century

 

 

ADD COMMENT
You must log in to leave a comment

Please sign in to access our KB

This page will automatically be redirected to the sign-in page in 10 seconds.

Up arrow icon

Warning Icon You are using an outdated version of Internet Explorer that may not display all features of this and other websites. Upgrade to Internet Explorer 8 or newer for a better experience.Close Icon

Live Chat Icon For mobile