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() {
  childManager.didStartLayout();
  childManager.setDidUnderflow(false);

  final double itemExtent = this.itemExtent;

  final double scrollOffset = constraints.scrollOffset + constraints.cacheOrigin;
  assert(scrollOffset >= 0.0);
  final double remainingExtent = constraints.remainingCacheExtent;
  assert(remainingExtent >= 0.0);
  final double targetEndScrollOffset = scrollOffset + remainingExtent;

  final BoxConstraints childConstraints = constraints.asBoxConstraints(
    minExtent: itemExtent,
    maxExtent: itemExtent,
  );

  final int firstIndex = getMinChildIndexForScrollOffset(scrollOffset, itemExtent);
  final int targetLastIndex = targetEndScrollOffset.isFinite ?
      getMaxChildIndexForScrollOffset(targetEndScrollOffset, itemExtent) : null;

  if (firstChild != null) {
    final int oldFirstIndex = indexOf(firstChild);
    final int oldLastIndex = indexOf(lastChild);
    final int leadingGarbage = (firstIndex - oldFirstIndex).clamp(0, childCount);
    final int trailingGarbage = targetLastIndex == null ? 0 : (oldLastIndex - targetLastIndex).clamp(0, childCount);
    collectGarbage(leadingGarbage, trailingGarbage);
  } else {
    collectGarbage(0, 0);
  }

  if (firstChild == null) {
    if (!addInitialChild(index: firstIndex, layoutOffset: indexToLayoutOffset(itemExtent, firstIndex))) {
      // There are either no children, or we are past the end of all our children.
      final double max = computeMaxScrollOffset(constraints, itemExtent);
      geometry = SliverGeometry(
        scrollExtent: max,
        maxPaintExtent: max,
      );
      childManager.didFinishLayout();
      return;
    }
  }

  RenderBox trailingChildWithLayout;

  for (int index = indexOf(firstChild) - 1; index >= firstIndex; --index) {
    final RenderBox child = insertAndLayoutLeadingChild(childConstraints);
    if (child == null) {
      // Items before the previously first child are no longer present.
      // Reset the scroll offset to offset all items prior and up to the
      // missing item. Let parent re-layout everything.
      geometry = SliverGeometry(scrollOffsetCorrection: index * itemExtent);
      return;
    }
    final SliverMultiBoxAdaptorParentData childParentData = child.parentData;
    childParentData.layoutOffset = indexToLayoutOffset(itemExtent, index);
    assert(childParentData.index == index);
    trailingChildWithLayout ??= child;
  }

  if (trailingChildWithLayout == null) {
    firstChild.layout(childConstraints);
    final SliverMultiBoxAdaptorParentData childParentData = firstChild.parentData;
    childParentData.layoutOffset = indexToLayoutOffset(itemExtent, firstIndex);
    trailingChildWithLayout = firstChild;
  }

  while (targetLastIndex == null || indexOf(trailingChildWithLayout) < targetLastIndex) {
    RenderBox child = childAfter(trailingChildWithLayout);
    if (child == null) {
      child = insertAndLayoutChild(childConstraints, after: trailingChildWithLayout);
      if (child == null) {
        // We have run out of children.
        break;
      }
    } else {
      child.layout(childConstraints);
    }
    trailingChildWithLayout = child;
    assert(child != null);
    final SliverMultiBoxAdaptorParentData childParentData = child.parentData;
    childParentData.layoutOffset = indexToLayoutOffset(itemExtent, childParentData.index);
  }

  final int lastIndex = indexOf(lastChild);
  final double leadingScrollOffset = indexToLayoutOffset(itemExtent, firstIndex);
  final double trailingScrollOffset = indexToLayoutOffset(itemExtent, lastIndex + 1);

  assert(firstIndex == 0 || childScrollOffset(firstChild) <= scrollOffset);
  assert(debugAssertChildListIsNonEmptyAndContiguous());
  assert(indexOf(firstChild) == firstIndex);
  assert(targetLastIndex == null || lastIndex <= targetLastIndex);

  final double estimatedMaxScrollOffset = estimateMaxScrollOffset(
    constraints,
    firstIndex: firstIndex,
    lastIndex: lastIndex,
    leadingScrollOffset: leadingScrollOffset,
    trailingScrollOffset: trailingScrollOffset,
  );

  final double paintExtent = calculatePaintOffset(
    constraints,
    from: leadingScrollOffset,
    to: trailingScrollOffset,
  );

  final double cacheExtent = calculateCacheOffset(
    constraints,
    from: leadingScrollOffset,
    to: trailingScrollOffset,
  );

  final double targetEndScrollOffsetForPaint = constraints.scrollOffset + constraints.remainingPaintExtent;
  final int targetLastIndexForPaint = targetEndScrollOffsetForPaint.isFinite ?
      getMaxChildIndexForScrollOffset(targetEndScrollOffsetForPaint, itemExtent) : null;
  geometry = SliverGeometry(
    scrollExtent: estimatedMaxScrollOffset,
    paintExtent: paintExtent,
    cacheExtent: cacheExtent,
    maxPaintExtent: estimatedMaxScrollOffset,
    // Conservative to avoid flickering away the clip during scroll.
    hasVisualOverflow: (targetLastIndexForPaint != null && lastIndex >= targetLastIndexForPaint)
      || constraints.scrollOffset > 0.0,
  );

  // We may have started the layout while scrolled to the end, which would not
  // expose a new child.
  if (estimatedMaxScrollOffset == trailingScrollOffset)
    childManager.setDidUnderflow(true);
  childManager.didFinishLayout();
}