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() {
  _updateAnimationCache();
  if (needsDelegateRebuild || didResize) {
    // Recomputes the tree row metrics, invalidates any cached information.
    _furthestHorizontalExtent = 0.0;
    _updateRowMetrics();
  } else {
    // Updates the visible rows based on cached _rowMetrics.
    _updateFirstAndLastVisibleRow();
  }

  if (_firstRow == null) {
    assert(_lastRow == null);
    return;
  }
  assert(_firstRow != null && _lastRow != null);

  _Span rowSpan;
  double rowOffset =
      -verticalOffset.pixels + _rowMetrics[_firstRow!]!.leadingOffset;
  for (int row = _firstRow!; row <= _lastRow!; row++) {
    rowSpan = _rowMetrics[row]!;
    final double rowHeight = rowSpan.extent;
    if (_animationLeadingIndices.keys.contains(row)) {
      rowOffset -= rowSpan.animationOffset;
    }
    rowOffset += rowSpan.configuration.padding.leading;

    final TreeVicinity vicinity = TreeVicinity(
      depth: _rowDepths[row]!,
      row: row,
    );
    final RenderBox child = buildOrObtainChildFor(vicinity)!;
    final TwoDimensionalViewportParentData parentData = parentDataOf(child);
    final BoxConstraints childConstraints = BoxConstraints(
      minHeight: rowHeight,
      maxHeight: rowHeight,
      // Width is allowed to be unbounded.
    );
    child.layout(childConstraints, parentUsesSize: true);
    parentData.layoutOffset = Offset(
      (_rowDepths[row]! * indentation) - horizontalOffset.pixels,
      rowOffset,
    );
    rowOffset += rowHeight + rowSpan.configuration.padding.trailing;
    _furthestHorizontalExtent = math.max(
      parentData.layoutOffset!.dx + child.size.width,
      _furthestHorizontalExtent,
    );
  }
  _updateScrollBounds();
}