Articles in this section
Category / Section

How to integrate Event Calendar (SfCalendar) with date picker (SfDateRangePicker) in Flutter

1 min read

In the Flutter Event Calendar, you can integrate the Calendar with DateRangePicker controls for required customization.

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 inside the column widget place the SfCalendar and SfDateRangePicker.

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 control you can move the calendar view and the picker selection will be applied 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

 

calendarpickergif

 

View sample in GitHub

Did you find this information helpful?
Yes
No
Help us improve this page
Please provide feedback or comments
Comments (0)
Please sign in to leave a comment
Access denied
Access denied