Articles in this section
Category / Section

How to get the month and year of month view in the Flutter Calendar?

1 min read

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

 

Conclusion

I hope you enjoyed learning about how to get the month and year of month view 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
Please sign in to leave a comment
Access denied
Access denied