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 get visible dates details from the Flutter Calendar

Platform: Flutter |
Control: SfCalendar |
Published Date: February 24, 2020 |
Last Revised Date: May 5, 2021

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

 

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