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() {
  double offset = 0;
  double maxPaintExtent = 0;

  RenderSliver? child = firstChild;


  while (child != null) {
    final double beforeOffsetPaintExtent = calculatePaintOffset(
      constraints,
      from: 0.0,
      to: offset,
    );
    child.layout(
      constraints.copyWith(
        scrollOffset: math.max(0.0, constraints.scrollOffset - offset),
        cacheOrigin: math.min(0.0, constraints.cacheOrigin + offset),
        overlap: math.max(0.0, constraints.overlap - beforeOffsetPaintExtent),
        remainingPaintExtent: constraints.remainingPaintExtent - beforeOffsetPaintExtent,
        remainingCacheExtent: constraints.remainingCacheExtent - calculateCacheOffset(constraints, from: 0.0, to: offset),
        precedingScrollExtent: offset + constraints.precedingScrollExtent,
      ),
      parentUsesSize: true,
    );
    final SliverGeometry childLayoutGeometry = child.geometry!;
    final SliverPhysicalParentData childParentData = child.parentData! as SliverPhysicalParentData;
    childParentData.paintOffset = switch (constraints.axis) {
      Axis.vertical   => Offset(0.0, beforeOffsetPaintExtent),
      Axis.horizontal => Offset(beforeOffsetPaintExtent, 0.0),
    };
    offset += childLayoutGeometry.scrollExtent;
    maxPaintExtent += child.geometry!.maxPaintExtent;
    child = childAfter(child);
    assert(() {
      if (child != null && maxPaintExtent.isInfinite) {
        throw FlutterError(
          'Unreachable sliver found, you may have a sliver following '
          'a sliver with an infinite extent. '
        );
      }
      return true;
    }());
  }

  final double totalScrollExtent = offset;
  offset = 0.0;
  child = firstChild;
  // Second pass to correct out of bound paintOffsets.
  while (child != null) {
    final double beforeOffsetPaintExtent = calculatePaintOffset(
      constraints,
      from: 0.0,
      to: offset,
    );
    final SliverGeometry childLayoutGeometry = child.geometry!;
    final SliverPhysicalParentData childParentData = child.parentData! as SliverPhysicalParentData;
    final double remainingExtent = totalScrollExtent - constraints.scrollOffset;
    if (childLayoutGeometry.paintExtent > remainingExtent) {
      final double paintCorrection = childLayoutGeometry.paintExtent - remainingExtent;
      childParentData.paintOffset = switch (constraints.axis) {
        Axis.vertical   => Offset(0.0, beforeOffsetPaintExtent - paintCorrection),
        Axis.horizontal => Offset(beforeOffsetPaintExtent - paintCorrection, 0.0),
      };
    }
    offset += child.geometry!.scrollExtent;
    child = childAfter(child);
  }

  final double paintExtent = calculatePaintOffset(
    constraints,
    from: math.min(constraints.scrollOffset, 0),
    to: totalScrollExtent,
  );
  final double cacheExtent = calculateCacheOffset(
    constraints,
    from: math.min(constraints.scrollOffset, 0),
    to: totalScrollExtent,
  );
  geometry = SliverGeometry(
    scrollExtent: totalScrollExtent,
    paintExtent: paintExtent,
    cacheExtent: cacheExtent,
    maxPaintExtent: maxPaintExtent,
    hasVisualOverflow: totalScrollExtent > constraints.remainingPaintExtent || constraints.scrollOffset > 0.0,
  );
}