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

Range selection using the multiple view picker in the Flutter Date Range Picker (SfDateRangePicker)


In the Flutter date range picker, you can select the dates ranges from different pickers.

picker with selected ranges

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

 

View sample in Github.

Screenshots:

Button texts

picker with selected ranges

selected ranges

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