layout method

  1. @override
PwBox layout(
  1. Context context,
  2. BoxConstraints constraints
)
override

layout API.

Implementation

@override
PwBox layout(Context context, BoxConstraints constraints) {
  final int cols = crossAxisCount < 1 ? 1 : crossAxisCount;
  final double maxW = constraints.hasBoundedWidth ? constraints.maxWidth : 400;
  final double cellW =
      (maxW - crossAxisSpacing * (cols - 1)).clamp(1, maxW) / cols;
  final double cellH = cellW / childAspectRatio;
  final List<(PwBox, PwOffset)> placed = <(PwBox, PwOffset)>[];
  for (int i = 0; i < children.length; i++) {
    final int col = i % cols;
    final int row = i ~/ cols;
    final PwBox box = children[i].layout(
      context,
      BoxConstraints.tight(PwSize(cellW, cellH)),
    );
    placed.add((
      box,
      PwOffset(
        col * (cellW + crossAxisSpacing),
        row * (cellH + mainAxisSpacing),
      ),
    ));
  }
  final int rows = children.isEmpty ? 0 : ((children.length + cols - 1) ~/ cols);
  final double h = rows == 0
      ? 0
      : rows * cellH + (rows - 1) * mainAxisSpacing;
  return GroupBox(constraints.constrain(PwSize(maxW, h)), placed);
}