SliverChildBuilderDelegate class

A delegate that supplies children for slivers using a builder callback.

Many slivers lazily construct their box children to avoid creating more children than are visible through the Viewport. This delegate provides children using a NullableIndexedWidgetBuilder callback, so that the children do not even have to be built until they are displayed.

The widgets returned from the builder callback are automatically wrapped in AutomaticKeepAlive widgets if addAutomaticKeepAlives is true (the default) and in RepaintBoundary widgets if addRepaintBoundaries is true (also the default).

Accessibility

The CustomScrollView requires that its semantic children are annotated using IndexedSemantics. This is done by default in the delegate with the addSemanticIndexes parameter set to true.

If multiple delegates are used in a single scroll view, then the indexes will not be correct by default. The semanticIndexOffset can be used to offset the semantic indexes of each delegate so that the indexes are monotonically increasing. For example, if a scroll view contains two delegates where the first has 10 children contributing semantics, then the second delegate should offset its children by 10.

This sample code shows how to use semanticIndexOffset to handle multiple delegates in a single scroll view.
link
CustomScrollView(
  semanticChildCount: 4,
  slivers: <Widget>[
    SliverGrid(
      gridDelegate: _gridDelegate,
      delegate: SliverChildBuilderDelegate(
        (BuildContext context, int index) {
           return const Text('...');
         },
         childCount: 2,
       ),
     ),
    SliverGrid(
      gridDelegate: _gridDelegate,
      delegate: SliverChildBuilderDelegate(
        (BuildContext context, int index) {
           return const Text('...');
         },
         childCount: 2,
         semanticIndexOffset: 2,
       ),
     ),
  ],
)

In certain cases, only a subset of child widgets should be annotated with a semantic index. For example, in ListView.separated() the separators do not have an index associated with them. This is done by providing a semanticIndexCallback which returns null for separators indexes and rounds the non-separator indexes down by half.

This sample code shows how to use semanticIndexCallback to handle annotating a subset of child nodes with a semantic index. There is a Spacer widget at odd indexes which should not have a semantic index.
link
CustomScrollView(
  semanticChildCount: 5,
  slivers: <Widget>[
    SliverGrid(
      gridDelegate: _gridDelegate,
      delegate: SliverChildBuilderDelegate(
        (BuildContext context, int index) {
           if (index.isEven) {
             return const Text('...');
           }
           return const Spacer();
         },
         semanticIndexCallback: (Widget widget, int localIndex) {
           if (localIndex.isEven) {
             return localIndex ~/ 2;
           }
           return null;
         },
         childCount: 10,
       ),
     ),
  ],
)

See also:

Inheritance

Constructors

SliverChildBuilderDelegate(NullableIndexedWidgetBuilder builder, {ChildIndexGetter? findChildIndexCallback, int? childCount, bool addAutomaticKeepAlives = true, bool addRepaintBoundaries = true, bool addSemanticIndexes = true, SemanticIndexCallback semanticIndexCallback = _kDefaultSemanticIndexCallback, int semanticIndexOffset = 0})
Creates a delegate that supplies children for slivers using the given builder callback.
const

Properties

addAutomaticKeepAlives bool
Whether to wrap each child in an AutomaticKeepAlive.
final
addRepaintBoundaries bool
Whether to wrap each child in a RepaintBoundary.
final
addSemanticIndexes bool
Whether to wrap each child in an IndexedSemantics.
final
builder NullableIndexedWidgetBuilder
Called to build children for the sliver.
final
childCount int?
The total number of children this delegate can provide.
final
estimatedChildCount int?
Returns an estimate of the number of children this delegate will build.
no setteroverride
findChildIndexCallback ChildIndexGetter?
Called to find the new index of a child based on its key in case of reordering.
final
hashCode int
The hash code for this object.
no setterinherited
runtimeType Type
A representation of the runtime type of the object.
no setterinherited
semanticIndexCallback SemanticIndexCallback
A SemanticIndexCallback which is used when addSemanticIndexes is true.
final
semanticIndexOffset int
An initial offset to add to the semantic indexes generated by this widget.
final

Methods

build(BuildContext context, int index) Widget?
Returns the child with the given index.
override
debugFillDescription(List<String> description) → void
Add additional information to the given description for use by toString.
inherited
didFinishLayout(int firstIndex, int lastIndex) → void
Called at the end of layout to indicate that layout is now complete.
inherited
estimateMaxScrollOffset(int firstIndex, int lastIndex, double leadingScrollOffset, double trailingScrollOffset) double?
Returns an estimate of the max scroll extent for all the children.
inherited
findIndexByKey(Key key) int?
Find index of child element with associated key.
override
noSuchMethod(Invocation invocation) → dynamic
Invoked when a nonexistent method or property is accessed.
inherited
shouldRebuild(covariant SliverChildBuilderDelegate oldDelegate) bool
Called whenever a new instance of the child delegate class is provided to the sliver.
override
toString() String
A string representation of this object.
inherited

Operators

operator ==(Object other) bool
The equality operator.
inherited