Articles in this section
Category / Section

How to get visible dates details from the Flutter Calendar

1 min read

In Flutter Event calendar, you can get the details of visible dates using the onViewChanged callback of the flutter calendar widget.

Step 1

In initState(), initialize the default values.

String? _startDate, _endDate;
Color? _headerColor, _viewHeaderColor, _calendarColor;
 
@override
void initState() {
  _startDate = '';
  _endDate = '';
  super.initState();
}

 

Step 2

Trigger the onViewChanged callback of the calendar widget and get the visible dates details through the viewChangedDetails.

Expanded(
  child: SfCalendar(
    viewHeaderStyle:
    ViewHeaderStyle(backgroundColor: _viewHeaderColor),
    allowedViews: [
      CalendarView.day,
      CalendarView.week,
      CalendarView.workWeek,
      CalendarView.month,
      CalendarView.timelineDay,
      CalendarView.timelineWeek,
      CalendarView.timelineWorkWeek
    ],
    backgroundColor: _calendarColor,
    view: CalendarView.week,
    monthViewSettings: MonthViewSettings(showAgenda: true),
    dataSource: getCalendarDataSource(),
    onViewChanged: viewChanged,
  ),
),
 
void viewChanged(ViewChangedDetails viewChangedDetails) {
  _startDate = DateFormat('dd, MMMM yyyy')
      .format(viewChangedDetails.visibleDates[0])
      .toString();
  _endDate = DateFormat('dd, MMMM yyyy')
      .format(viewChangedDetails
      .visibleDates[viewChangedDetails.visibleDates.length - 1])
      .toString();
 
  SchedulerBinding.instance!.addPostFrameCallback((duration) {
    setState(() {});
  });
}
 

 

View sample in Github

Flutter calendar visible dates

 

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