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() {
  // Iterate through each sliver.
  // Get the parent's dimensions.
  final double crossAxisExtent = constraints.crossAxisExtent;
  assert(crossAxisExtent.isFinite);

  // First, layout each child with flex == 0 or null.
  int totalFlex = 0;
  double remainingExtent = crossAxisExtent;
  RenderSliver? child = firstChild;
  while (child != null) {
    final SliverPhysicalParentData childParentData = child.parentData! as SliverPhysicalParentData;
    final int flex = childParentData.crossAxisFlex ?? 0;
    if (flex == 0) {
      // If flex is 0 or null, then the child sliver must provide their own crossAxisExtent.
      assert(_assertOutOfExtent(remainingExtent));
      child.layout(constraints.copyWith(crossAxisExtent: remainingExtent), parentUsesSize: true);
      final double? childCrossAxisExtent = child.geometry!.crossAxisExtent;
      assert(childCrossAxisExtent != null);
      remainingExtent = math.max(0.0, remainingExtent - childCrossAxisExtent!);
    } else {
      totalFlex += flex;
    }
    child = childAfter(child);
  }
  final double extentPerFlexValue = remainingExtent / totalFlex;

  child = firstChild;

  // At this point, all slivers with constrained cross axis should already be laid out.
  // Layout the rest and keep track of the child geometry with greatest scrollExtent.
  geometry = SliverGeometry.zero;
  while (child != null) {
    final SliverPhysicalParentData childParentData = child.parentData! as SliverPhysicalParentData;
    final int flex = childParentData.crossAxisFlex ?? 0;
    double childExtent;
    if (flex != 0) {
      childExtent = extentPerFlexValue * flex;
      assert(_assertOutOfExtent(childExtent));
      child.layout(constraints.copyWith(
        crossAxisExtent: extentPerFlexValue * flex,
      ), parentUsesSize: true);
    } else {
      childExtent = child.geometry!.crossAxisExtent!;
    }
    final SliverGeometry childLayoutGeometry = child.geometry!;
    if (geometry!.scrollExtent < childLayoutGeometry.scrollExtent) {
      geometry = childLayoutGeometry;
    }
    child = childAfter(child);
  }

  // Go back and correct any slivers using a negative paint offset if it tries
  // to paint outside the bounds of the sliver group.
  child = firstChild;
  double offset = 0.0;
  while (child != null) {
    final SliverPhysicalParentData childParentData = child.parentData! as SliverPhysicalParentData;
    final SliverGeometry childLayoutGeometry = child.geometry!;
    final double remainingExtent = geometry!.scrollExtent - constraints.scrollOffset;
    final double paintCorrection = childLayoutGeometry.paintExtent > remainingExtent
      ? childLayoutGeometry.paintExtent - remainingExtent
      : 0.0;
    final double childExtent = child.geometry!.crossAxisExtent ?? extentPerFlexValue * (childParentData.crossAxisFlex ?? 0);
    // Set child parent data.
    childParentData.paintOffset = switch (constraints.axis) {
      Axis.vertical   => Offset(offset, -paintCorrection),
      Axis.horizontal => Offset(-paintCorrection, offset),
    };
    offset += childExtent;
    child = childAfter(child);
  }
}