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 style the month cell in the Flutter Calendar

Platform: Flutter |
Control: SfCalendar |
Published Date: November 17, 2020 |
Last Revised Date: May 5, 2021

In the Flutter Event Calendar, you can add active dates by using the reverse concept of the blackoutDates.

STEP 1: In initState(), set the default values for event calendar.

List<DateTime>? _blackoutDateCollection= <DateTime>[];
List<DateTime>? _activeDates;
 
@override
void initState() {
  DateTime today = DateTime.now();
  today = DateTime(today.year, today.month, today.day);
  _activeDates = [
    today,
    today.add(Duration(days: 5)),
    today.add(Duration(days: 10)),
    today.add(Duration(days: 15)),
    today.add(Duration(days: 20)),
    today.add(Duration(days: 25)),
    today.add(Duration(days: 30)),
    today.add(Duration(days: 35))
  ];
  super.initState();
}

STEP 2: Using onViewChanged callback, check the mentioned active dates are equal to the visible dates. If it is equal, then you can add the active dates and can be selected. Otherwise, add remaining dates in the blackout dates collection.

void viewChanged(ViewChangedDetails viewChangedDetails) {
  DateTime date;
  DateTime startDate = viewChangedDetails.visibleDates[0];
  DateTime endDate = viewChangedDetails
      .visibleDates[viewChangedDetails.visibleDates.length - 1];
  List<DateTime> _blackDates = <DateTime>[];
  for (date = startDate;
  date.isBefore(endDate) || date == endDate;
  date = date.add(const Duration(days: 1))) {
    if (_activeDates!.contains(date)) {
      continue;
    }
 
    _blackDates.add(date);
  }
  SchedulerBinding.instance!.addPostFrameCallback((timeStamp) {
    setState(() {
      _blackoutDateCollection = _blackDates;
    });
  });
}

STEP 3: Assign the above date collections to the blackoutDates property of the Flutter event calendar.

child: SfCalendar(
  view: CalendarView.month,
  blackoutDates: _blackoutDateCollection,
  blackoutDatesTextStyle: TextStyle(
      color: Colors.grey, decoration: TextDecoration.lineThrough),
  onViewChanged: viewChanged,
),

View sample in GitHub

Selectiongif

 

 

 

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