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 calendar

Platform: Flutter |
Control: SfCalendar |
Published Date: February 16, 2021 |
Last Revised Date: February 18, 2021

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

In initState() set the default values for calendar.

List<DateTime> _blackoutDates;
 
@override
void initState() {
  _blackoutDates = <DateTime>[];
  super.initState();
}

Using the onViewChanged callback get the first 5 visible dates of the month and add dates to _blackoutDateCollection variable and assign the collection to the _blackoutDates variable.

void viewChanged(ViewChangedDetails viewChangedDetails) {
  List<DateTime> _blackoutDateCollection = <DateTime>[];
 
  _blackoutDateCollection.add(viewChangedDetails.visibleDates[0]);
  _blackoutDateCollection.add(viewChangedDetails.visibleDates[1]);
  _blackoutDateCollection.add(viewChangedDetails.visibleDates[2]);
  _blackoutDateCollection.add(viewChangedDetails.visibleDates[3]);
  _blackoutDateCollection.add(viewChangedDetails.visibleDates[4]);
  SchedulerBinding.instance.addPostFrameCallback((timeStamp) {
    setState(() {
      _blackoutDates = _blackoutDateCollection;
    });
  });
}

Assign _blackoutDates to blackoutdates property of the calendar.

child: SfCalendar(
  view: CalendarView.month,
  dataSource: _getCalendarDataSource(),
  allowedViews: [
    CalendarView.month,
    CalendarView.timelineMonth,
  ],
  blackoutDates: _blackoutDates,
  onViewChanged: viewChanged,
  blackoutDatesTextStyle: TextStyle(
      color: Colors.red, decoration: TextDecoration.lineThrough),
),

View sample in GitHub

Calendar Blackout Dates

 

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
Live Chat Icon