Articles in this section
Category / Section

How to style the month cell in the Flutter Calendar?

1 min read

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

 

 

 

Conclusion

I hope you enjoyed learning about how to style the month cell in the Flutter Calendar.

You can refer to our  Flutter Calender feature tour page to know about its other groundbreaking feature representations. You can also explore our Flutter Calendar documentation to understand how to create and manipulate data.

For current customers, you can check out our components from the License and Downloads page. If you are new to Syncfusion, you can try our 30-day free trial to check out our other controls.

If you have any queries or require clarifications, please let us know in the comments section below. You can also contact us through our support forumsDirect-Trac, or feedback portal. We are always happy to assist you!

Did you find this information helpful?
Yes
No
Help us improve this page
Please provide feedback or comments
Comments (0)
Please sign in to leave a comment
Access denied
Access denied