onLayout method

  1. @override
void onLayout(
  1. double left,
  2. double top,
  3. double right,
  4. double bottom,
)
override

Implementation

@override
void onLayout(double left, double top, double right, double bottom) {
  Pair<DateTime> pair = _adjustTime(props.range.start, props.range.end);
  int count = computeDayDiff(pair.start, pair.end);
  int tmpCount = count ~/ 7;
  if (count % 7 != 0) {
    tmpCount += 1;
  }

  int rowCount;
  int columnCount;
  if (props.direction == Direction.vertical) {
    columnCount = 7;
    rowCount = tmpCount;
  } else {
    columnCount = tmpCount;
    rowCount = 7;
  }

  _nodeMap.clear();
  List<DateTime> timeList = buildDateRange(pair.start, pair.end, true);
  int rowIndex = 0;
  int columnIndex = 0;

  for (var element in timeList) {
    _nodeMap[element] = CalendarNode(element, rowIndex, columnIndex);
    if (props.direction == Direction.vertical) {
      columnIndex += 1;
      if (columnIndex >= 7) {
        columnIndex = 0;
        rowIndex += 1;
      }
    } else {
      rowIndex += 1;
      if (rowIndex >= 7) {
        columnIndex += 1;
        rowIndex = 0;
      }
    }
  }
  num vw = width;
  num vh = height;
  if (props.cellSize.isNotEmpty) {
    if (props.cellSize.length == 1) {
      num? size = props.cellSize[0];
      if (size != null) {
        vw = columnCount * size;
        vh = rowCount * size;
      }
    } else if (props.cellSize.length == 2) {
      num? w = props.cellSize[0];
      if (w != null) {
        vw = columnCount * w;
      }
      num? h = props.cellSize[1];
      if (h != null) {
        vh = rowCount * h;
      }
    }
  }

  double cellWidth = vw / columnCount;
  double cellHeight = vh / rowCount;
  _nodeMap.forEach((key, node) {
    double left = node.column * cellWidth;
    double top = node.row * cellHeight;
    node.rect = Rect.fromLTWH(left, top, cellWidth, cellHeight);
  });

  ///移除范围以外的数据
  _nodeMap.removeWhere((key, value) {
    if (key.isAfterDay(props.range.end) || key.isBeforeDay(props.range.start)) {
      return true;
    }
    return false;
  });

  _cellWidth = cellWidth;
  _cellHeight = _cellHeight;
  _rowCount = rowCount;
  _columnCount = columnCount;
}