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 Calendar and in this article, switching between event calendar view has been achieved using the allowedViews property of the calendar.
STEP 1: Inside the state initialize the default values.
final CalendarController _controller = CalendarController();
Color? _headerColor, _viewHeaderColor, _calendarColor;
STEP 2: Place the event calendar to the body of the Scaffold widget.
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),
),
addPostFrameCallback will be called after the widget build() is completed.
STEP 3: Using the OnTap event, you will get the targetElement (get details about the tapped element). By this, you can move to the calendar view using selected date of the calendar.
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!