We use cookies to give you the best experience on our website. If you continue to browse, then you agree to our privacy policy and cookie policy. Image for the cookie policy date
Unfortunately, activation email could not send to your email. Please try again.
Syncfusion Feedback

How to add the appointments to the Firebase database using appointment editor in the Flutter event calendar (SfCalendar)

Platform: Flutter |
Control: SfCalendar |
Published Date: November 24, 2020 |
Last Revised Date: November 24, 2020

In the Flutter event calendar, you can add the appointment to the Firebase database using appointment editor.

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

CalendarController _controller;
List<String> _eventNameCollection;
 
@override
void initState() {
_controller=CalendarController();
_events = DataSource(getMeetingDetails());
  _selectedAppointment = null;
  _selectedColorIndex = 0;
  _selectedTimeZoneIndex = 0;
  _subject = '';
  _notes = '';
  super.initState();
}

STEP 2: onTap callback event of the calendar widget will return the tapped element, tapped date and the tapped appointment details and you can add the appointment using the editor.

void onCalendarTapped(CalendarTapDetails calendarTapDetails) {
  if (calendarTapDetails.targetElement != CalendarElement.calendarCell &&
      calendarTapDetails.targetElement != CalendarElement.appointment) {
    return;
  }
  
    _selectedAppointment = null;
    _isAllDay = false;
    _selectedColorIndex = 0;
    _selectedTimeZoneIndex = 0;
    _subject = '';
    _notes = '';
    if (_controller.view == CalendarView.month) {
      _controller.view = CalendarView.day;
    } else {
      if (calendarTapDetails.appointments != null &&
          calendarTapDetails.appointments.length == 1) {
        final Meeting meetingDetails = calendarTapDetails.appointments[0];
        _startDate = meetingDetails.from;
        _endDate = meetingDetails.to;
        _isAllDay = meetingDetails.isAllDay;
        _selectedColorIndex =
            _colorCollection.indexOf(meetingDetails.background);
        _selectedTimeZoneIndex = meetingDetails.startTimeZone == ''
            ? 0
            : _timeZoneCollection.indexOf(meetingDetails.startTimeZone);
        _subject = meetingDetails.eventName == '(No title)'
            ? ''
            : meetingDetails.eventName;
        _notes = meetingDetails.description;
        _selectedAppointment = meetingDetails;
      } else {
        final DateTime date = calendarTapDetails.date;
        _startDate = date;
        _endDate = date.add(const Duration(hours: 1));
      }
      _startTime =
          TimeOfDay(hour: _startDate.hour, minute: _startDate.minute);
      _endTime = TimeOfDay(hour: _endDate.hour, minute: _endDate.minute);
      Navigator.push<Widget>(
        context,
        MaterialPageRoute(
            builder: (BuildContext context) => AppointmentEditor()),
      );
    }
}

STEP 3: Assign onCalendarTapped to onTap callback, _events to dataSource property of the Flutter calendar.

child: SfCalendar(
  view: CalendarView.month,
  controller: _controller,
  dataSource: _events,
  onTap: onCalendarTapped,
  monthViewSettings: MonthViewSettings(
      appointmentDisplayMode:
          MonthAppointmentDisplayMode.appointment),
))));

STEP 4: After editing the editor fields, you can add appointment details to calendar and firebase database. Using push() method you can add the appointment data to the firebase database. push() method will create a random id in the database, and the set() method will contain the data from the form. The then() callback will execute after the data is added in the Firebase database.

IconButton(
    padding: const EdgeInsets.fromLTRB(5, 0, 5, 0),
    icon: const Icon(
      Icons.done,
      color: Colors.white,
    ),
    onPressed: () {
      final List<Meeting> meetings = <Meeting>[];
      if (_selectedAppointment != null) {
        _events.appointments.removeAt(
            _events.appointments.indexOf(_selectedAppointment));
        _events.notifyListeners(CalendarDataSourceAction.remove,
            <Meeting>[]..add(_selectedAppointment));
      }
      meetings.add(Meeting(
        from: _startDate,
        to: _endDate,
        background: _colorCollection[_selectedColorIndex],
        startTimeZone: _selectedTimeZoneIndex == 0
            ? ''
            : _timeZoneCollection[_selectedTimeZoneIndex],
        endTimeZone: _selectedTimeZoneIndex == 0
            ? ''
            : _timeZoneCollection[_selectedTimeZoneIndex],
        description: _notes,
        isAllDay: _isAllDay,
        eventName: _subject == '' ? '(No title)' : _subject,
      ));
      final dbRef = FirebaseDatabase.instance
          .reference()
          .child("CalendarData");
      dbRef.push().set({
        "StartTime": _startDate.toString(),
        "EndTime": _endDate.toString(),
        "Subject": _subject,
      }).then((_) {
        Scaffold.of(context).showSnackBar(
            SnackBar(content: Text('Successfully Added')));
      }).catchError((onError) {
        print(onError);
      });
 
      _events.appointments.add(meetings[0]);
 
      _events.notifyListeners(
          CalendarDataSourceAction.add, meetings);
      _selectedAppointment = null;
 
      Navigator.pop(context);
    })

View sample in GitHub

firebase

appointment in mobile

 

 

Note:

Create a sample in firebase console project and configure firebase sample with created Flutter sample and make sure as runnable. Please refer the following link for the same.

 

 

ADD COMMENT
You must log in to leave a comment

Please sign in to access our KB

This page will automatically be redirected to the sign-in page in 10 seconds.

Up arrow icon

Warning Icon You are using an outdated version of Internet Explorer that may not display all features of this and other websites. Upgrade to Internet Explorer 8 or newer for a better experience.Close Icon

Live Chat Icon For mobile
Live Chat Icon