insertEventBox method

List<Widget> insertEventBox({
  1. required T event,
  2. required double eventWidth,
  3. required double viewDays,
  4. required DateTime viewStart,
  5. required DateTime viewEnd,
})

Implementation

List<Widget> insertEventBox({
  required T event,
  required double eventWidth,
  required double viewDays,
  required DateTime viewStart,
  required DateTime viewEnd,
}) {
  var startDate = event.startDate.toLocal();
  var endDate = event.endDate?.toLocal() ?? startDate;
  var days = endDate.difference(startDate).inDays + 1;
  List<Widget> eventBoxes = [];
  for (var i = 0; i < days; i++) {
    var eventBoxStartDate = startDate.add(Duration(days: i));
    if (!(eventBoxStartDate.isAfter(viewStart) &&
        eventBoxStartDate.isBefore(viewEnd))) {
      continue;
    }

    var startHour = days > 1 && i > 0 ? 0 : eventBoxStartDate.hour;
    var endHour = days > 1 && i + 1 < days ? 25 : endDate.hour;
    var startMin = startDate.minute;
    var endMin = event.endDate?.toLocal().minute ?? (startMin + 30) % 60;
    double topOffset = startHour * 60 + startMin * 1.0 - 10;

    var duration = (endHour + endMin / 60) - (startHour + startMin / 60);
    double height = duration * 60;

    final left = (eventBoxStartDate.weekday - 1) * eventWidth;
    final right = (viewDays - eventBoxStartDate.weekday) * eventWidth;

    eventBoxes.add(CalendarEventBox<T>(
      event: event,
      top: topOffset,
      height: height,
      left: left,
      right: right,
    ));
  }

  return eventBoxes;
}