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 update blackout dates using onViewChanged callback in the Flutter Date Range Picker

Platform: Flutter |
Control: SfDateRangePicker |
Published Date: February 18, 2021 |
Last Revised Date: May 5, 2021

In the Flutter date picker, you can update the blackoutDates by using the onViewChanged callback of the date picker.

Inside the state, set the default values for picker.

List<DateTime> _blackoutDates = <DateTime>[];

Using the onViewChanged callback, add first 5 visible dates to the _blackoutDateCollection and assign it _blackoutDates variable.

void viewChanged(DateRangePickerViewChangedArgs args) {
  List<DateTime> _blackoutDateCollection = <DateTime>[];
  final DateTime visibleStartDate = args.visibleDateRange.startDate!;
  _blackoutDateCollection.add(visibleStartDate);
  _blackoutDateCollection
      .add(visibleStartDate.add(Duration(days: 1)));
  _blackoutDateCollection
      .add(visibleStartDate.add(Duration(days: 2)));
  _blackoutDateCollection
      .add(visibleStartDate.add(Duration(days: 3)));
  _blackoutDateCollection
      .add(visibleStartDate.add(Duration(days: 4)));
  SchedulerBinding.instance!.addPostFrameCallback((timeStamp) {
    setState(() {
      _blackoutDates = _blackoutDateCollection;
    });
  });
}

Assign _blackoutDates to the blackoutDates property of the DateRangePickerMonthViewSettings.

child: SfDateRangePicker(
  selectionMode: DateRangePickerSelectionMode.single,
  monthViewSettings: DateRangePickerMonthViewSettings(
      blackoutDates: _blackoutDates),
  monthCellStyle: DateRangePickerMonthCellStyle(blackoutDateTextStyle:
    TextStyle(
        color: Colors.red,
        decoration: TextDecoration.lineThrough),
  ),
  onViewChanged: viewChanged,
)

View sample in GitHub

blackoutdategif

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