Articles in this section
Category / Section

How to load appointments On-Demand in Flutter Calendar?

1 min read

In the Flutter Event Calendar, load the appointments in on-demand by using the handleLoadMore  method and customize the loading indicator by using the loadMoreWidgetBuilder.

In initState(), set the default values  for the calendar.

List<String> _subjectCollection = <String>[];
List<Color> _colorCollection = <Color>[];
MeetingDataSource _events = MeetingDataSource(<_Meeting>[]);
 
@override
void initState() {
  _addAppointment();
  super.initState();
}

The loadMoreWidgetBuilder to customizes the load more indicator.

Widget loadMoreWidget(
    BuildContext context, LoadMoreCallback loadMoreAppointments) {
  return FutureBuilder<void>(
    initialData: 'loading',
    future: loadMoreAppointments(),
    builder: (context, snapShot) {
      return Container(
          alignment: Alignment.center, child: CircularProgressIndicator());
    },
  );
}

Define the Flutter calendar with the loadMoreWidgetBuilder.

  child: SfCalendar(
  view: CalendarView.day,
  allowedViews: [
    CalendarView.day,
    CalendarView.week,
    CalendarView.workWeek,
    CalendarView.month,
    CalendarView.timelineDay,
    CalendarView.timelineWeek,
    CalendarView.timelineWorkWeek,
    CalendarView.timelineMonth,
    CalendarView.schedule,
  ],
  dataSource: _events,
  loadMoreWidgetBuilder: loadMoreWidget,
)

Override the handleLoadMore method to populate the appointments in on-demand.

 
class MeetingDataSource extends CalendarDataSource {
  MeetingDataSource(this.source);
 
  List<_Meeting> source;
 
  @override
  List<dynamic> get appointments => source;
 
  @override
  DateTime getStartTime(int index) {
    return source[index].from;
  }
 
  @override
  DateTime getEndTime(int index) {
    return source[index].to;
  }
 
  @override
  bool isAllDay(int index) {
    return source[index].isAllDay;
  }
 
  @override
  String getSubject(int index) {
    return source[index].eventName;
  }
 
  @override
  Color getColor(int index) {
    return source[index].background;
  }
 
  @override
  Future<void> handleLoadMore(DateTime startDate, DateTime endDate) async {
    await Future<void>.delayed(const Duration(seconds: 1));
    List<_Meeting> meetings = <_Meeting>[];
    DateTime date = DateTime(startDate.year, startDate.month, startDate.day);
    DateTime appEndDate =
        DateTime(endDate.year, endDate.month, endDate.day, 23, 59, 59);
    while (date.isBefore(appEndDate)) {
      final List<_Meeting>? data = _dataCollection[date];
      if (data == null) {
        date = date.add(const Duration(days: 1));
        continue;
      }
 
      for (final _Meeting meeting in data) {
        if (appointments.contains(meeting)) {
          continue;
        }
 
        meetings.add(meeting);
      }
      date = date.add(const Duration(days: 1));
    }
 
    appointments.addAll(meetings);
    notifyListeners(CalendarDataSourceAction.add, meetings);;
  }
}

View sample in GitHub

Flutter Calendar loadMoreWidget

 

Conclusion

I hope you enjoyed learning about how to load appointments On-Demand in Flutter Calendar.

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
Please  to leave a comment
Access denied
Access denied