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 additional attributes in events in the Flutter event calendar (SfCalendar)

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

In the Flutter event calendar, you can add the additional attributes for events by using custom appointments.

STEP 1: For adding custom appointments, create custom class Meeting with required fields. We must require two mandatory DateTime fields for events start and end time. In this sample id property added additionally.

class Meeting {
  Meeting(
      {this.eventName,
      this.from,
      this.to,
      this.background,
      this.isAllDay = false,
      this.id});
 
  String eventName;
  DateTime from;
  DateTime to;
  Color background;
  bool isAllDay;
  int id;
}

STEP 2: Then, map the Meeting class properties to calendar by using the override method properties of CalendarDataSource.

class MeetingDataSource extends CalendarDataSource {
  MeetingDataSource(List<Meeting> source) {
    appointments = source;
  }
 
  @override
  DateTime getStartTime(int index) {
    return appointments[index].from;
  }
 
  @override
  DateTime getEndTime(int index) {
    return appointments[index].to;
  }
 
  @override
  String getSubject(int index) {
    return appointments[index].eventName;
  }
 
  @override
  bool isAllDay(int index) {
    return appointments[index].isAllDay;
  }
 
  @override
  Color getColor(int index) {
    return appointments[index].background;
  }
}

STEP 3: Then, you can schedule an appointment for a day using the properties of Meeting class and use the additionally added properties.

child: SfCalendar(
  view: CalendarView.week,
  dataSource: getCalendarDataSource(),
  onTap: calendarTapped,
),
 
MeetingDataSource getCalendarDataSource() {
  List<Meeting> appointments = <Meeting>[];
  appointments.add(Meeting(
      from: DateTime.now(),
      to: DateTime.now().add(const Duration(hours: 1)),
      eventName: 'Meeting',
      background: Colors.pink,
      isAllDay: true,
      id: 1));
  appointments.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,
      id: 2));
  appointments.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,
      id: 5));
  appointments.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,
      id: 3));
  appointments.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,
      id: 4));
 
  return MeetingDataSource(appointments);
}

STEP 4: Implement the onTap callback of the Flutter calendar and you can get the additionally added property details and show the same in alert window.

void calendarTapped(CalendarTapDetails details) {
  if (details.targetElement == CalendarElement.appointment ||
      details.targetElement == CalendarElement.agenda) {
    final Meeting appointmentDetails = details.appointments[0];
    _subjectText = appointmentDetails.eventName;
    _dateText = DateFormat('MMMM dd, yyyy')
        .format(appointmentDetails.from)
        .toString();
    _startTimeText =
        DateFormat('hh:mm a').format(appointmentDetails.from).toString();
    _endTimeText =
        DateFormat('hh:mm a').format(appointmentDetails.to).toString();
    if (appointmentDetails.isAllDay) {
      _timeDetails = 'All day';
    } else {
      _timeDetails = '$_startTimeText - $_endTimeText';
    }
    showDialog(
        context: context,
        builder: (BuildContext context) {
          return AlertDialog(
            title: Container(child: new Text('$_subjectText')),
            content: Container(
              height: 80,
              child: Column(
                children: <Widget>[
                  Row(
                    children: <Widget>[
                      Text(
                        '$_dateText',
                        style: TextStyle(
                          fontWeight: FontWeight.w400,
                          fontSize: 20,
                        ),
                      ),
                    ],
                  ),
                  Row(
                    children: <Widget>[
                      Text(_timeDetails,
                          style: TextStyle(
                              fontWeight: FontWeight.w400, fontSize: 15)),
                    ],
                  ),
                  Row(
                    children: [
                      Text("Id:" + appointmentDetails.id.toString())
                    ],
                  )
                ],
              ),
            ),
            actions: <Widget>[
              new FlatButton(
                  onPressed: () {
                    Navigator.of(context).pop();
                  },
                  child: new Text('close'))
            ],
          );
        });
  }
}

View sample in GitHub 

additional attribute

 

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