performLayout method
Do the work of computing the layout for this render object.
Do not call this function directly: call layout instead. This function is called by layout when there is actually work to be done by this render object during layout. The layout constraints provided by your parent are available via the constraints getter.
If sizedByParent is true, then this function should not actually change the dimensions of this render object. Instead, that work should be done by performResize. If sizedByParent is false, then this function should both change the dimensions of this render object and instruct its children to layout.
In implementing this function, you must call layout on each of your children, passing true for parentUsesSize if your layout information is dependent on your child's layout information. Passing true for parentUsesSize ensures that this render object will undergo layout if the child undergoes layout. Otherwise, the child can change its layout information without informing this render object.
Implementation
@override
void performLayout() {
// Size and position all children here.
final constraints = this.constraints.copyWith(minWidth: 0);
// final maxHeight = constraints.maxHeight;
final maxWidth = constraints.maxWidth;
var child = firstChild;
/// Layout Phase
final childSizes = <RenderBox, Size>{};
while (child != null) {
child.layout(constraints, parentUsesSize: true);
final size = child.size;
childSizes[child] = size;
child = childAfter(child);
}
child = firstChild;
/// Position Phase 1: Fit children into rows
var offset = Offset.zero;
var height = 0.0;
var width = 0.0;
final rowMaxHeights = <int, (Iterable<RenderBox>, Size)>{};
var i = 0;
while (child != null) {
final childSize = childSizes[child]!;
final childParentData = child.pData;
final overlaps = offset.dx + childSize.width > maxWidth;
if (overlaps) {
offset = Offset(
0,
offset.dy + (rowMaxHeights[i]?.$2.height ?? 0) + vSpacing,
);
i++;
}
rowMaxHeights[i] = (
[
...?rowMaxHeights[i]?.$1,
child,
],
Size(
max(rowMaxHeights[i]?.$2.width ?? 0, childSize.width),
max(rowMaxHeights[i]?.$2.height ?? 0, childSize.height),
),
);
childParentData.offset = offset;
final cWidth = child.size.width;
// Update
final isLast = child == lastChild;
offset = offset.translate(cWidth + (isLast ? 0 : hSpacing), 0);
width = max(width, offset.dx);
child = childAfter(child);
}
/// Position Phase 2: Align children in rows and calculate height
child = firstChild;
while (child != null) {
final childParentData = child.pData;
final maxVSize = rowMaxHeights.entries
.singleWhere((e) => e.value.$1.contains(child))
.value
.$2
.height;
if (crossAxisAlignment == CrossAxisAlignment.center) {
if (child.size.height < maxVSize) {
childParentData.offset = childParentData.offset.translate(
0,
(maxVSize - child.size.height) / 2,
);
}
}
child = childAfter(child);
}
height = offset.dy + rowMaxHeights[i]!.$2.height;
width = mainAxisSize == MainAxisSize.min ? width : maxWidth;
size = Size(width, height);
}