How to get the month and year of month view in the Flutter Calendar?
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,
),
),
],
),
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 forums, Direct-Trac, or feedback portal. We are always happy to assist you!
Hi Valentin,
Based on the shared information, we have checked and your requirement is “Show the month name in AppBar and localize the month name in Flutter Calendar”. By using the onViewChanged callback you can get the current visible month and format to the required locale. We have prepared the simple sample for the same. Please find the sample from the following link.
Sample link:
We hope that this helps you. Please let us know if you need further assistance.
Regards, Indumathi R
Hi Valentin,
We have prepared the simple sample for accessing the variable from another dart file. Please find the sample from the following link.
Sample link: https://www.syncfusion.com/downloads/support/directtrac/general/ze/flutter_headercustom1184818460.zip
We hope that this helps you. Please let us know if you need further assistance.
Regards, Indumathi R
This works really well! However it doesn't show the title (month) on the set language (in this case german)
(I am using this method to place the month on the appBar)
class MyAppState extends State<Saveta> { String? _month, _year;
@override Widget build(BuildContext context) { return MaterialApp( localizationsDelegates: const [ //Sprachen GlobalMaterialLocalizations.delegate, GlobalWidgetsLocalizations.delegate, GlobalCupertinoLocalizations.delegate, ], supportedLocales: const [ Locale('de'), ], locale: const Locale('de'), debugShowCheckedModeBanner: false,