layoutChildSequence method

  1. @override
void layoutChildSequence()
override

Primary work horse of performLayout.

Subclasses must implement this method to layout the children of the viewport. The TwoDimensionalViewportParentData.layoutOffset must be set during this method in order for the children to be positioned during paint. Further, children of the viewport must be laid out with the expectation that the parent (this viewport) will use their size.

child.layout(constraints, parentUsesSize: true);

The primary methods used for creating and obtaining children is buildOrObtainChildFor, which takes a ChildVicinity that is used by the TwoDimensionalChildDelegate. If a child is not provided by the delegate for the provided vicinity, the method will return null, otherwise, it will return the RenderBox of the child.

After layoutChildSequence is completed, any remaining children that were not obtained will be disposed.

Implementation

@override
void layoutChildSequence() {
  final double horizontalPixels = horizontalOffset.pixels;
  final double verticalPixels = verticalOffset.pixels;
  final double viewportWidth = viewportDimension.width + cacheExtent;
  final double viewportHeight = viewportDimension.height + cacheExtent;
  final TwoDimensionalChildBuilderDelegate builderDelegate =
      delegate as TwoDimensionalChildBuilderDelegate;

  final int maxRowIndex = builderDelegate.maxYIndex!;
  final int maxColumnIndex = builderDelegate.maxXIndex!;

  final int leadingColumn = math.max((horizontalPixels / width).floor(), 0);
  final int leadingRow = math.max((verticalPixels / height).floor(), 0);
  final int trailingColumn = math.min(
    ((horizontalPixels + viewportWidth) / width).ceil(),
    maxColumnIndex,
  );
  final int trailingRow = math.min(
    ((verticalPixels + viewportHeight) / height).ceil(),
    maxRowIndex,
  );

  double xLayoutOffset = (leadingColumn * width) - horizontalOffset.pixels;
  for (int column = leadingColumn; column <= trailingColumn; column++) {
    double yLayoutOffset = (leadingRow * height) - verticalOffset.pixels;
    for (int row = leadingRow; row <= trailingRow; row++) {
      final ChildVicinity vicinity =
          ChildVicinity(xIndex: column, yIndex: row);
      final RenderBox child = buildOrObtainChildFor(vicinity)!;
      child.layout(constraints.tighten(width: width, height: height));

      // Subclasses only need to set the normalized layout offset. The super
      // class adjusts for reversed axes.
      parentDataOf(child).layoutOffset = Offset(xLayoutOffset, yLayoutOffset);
      yLayoutOffset += height;
    }
    xLayoutOffset += width;
  }

  // Set the min and max scroll extents for each axis.
  final double verticalExtent = height * (maxRowIndex + 1);
  verticalOffset.applyContentDimensions(
    0.0,
    clampDouble(
        verticalExtent - viewportDimension.height, 0.0, double.infinity),
  );
  final double horizontalExtent = width * (maxColumnIndex + 1);
  horizontalOffset.applyContentDimensions(
    0.0,
    clampDouble(
        horizontalExtent - viewportDimension.width, 0.0, double.infinity),
  );
  // Super class handles garbage collection too!
}