Articles in this section
Category / Section

How to use resource builder in the Flutter Calendar (SfCalendar)?

2 mins read

In the Flutter Calendar, add the custom widgets for resources by using the resourceHeaderBuilder property of the calendar.

STEP 1: Initialize the default values for the calendar.

final List<CalendarView> _allowedViews = <CalendarView>[
  CalendarView.timelineDay,
  CalendarView.timelineWeek,
  CalendarView.timelineWorkWeek,
  CalendarView.timelineMonth,
];
final List<String> _subjectCollection = <String>[];
final List<Color> _colorCollection = <Color>[];
List<Appointment> _shiftCollection = <Appointment>[];
final List<CalendarResource> _employeeCollection = <CalendarResource>[];
final List<String> _nameCollection = <String>[];
final List<String> _userImages=<String>[];
_DataSource? _events;
 
@override
void initState() {
  _addResourceDetails();
  _addResources();
  _addAppointmentDetails();
  _addAppointments();
  _events = _DataSource(_shiftCollection, _employeeCollection);
  super.initState();
}

STEP 2: Add the resources by using the displayNamecoloridimage 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),
        image:
        i < _userImages.length ? ExactAssetImage(_userImages[i]) : null
    ));
  }
}

STEP 3: Replace the resource header view by using the resourceHeaderBuilder property of the calendar.

child: SfCalendar(
  view: CalendarView.timelineWeek,
  showDatePickerButton: true,
  allowedViews: _allowedViews,
  dataSource: _events,
  resourceViewHeaderBuilder: resourceBuilder,
),
 
Widget resourceBuilder(
    BuildContext context, ResourceViewHeaderDetails details) {
  if (details.resource.image != null) {
    return Column(
      mainAxisAlignment: MainAxisAlignment.center,
      crossAxisAlignment: CrossAxisAlignment.center,
      mainAxisSize: MainAxisSize.max,
      children: [
        CircleAvatar(backgroundImage:details.resource.image,backgroundColor: details.resource.color),
        Center(child: Text(details.resource.displayName,textAlign: TextAlign.center,)),
      ],
    );
  }
  else {
    return Container(
      color: details.resource.color,
      child: Text(details.resource.displayName),
    );
  }
}

View sample in GitHub

Flutter Calendar resource header builder

 

Conclusion

I hope you enjoyed learning about how to use resource builder in the Flutter Calendar (SfCalendar).

You can refer to our Flutter Calendar feature tour page to know about its other groundbreaking feature representations and documentation, and how to quickly get started for configuration specifications. You can also explore our  Flutter Calendar example 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 forumsDirect-Trac, or feedback portal. We are always happy to assist you!


Did you find this information helpful?
Yes
No
Help us improve this page
Please provide feedback or comments
Comments (0)
Please  to leave a comment
Access denied
Access denied