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() {
  // Reset for a new frame
  // We always reset the null terminating indices in case rows or columns have
  // been added or removed.
  _rowNullTerminatedIndex = null;
  _columnNullTerminatedIndex = null;
  _mergedVicinities.clear();
  _mergedRows.clear();
  _mergedColumns.clear();

  if (needsDelegateRebuild || didResize) {
    // Recomputes the table metrics, invalidates any cached information.
    for (final _Span span in _columnMetrics.values) {
      span.dispose();
    }
    _columnMetrics.clear();
    for (final _Span span in _rowMetrics.values) {
      span.dispose();
    }
    _rowMetrics.clear();
    _updateColumnMetrics();
    _updateRowMetrics();
    _updateScrollBounds();
  } else {
    // Updates the visible cells based on cached table metrics.
    _updateFirstAndLastVisibleCell();
  }

  if (_firstNonPinnedCell == null &&
      _lastPinnedRow == null &&
      _lastPinnedColumn == null) {
    assert(_lastNonPinnedCell == null);
    return;
  }

  final double? offsetIntoColumn = _firstNonPinnedColumn != null
      ? horizontalOffset.pixels -
          _columnMetrics[_firstNonPinnedColumn]!.leadingOffset -
          _pinnedColumnsExtent
      : null;
  final double? offsetIntoRow = _firstNonPinnedRow != null
      ? verticalOffset.pixels -
          _rowMetrics[_firstNonPinnedRow]!.leadingOffset -
          _pinnedRowsExtent
      : null;

  if (_lastPinnedRow != null && _lastPinnedColumn != null) {
    // Layout cells that are contained in both pinned rows and columns
    _layoutCells(
      start: TableVicinity.zero,
      end: TableVicinity(column: _lastPinnedColumn!, row: _lastPinnedRow!),
      offset: Offset.zero,
    );
  }

  if (_lastPinnedRow != null && _firstNonPinnedColumn != null) {
    // Layout cells of pinned rows - those that do not intersect with pinned
    // columns above
    assert(_lastNonPinnedColumn != null);
    assert(offsetIntoColumn != null);
    _layoutCells(
      start: TableVicinity(column: _firstNonPinnedColumn!, row: 0),
      end: TableVicinity(column: _lastNonPinnedColumn!, row: _lastPinnedRow!),
      offset: Offset(offsetIntoColumn!, 0),
    );
  }
  if (_lastPinnedColumn != null && _firstNonPinnedRow != null) {
    // Layout cells of pinned columns - those that do not intersect with
    // pinned rows above
    assert(_lastNonPinnedRow != null);
    assert(offsetIntoRow != null);
    _layoutCells(
      start: TableVicinity(column: 0, row: _firstNonPinnedRow!),
      end: TableVicinity(column: _lastPinnedColumn!, row: _lastNonPinnedRow!),
      offset: Offset(0, offsetIntoRow!),
    );
  }
  if (_firstNonPinnedCell != null) {
    // Layout all other cells.
    assert(_lastNonPinnedCell != null);
    assert(offsetIntoColumn != null);
    assert(offsetIntoRow != null);
    _layoutCells(
      start: _firstNonPinnedCell!,
      end: _lastNonPinnedCell!,
      offset: Offset(offsetIntoColumn!, offsetIntoRow!),
    );
  }
}