Articles in this section
Category / Section

How to create timetable using Flutter Event Calendar?

3 mins read

You can create a time table by adding appointments for specific weekdays using the onViewChanged callback in Flutter Calendar.

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

CalendarDataSource _dataSource = _DataSource(<Appointment>[]);
List<String> _subjectCollection = <String>[];
List<DateTime> _startTimeCollection = <DateTime>[];
List<DateTime> _endTimeCollection = <DateTime>[];
List<Color> _colorCollection = <Color>[];
List<TimeRegion> _specialTimeRegion = <TimeRegion>[];
 
@override
void initState() {
  _getSubjectCollection();
  _getStartTimeCollection();
  _getEndTimeCollection();
  _getColorCollection();
  super.initState();
}

Use the onViewchanged to check if the visible date's weekday is not a weekend day, then add appointments for weekdays:

void viewChanged(ViewChangedDetails viewChangedDetails) {
  List<DateTime> visibleDates = viewChangedDetails.visibleDates;
  List<TimeRegion> _timeRegion = <TimeRegion>[];
  List<Appointment> appointments = <Appointment>[];
  _dataSource.appointments!.clear();
 
  for (int i = 0; i < visibleDates.length; i++) {
    if (visibleDates[i].weekday == 6 || visibleDates[i].weekday == 7) {
      continue;
    }
    _timeRegion.add(TimeRegion(
        startTime: DateTime(visibleDates[i].year, visibleDates[i].month,
            visibleDates[i].day, 13, 0, 0),
        endTime: DateTime(visibleDates[i].year, visibleDates[i].month,
            visibleDates[i].day, 14, 0, 0),
        text: 'Break',
        enablePointerInteraction: false));
    SchedulerBinding.instance!.addPostFrameCallback((timeStamp) {
      setState(() {
        _specialTimeRegion = _timeRegion;
      });
    });
    for (int j = 0; j < _startTimeCollection.length; j++) {
      DateTime startTime = new DateTime(
          visibleDates[i].year,
          visibleDates[i].month,
          visibleDates[i].day,
          _startTimeCollection[j].hour,
          _startTimeCollection[j].minute,
          _startTimeCollection[j].second);
      DateTime endTime = new DateTime(
          visibleDates[i].year,
          visibleDates[i].month,
          visibleDates[i].day,
          _endTimeCollection[j].hour,
          _endTimeCollection[j].minute,
          _endTimeCollection[j].second);
      Random random = Random();
      appointments.add(Appointment(
          startTime: startTime,
          endTime: endTime,
          subject: _subjectCollection[random.nextInt(9)],
          color: _colorCollection[random.nextInt(9)]));
    }
  }
  for (int i = 0; i < appointments.length; i++) {
    _dataSource.appointments!.add(appointments[i]);
  }
  _dataSource.notifyListeners(
      CalendarDataSourceAction.reset, _dataSource.appointments!);
}

Then assign the _dataSource to dataSource property of the calendar.

child: SfCalendar(
  dataSource: _dataSource,
  view: CalendarView.week,
  allowedViews: [
    CalendarView.day,
    CalendarView.week,
    CalendarView.workWeek,
    CalendarView.month,
    CalendarView.timelineDay,
    CalendarView.timelineWeek,
    CalendarView.timelineWorkWeek,
    CalendarView.timelineMonth,
    CalendarView.schedule
  ],
  onViewChanged: viewChanged,
  specialRegions: _specialTimeRegion,
),

View the GitHub sample here

Flutter Calendar Time Table

 

Conclusion

I hope you enjoyed learning about how to create timetable using Flutter Event Calendar.

You can refer to our Flutter Calendar featuretour 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 demo 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