createChild method

  1. @override
void createChild(
  1. int index
)
override

Called during layout when a new child is needed. The child should be inserted into the child list in the appropriate position. Its index and scroll offsets will automatically be set appropriately.

The index argument gives the index of the child to show. It is possible for negative indices to be requested. For example: if the user scrolls from child 0 to child 10, and then those children get much smaller, and then the user scrolls back up again, this method will eventually be asked to produce a child for index -1.

If no child corresponds to index, then do nothing.

Which child is indicated by index zero depends on the GrowthDirection specified in the RenderSliverVariableSizeBoxAdaptor.constraints. For example if the children are the alphabet, then if SliverConstraints.growthDirection is GrowthDirection.forward then index zero is A, and index 25 is Z. On the other hand if SliverConstraints.growthDirection is GrowthDirection.reverse then index zero is Z, and index 25 is A.

During a call to createChild it is valid to remove other children from the RenderSliverVariableSizeBoxAdaptor object if they were not created during this frame and have not yet been updated during this frame. It is not valid to add any other children to this render object.

If this method does not create a child for a given index greater than or equal to zero, then computeMaxScrollOffset must be able to return a precise value.

Implementation

@override
void createChild(int index) {
  assert(_currentlyUpdatingChildIndex == null);
  owner!.buildScope(this, () {
    Element? newChild;
    try {
      _currentlyUpdatingChildIndex = index;
      newChild = updateChild(_childElements[index], _build(index), index);
    } finally {
      _currentlyUpdatingChildIndex = null;
    }
    if (newChild != null) {
      _childElements[index] = newChild;
    } else {
      _childElements.remove(index);
    }
  });
}