Articles in this section
Category / Section

How to get the selected dates using selection changed callback in the Flutter Calendar

1 min read

In the Flutter Event Calendar, Using the onSelectionChanged callback, selected details can be shown in the Alert dialog window. The onSelectionChanged callback is triggered when controller selected date is changed.

In initState(), initialize the default values.

final CalendarController _controller = CalendarController();
String _text = '';

Use the onSelectionChanged callback like the following code snippet and show the details in the Alert dialog window.

child: SfCalendar(
  view: CalendarView.day,
  allowedViews: [
    CalendarView.day,
    CalendarView.week,
    CalendarView.workWeek,
    CalendarView.month,
    CalendarView.timelineDay,
    CalendarView.timelineWeek,
    CalendarView.timelineWorkWeek,
    CalendarView.timelineMonth
  ],
  controller: _controller,
  onSelectionChanged: selectionChanged,
),
 
void selectionChanged(CalendarSelectionDetails details) {
  if (_controller.view == CalendarView.month ||
      _controller.view == CalendarView.timelineMonth) {
    _text = DateFormat('dd, MMMM yyyy').format(details.date!).toString();
  } else {
    _text =
        DateFormat('dd, MMMM yyyy hh:mm a').format(details.date!).toString();
  }
  showDialog(
      context: context,
      builder: (BuildContext context) {
        return AlertDialog(
          title: Container(
              child: new Text("Details shown by selection changed callback")),
          content:
              Container(child: new Text("You have selected " + '$_text')),
          actions: <Widget>[
            new TextButton(
                onPressed: () {
                  Navigator.of(context).pop();
                },
                child: new Text('close'))
          ],
        );
      });
}

View sample in GitHub

Flutter calendar selectionchanged callback

 

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