How to switch between views in the Flutter SfCalendar?
In Flutter Event Calendar, you can navigate between the calendar views using the view property of the SfCalendar widget. This article demonstrates how to switch between event calendar views using the allowedViews property.
STEP 1: Initialize the default values.
final CalendarController _controller = CalendarController(); Color? _headerColor, _viewHeaderColor, _calendarColor;
STEP 2: Place the Event Calendar in the Scaffold body.
body: SfCalendar( view: CalendarView.week, allowedViews: [ CalendarView.day, CalendarView.week, CalendarView.workWeek, CalendarView.month, CalendarView.timelineDay, CalendarView.timelineWeek, CalendarView.timelineWorkWeek ], viewHeaderStyle: ViewHeaderStyle(backgroundColor: viewHeaderColor), backgroundColor: calendarColor, controller: _controller, initialDisplayDate: DateTime.now(), dataSource: getCalendarDataSource(), onTap: calendarTapped, monthViewSettings: MonthViewSettings( navigationDirection: MonthNavigationDirection.vertical), ),
STEP 3: Using the OnTap event, you will get the targetElement (details about the tapped element). With this information, you can navigate to different calendar views based on the selected date.
void calendarTapped(CalendarTapDetails calendarTapDetails) { if (_controller.view == CalendarView.month && calendarTapDetails.targetElement == CalendarElement.calendarCell) { _controller.view = CalendarView.day; } else if ((_controller.view == CalendarView.week || _controller.view == CalendarView.workWeek) && calendarTapDetails.targetElement == CalendarElement.viewHeader) { _controller.view = CalendarView.day; }}
Conclusion
I hope you enjoyed learning about how to switch between views in the Flutter SfCalendar.
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!