Articles in this section
Category / Section

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

4 mins read

In the Flutter Calender, you can retrieve the month and year of month view by using onViewChanged callback of calendar.

Using the onViewChanged callback, you can access the month and year of the month view through the visibleDates property of the ViewChangedDetails argument.

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 widgets to display 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 the GitHub sample here

monthgif

 

Conclusion

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