How to Add, Remove, Update the Resources in the Flutter Calendar?
In the Flutter event calendar, you can dynamically insert, remove, reset the resource by using the insert(), remove(), and clear() method.
STEP 1: In the initState() method, set the default values for the calendar:
List<String> _subjectCollection=<String>[]; List<Color> _colorCollection=<Color>[]; List<Appointment> _shiftCollection=<Appointment>[]; List<CalendarResource> _employeeCollection=<CalendarResource>[]; List<String> _nameCollection=<String>[]; _DataSource? _events; @override void initState() { _addResourceDetails(); _addResources(); _addAppointmentDetails(); _addAppointments(); _events = _DataSource(_shiftCollection, _employeeCollection); super.initState(); }
STEP 2: Create a resource view using the displayName, color, id and image properties of the CalendarResource.
void _addResources() { Random random = Random(); for (int i = 0; i < _nameCollection.length; i++) { _employeeCollection.add(CalendarResource( displayName: _nameCollection[i], id: '000' + i.toString(), color: Color.fromRGBO( random.nextInt(255), random.nextInt(255), random.nextInt(255), 1), } }
STEP 3: Add the resources to the appointment by using the resources property of the CalendarDataSource.
class _DataSource extends CalendarDataSource { _DataSource(List<Appointment> source, List<CalendarResource> resourceColl) { appointments = source; resources = resourceColl; } }
STEP 4: You can assign resources to appointments using the resourceIds property of the appointment:
void _addAppointments() { _shiftCollection = <Appointment>[]; final Random random = Random(); for (int i = 0; i < _employeeCollection.length; i++) { final List<String> _employeeIds = <String>[_employeeCollection[i].id.toString()]; if (i == _employeeCollection.length - 1) { int index = random.nextInt(5); index = index == i ? index + 1 : index; _employeeIds.add(_employeeCollection[index].id.toString()); } for (int k = 0; k < 365; k++) { if (_employeeIds.length > 1 && k % 2 == 0) { continue; } for (int j = 0; j < 2; j++) { final DateTime date = DateTime.now().add(Duration(days: k + j)); int startHour = 9 + random.nextInt(6); startHour = startHour >= 13 && startHour <= 14 ? startHour + 1 : startHour; final DateTime _shiftStartTime = DateTime(date.year, date.month, date.day, startHour, 0, 0); _shiftCollection.add(Appointment( startTime: _shiftStartTime, endTime: _shiftStartTime.add(const Duration(hours: 1)), subject: _subjectCollection[random.nextInt(8)], color: _colorCollection[random.nextInt(8)], startTimeZone: '', endTimeZone: '', resourceIds: _employeeIds)); } } } }
STEP 6: Assign those events and special regions to the corresponding properties of the calendar.
child: SfCalendar( view: CalendarView.timelineWeek, showDatePickerButton: true, allowedViews: _allowedViews, specialRegions: _specialTimeRegions, dataSource: _events, ),
STEP 7: Use button click to insert, remove or reset the resource collection using insert(), remove(), clear() methods and notify the changes to the calendar UI by notifyListeners method.
TextButton(child: const Text('Insert resource'),onPressed: () { final CalendarResource resource = CalendarResource( displayName: 'Sophia', color: Colors.red, id: '0004', ); _employeeCollection.insert(2, resource); _events!.notifyListeners( CalendarDataSourceAction.addResource, <CalendarResource>[resource]); },), TextButton(child: const Text('Remove resource'),onPressed: () { final CalendarResource resource = _employeeCollection[0]; _employeeCollection.remove(resource); _events!.notifyListeners( CalendarDataSourceAction.removeResource, <CalendarResource>[resource]); },), TextButton(child: const Text('Reset resource'),onPressed: () { _employeeCollection = <CalendarResource>[]; _events!.resources!.clear(); _employeeCollection.add(CalendarResource( displayName: "Sophia", id: '0004', color: Colors.green )); _events!.resources!.addAll(_employeeCollection); _events!.notifyListeners( CalendarDataSourceAction.resetResource, _employeeCollection); },),
Conclusion
I hope you enjoyed learning about how to add, remove, update the resources in the Flutter Calendar (SfCalendar).
You can refer to our Flutter Calender 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 forums, Direct-Trac, or feedback portal. We are always happy to assist you!