Articles in this section
Category / Section

How to switch between views of the Flutter Calendar

2 mins read

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

 

Note:

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

View sample in GitHub

Flutter Calendar view switch

 

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