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 the month and year of month view in the Flutter Calendar

Platform: Flutter |
Control: SfCalendar |
Published Date: January 7, 2021 |
Last Revised Date: May 5, 2021

In the Flutter Event Calendar, you can get the month and year of month view by using onViewChanged callback of calendar.

Using onViewChanged callback you can get the month and year of the month view using visibleDates property of ViewChangedDetails args.

void viewChanged(ViewChangedDetails viewChangedDetails) {
  SchedulerBinding.instance!
      .addPostFrameCallback((Duration duration) {
    setState(() {
      _month = DateFormat('MMMM').format(viewChangedDetails
          .visibleDates[viewChangedDetails.visibleDates.length ~/ 2]).toString();
      _year = DateFormat('yyyy').format(viewChangedDetails
          .visibleDates[viewChangedDetails.visibleDates.length ~/ 2]).toString();
    });
  });
}

Assign _month and _year values to the Text properties to show the values.

child: Column(
  children: [
    Container(
      child: Text('Month: ' '$_month'),
    ),
    Container(
      child: Text('Year: ' '$_year'),
    ),
    Expanded(
      child: SfCalendar(
        view: CalendarView.month,
        dataSource: getCalendarDataSource(),
        onViewChanged: viewChanged,
      ),
    ),
  ],
),

View sample in GitHub

monthgif

 

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