Category / Section
How to integrate Event Calendar (SfCalendar) with date picker (SfDateRangePicker) in Flutter
2 mins read
In the Flutter Event Calendar, you can integrate the Calendar with DateRangePicker controls for customization needs.
STEP 1: Inside the state, set the default values for the calendar and date range picker.
final CalendarController _calendarController = CalendarController(); final DateRangePickerController _dateRangePickerController = DateRangePickerController();
STEP 2: Hide the header and view header of the calendar and place the picker with single week and place the SfCalendar and SfDateRangePicker widget inside the column widget
child: Column( children: [ Container( height: 100, child: SfDateRangePicker( controller: _dateRangePickerController, showNavigationArrow: true, allowViewNavigation: false, monthViewSettings: DateRangePickerMonthViewSettings( numberOfWeeksInView: 1), onSelectionChanged: selectionChanged, ), ), Expanded( child: SfCalendar( headerHeight: 0, controller: _calendarController, viewHeaderHeight: 0, dataSource: _getCalendarDataSource(), onViewChanged: viewChanged, ), ), ], ),
STEP 3: Using the onSelectionChanged callback of the Flutter date range picker to move the calendar view, and apply the picker selection based on the onViewChanged callback of the Flutter event calendar.
void viewChanged(ViewChangedDetails viewChangedDetails) { SchedulerBinding.instance!.addPostFrameCallback((timeStamp) { _dateRangePickerController.selectedDate = viewChangedDetails.visibleDates[0]; _dateRangePickerController.displayDate = viewChangedDetails.visibleDates[0]; }); } void selectionChanged(DateRangePickerSelectionChangedArgs args) { SchedulerBinding.instance!.addPostFrameCallback((timeStamp) { _calendarController.displayDate = args.value; }); }
Screenshot
|
|