Switching views goes to first appointment in the list
My list of appointments starts before the current month (i.e in 2023).
I start in the CalendarView.month view as my default. As long as I switch between month and day views, the selected date is preserved.
When I switch to a CalendarView.schedule, and then go back to CalendarView.month, it shows the first appointment in the list, i.e from August of 2023 instead of October 2025, the current date. How can change that behavior?
Setting
initialSelectedDate: DateTime.now(),
initialDisplayDate: DateTime.now(),
has no effect. Using onViewChanged to set the controller to today causes runtime errors.
void calendarViewChanged(ViewChangedDetails _viewChangedDetails) {
_controller.displayDate = DateTime.now(); //
}
The following assertion was thrown building _OpacityWidget(state: _OpacityWidgetState#5f79b):
setState() or markNeedsBuild() called during build.
My SfCalendar code:
SfCalendar(
controller: _controller,
view: CalendarView.month,
allowedViews: [
CalendarView.day,
CalendarView.month,
CalendarView.schedule
],
showNavigationArrow: true,
dataSource: _AppointmentDataSource(appointments), //MeetingDataSource(meetings),
showDatePickerButton: true ,
showTodayButton: true ,
// onViewChanged: calendarViewChanged,
allowViewNavigation:true,
appointmentBuilder: _appointmentBuilder,
monthViewSettings: MonthViewSettings(
showAgenda: false,
appointmentDisplayMode: MonthAppointmentDisplayMode.appointment,
),
);
Hi Andreas Knoefel,
We have validated your code snippet, and this runtime error behavior occurs because the display date is being updated during the layout phase, which leads to runtime errors when using setState() directly. To address this, we recommend using the onViewChanged callback and wrapping the display date update inside WidgetsBinding.instance.addPostFrameCallback.
This ensures the update happens after the layout is complete and maintains the current date when switching from CalendarView.schedule back to CalendarView.month. We have shared the code snippet for your reference.
Code snippet:
|
onViewChanged: (ViewChangedDetails details) { WidgetsBinding.instance.addPostFrameCallback((_) { _controller.displayDate = _today; }); }, |
We
have attached the prepared sample and user guide for your reference.
onViewChanged: Callbacks
in Flutter Event Calendar widget | Syncfusion | Scheduler
Regards,
Natrayan
Attachment: f_197598_e6406baf.zip
Almost there!
The runtime errors are gone, but now I am stuck in the current month. The monthPicker or previous month "<" and next month ">" navigation are overwritten by the function call.
I solved the problem like this, but this is not the most elegant solution.
monthViewSettings: MonthViewSettings(
showTrailingAndLeadingDates: false,
),
void calendarViewChanged(ViewChangedDetails _viewChangedDetails) {
WidgetsBinding.instance.addPostFrameCallback((_) {
_controller.displayDate = _viewChangedDetails.visibleDates[0];
});
}
Hi Andreas Knoefel,
The earlier approach involved assigning the current date as the display date each time the onViewChanged callback was triggered. This meant that even when navigating to a different month using the arrows or pickers, the view would immediately snap back to the current month, preventing proper navigation.
To resolve this, the updated logic tracks the last view and resets the display date only when the user switches between views. If the view remains the same, the handler exits early, allowing navigation to work as expected.
Code
Snippet:
|
onViewChanged: (ViewChangedDetails details) { final CalendarView? currentView = _controller.view; if (currentView == null || currentView == _lastView) { return; } _lastView = currentView; WidgetsBinding.instance.addPostFrameCallback((_) { _controller.displayDate = _today; }); }, |
We have attached the modified sample and output for reference.
Regards,
Natrayan
Attachment: f_197598_4a43c60b.zip
I need to find more bugs, because all of you are always so helpful ⭐️⭐️⭐️⭐️⭐️. Thank you
Hi Andreas,
We truly appreciate your feedback and are always glad to be of assistance. Please don't hesitate to reach out if you need any further assistance. We're always happy to help.
Regards,
Maryline A.
- 5 Replies
- 3 Participants
- Marked answer
-
AK Andreas Knoefel
- Oct 10, 2025 12:59 AM UTC
- Oct 20, 2025 05:22 AM UTC