Articles in this section
Category / Section

How to add, remove, update the resources in the Flutter Calendar (SfCalendar)

2 mins read

In the Flutter event calendar, you can dynamically insert, remove, reset the resource by using the insert(), remove(), and reset() method.

STEP 1: In initState(), 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 by 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: The resources to the appointments can be assigned by 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: By using the button click, you can insert, remove, reset the resource to the resource collection using insert(), remove(), clear() methods and notify the changes to the calendar UI by notifyListeners.

 

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);
 
},),

View sample in GitHub

Flutter calendar dynamic resource insert, remove, reset

 

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