CalendarLayoutConfig.calculate constructor

CalendarLayoutConfig.calculate({
  1. required CalendarConfig config,
  2. required DateTimeRange<DateTime> range,
  3. required Map<DateTime, int?> values,
})

Calculate the layout configuration for the calendar from other configurations.

Implementation

factory CalendarLayoutConfig.calculate({
  required CalendarConfig config,
  required DateTimeRange range,
  required Map<DateTime, int?> values,
}) {
  // calculate the number of blank cells and total cells
  final int firstDay = range.start.weekday;
  final int base = config.firstDayOfWeek;
  final int blanks = (firstDay - base + 7) % 7;
  final int dates = range.duration.inDays + 1;

  // calculate the value range
  int? min, max;
  for (final value in values.values) {
    if (value == null) continue;

    if (min == null) {
      // first value encountered
      min = value;
      max = value;
    } else {
      // update min and max
      if (value < min) {
        min = value;
      } else if (value > max!) {
        max = value;
      }
    }
  }

  // calculate the number of weeks (columns)
  final weeks = ((blanks + dates) / 7).ceil();

  return CalendarLayoutConfig._(
    rawConfig: config,
    weeks: weeks,
    dateBase: range.start,
    values: values,
    paintOffset: config.cellSize + config.cellGap,
    size: Size(
      config.cellSize * weeks + config.cellGap * (weeks - 1),
      config.cellSize * 8 + config.cellGap * 7,
    ),
    blankCells: blanks,
    dateCells: dates,
    minValue: min,
    maxValue: max,
  );
}