Articles in this section
Category / Section

How to switch between views in the Flutter SfCalendar?

4 mins read

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),
),

 

Note: addPostFrameCallback will be called after the widget's build() method is completed.

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;  }}

View the GitHub sample here

Flutter Calendar view switch

 

Conclusion

I hope you enjoyed learning about how to switch between views in the Flutter SfCalendar.

You can refer to our Flutter SfCalendar  featuretour page to know about its other groundbreaking feature representations and documentation, and how to quickly get started for configuration specifications. You can also explore our Flutter SfCalendar demo 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