How to add additional attributes in events in the Flutter Calendar?
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')) ], ); }); } }
Conclusion
I hope you enjoyed learning about how to add additional attributes in events 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 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 forums, Direct-Trac, or feedback portal. We are always happy to assist you!