performLayout method

  1. @override
void performLayout()
override

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() {
  final BoxConstraints constraints = this.constraints;
  assert(() {
    final FlutterError? constraintsError = _debugCheckConstraints(
      constraints: constraints,
      reportParentConstraints: true,
    );
    if (constraintsError != null) {
      throw constraintsError;
    }
    return true;
  }());

  final _LayoutSizes sizes = _computeSizes(
    constraints: constraints,
    layoutChild: ChildLayoutHelper.layoutChild,
    getBaseline: ChildLayoutHelper.getBaseline,
  );

  final double crossAxisExtent = sizes.axisSize.crossAxisExtent;
  size = sizes.axisSize.toSize(direction);
  _overflow = math.max(0.0, -sizes.mainAxisFreeSpace);

  final double remainingSpace = math.max(0.0, sizes.mainAxisFreeSpace);
  final bool flipMainAxis = _flipMainAxis;
  final bool flipCrossAxis = _flipCrossAxis;
  final (double leadingSpace, double betweenSpace) = mainAxisAlignment._distributeSpace(remainingSpace, childCount, flipMainAxis, spacing);
  final (_NextChild nextChild, RenderBox? topLeftChild) = flipMainAxis ? (childBefore, lastChild) : (childAfter, firstChild);
  final double? baselineOffset = sizes.baselineOffset;
  assert(baselineOffset == null || (crossAxisAlignment == CrossAxisAlignment.baseline && direction == Axis.horizontal));

  // Position all children in visual order: starting from the top-left child and
  // work towards the child that's farthest away from the origin.
  double childMainPosition = leadingSpace;
  for (RenderBox? child = topLeftChild; child != null; child = nextChild(child)) {
    final double? childBaselineOffset;
    final bool baselineAlign = baselineOffset != null
      && (childBaselineOffset = child.getDistanceToBaseline(textBaseline!, onlyReal: true)) != null;
    final double childCrossPosition = baselineAlign
      ? baselineOffset - childBaselineOffset!
      : crossAxisAlignment._getChildCrossAxisOffset(crossAxisExtent - _getCrossSize(child.size), flipCrossAxis);
    final FlexParentData childParentData = child.parentData! as FlexParentData;
    childParentData.offset = switch (direction) {
      Axis.horizontal => Offset(childMainPosition, childCrossPosition),
      Axis.vertical => Offset(childCrossPosition, childMainPosition),
    };
    childMainPosition += _getMainSize(child.size) + betweenSpace;
  }
}