Articles in this section
Category / Section

How to delete an appointment in the Flutter Calendar?

2 mins read

In the Flutter Event Calendar, it is possible to remove an appointment. Notify the actions to the calendar by using the `notifyListener` on the calendar datasource.

Step 1:

In initState(), set the default values for calendar.

@override
void initState() {
  _calendarView = CalendarView.month;
  _selectedAppointment = null;
  meetings = <Meeting>[];
  events = _getCalendarDataSource();
  super.initState();
}

 

Step 2:

Place the calendar inside the body of the Scaffold widget.

return (Scaffold(
  body: Column(
    children: <Widget>[
      Container(
        height: 500,
        child: SfCalendar(
          view: _calendarView,
          initialSelectedDate: DateTime.now(),
          monthViewSettings: MonthViewSettings(showAgenda: true),
          dataSource: events
        ),
      ),
    ],
  ),
));
 
MeetingDataSource _getCalendarDataSource() {
  meetings.add(Meeting(
    from: DateTime.now(),
    to: DateTime.now().add(const Duration(hours: 1)),
    eventName: 'Meeting',
    background: Colors.pink,
    isAllDay: true,
  ));
  meetings.add(Meeting(
    from: DateTime.now().add(const Duration(hours: 4, days: -1)),
    to: DateTime.now().add(const Duration(hours: 5, days: -1)),
    eventName: 'Release Meeting',
    background: Colors.lightBlueAccent,
  ));
  meetings.add(Meeting(
    from: DateTime.now().add(const Duration(hours: 2, days: -2)),
    to: DateTime.now().add(const Duration(hours: 4, days: -2)),
    eventName: 'Performance check',
    background: Colors.amber,
  ));
  meetings.add(Meeting(
    from: DateTime.now().add(const Duration(hours: 6, days: -3)),
    to: DateTime.now().add(const Duration(hours: 7, days: -3)),
    eventName: 'Support',
    background: Colors.green,
  ));
  meetings.add(Meeting(
    from: DateTime.now().add(const Duration(hours: 6, days: 2)),
    to: DateTime.now().add(const Duration(hours: 7, days: 2)),
    eventName: 'Retrospective',
    background: Colors.purple,
  ));
  return MeetingDataSource(meetings);
}
 
 
class MeetingDataSource extends CalendarDataSource {
  MeetingDataSource(this.source);
 
  List<Meeting> source;
 
  @override
  List<dynamic> get appointments => source;
 
  @override
  DateTime getStartTime(int index) {
    return source[index].from;
  }
 
  @override
  DateTime getEndTime(int index) {
    return source[index].to;
  }
 
  @override
  bool isAllDay(int index) {
    return source[index].isAllDay;
  }
 
  @override
  String getSubject(int index) {
    return source[index].eventName;
  }
 
  @override
  String getStartTimeZone(int index) {
    return source[index].startTimeZone;
  }
 
  @override
  String getEndTimeZone(int index) {
    return source[index].endTimeZone;
  }
 
  @override
  Color getColor(int index) {
    return source[index].background;
  }
}
 
 
class Meeting {
  Meeting(
      {@required this.from,
      @required this.to,
      this.background = Colors.green,
      this.isAllDay = false,
      this.eventName = '',
      this.startTimeZone = '',
      this.endTimeZone = '',
      this.description = ''});
 
  final String eventName;
  final DateTime from;
  final DateTime to;
  final Color background;
  final bool isAllDay;
  final String startTimeZone;
  final String endTimeZone;
  final String description;
}

 

Step 3:

Implement the `onTap` callback of the calendar and it holds the tapped details. You can get the appointment from the tapped details. Through this appointment, you can remove an appointment from the calendar datasource.

 

void calendarTapped(CalendarTapDetails calendarTapDetails) {
  if (calendarTapDetails.targetElement==CalendarElement.agenda || calendarTapDetails.targetElement==CalendarElement.appointment) {
    final Meeting appointment = calendarTapDetails.appointments[0];
    _selectedAppointment = appointment;
  }
}

 

Step 4:

Then, remove the appointment from the datasource and notify the remove action to calendar using the `notifyListener`.

Container(
  height: 30,
  child: FlatButton(
    child: Text('Remove appointment'),
    onPressed: () {
      if (_selectedAppointment != null) {
        events.appointments.removeAt(events.appointments
            .indexOf(_selectedAppointment));
        events.notifyListeners(
            CalendarDataSourceAction.remove,
            <Meeting>[]..add(_selectedAppointment));
      }
    },
  ),
),

 

View sample in Github.

Screenshots:

 

With appointment

without appointment

 

Conclusion

I hope you enjoyed learning about how to delete an appointment in the Flutter Calendar.

You can refer to our  Flutter Calendar feature tour page to know about its other groundbreaking feature representations. You can also explore our Flutter Calendar documentation to understand how to create and manipulate data.

For current customers, you can check out our components from the License and Downloads page. If you are new to Syncfusion, you can try our 30-day free trial to check out our other controls.

If you have any queries or require clarifications, please let us know in the comments section below. You can also contact us through our support forumsDirect-Trac, or feedback portal. We are always happy to assist you!

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