buildOrObtainChildFor method

RenderBox? buildOrObtainChildFor(
  1. ChildVicinity vicinity
)

Returns the child for a given ChildVicinity, should be called during layoutChildSequence in order to instantiate or retrieve children.

This method will build the child if it has not been already, or will reuse it if it already exists, whether it was part of the previous frame or kept alive.

Children for the given ChildVicinity will be inserted into the active children list, and so should be visible, or contained within the cacheExtent.

Implementation

RenderBox? buildOrObtainChildFor(ChildVicinity vicinity) {
  assert(vicinity != ChildVicinity.invalid);
  // This should only be called during layout.
  assert(debugDoingThisLayout);
  if (_leadingXIndex == null || _trailingXIndex == null || _leadingXIndex == null || _trailingYIndex == null) {
    // First child of this layout pass. Set leading and trailing trackers.
    _leadingXIndex = vicinity.xIndex;
    _trailingXIndex = vicinity.xIndex;
    _leadingYIndex = vicinity.yIndex;
    _trailingYIndex = vicinity.yIndex;
  } else {
    // If any of these are still null, we missed a child.
    assert(_leadingXIndex != null);
    assert(_trailingXIndex != null);
    assert(_leadingYIndex != null);
    assert(_trailingYIndex != null);

    // Update as we go.
    _leadingXIndex = math.min(vicinity.xIndex, _leadingXIndex!);
    _trailingXIndex = math.max(vicinity.xIndex, _trailingXIndex!);
    _leadingYIndex = math.min(vicinity.yIndex, _leadingYIndex!);
    _trailingYIndex = math.max(vicinity.yIndex, _trailingYIndex!);
  }
  if (_needsDelegateRebuild || (!_children.containsKey(vicinity) && !_keepAliveBucket.containsKey(vicinity))) {
    invokeLayoutCallback<BoxConstraints>((BoxConstraints _) {
      _childManager._buildChild(vicinity);
    });
  } else {
    _keepAliveBucket.remove(vicinity);
    _childManager._reuseChild(vicinity);
  }
  if (!_children.containsKey(vicinity)) {
    // There is no child for this vicinity, we may have reached the end of the
    // children in one or both of the x/y indices.
    return null;
  }

  assert(_children.containsKey(vicinity));
  final RenderBox child = _children[vicinity]!;
  _activeChildrenForLayoutPass[vicinity] = child;
  parentDataOf(child).vicinity = vicinity;
  return child;
}