SliverList class
A sliver that places multiple box children in a linear array along the main axis.
To learn more about slivers, see CustomScrollView.slivers.
Each child is forced to have the SliverConstraints.crossAxisExtent in the cross axis but determines its own main axis extent.
SliverList determines its scroll offset by "dead reckoning" because children outside the visible part of the sliver are not materialized, which means SliverList cannot learn their main axis extent. Instead, newly materialized children are placed adjacent to existing children.
If the children have a fixed extent in the main axis, consider using SliverFixedExtentList rather than SliverList because SliverFixedExtentList does not need to perform layout on its children to obtain their extent in the main axis and is therefore more efficient.
Child elements' lifecycle
Creation
While laying out the list, visible children's elements, states and render objects will be created lazily based on existing widgets (such as in the case of SliverChildListDelegate) or lazily provided ones (such as in the case of SliverChildBuilderDelegate).
Destruction
When a child is scrolled out of view, the associated element subtree, states and render objects are destroyed. A new child at the same position in the sliver will be lazily recreated along with new elements, states and render objects when it is scrolled back.
Destruction mitigation
In order to preserve state as child elements are scrolled in and out of view, the following options are possible:
-
Moving the ownership of non-trivial UI-state-driving business logic out of the sliver child subtree. For instance, if a list contains posts with their number of upvotes coming from a cached network response, store the list of posts and upvote number in a data model outside the list. Let the sliver child UI subtree be easily recreate-able from the source-of-truth model object. Use StatefulWidgets in the child widget subtree to store instantaneous UI state only.
-
Letting KeepAlive be the root widget of the sliver child widget subtree that needs to be preserved. The KeepAlive widget marks the child subtree's top render object child for keepalive. When the associated top render object is scrolled out of view, the sliver keeps the child's render object (and by extension, its associated elements and states) in a cache list instead of destroying them. When scrolled back into view, the render object is repainted as-is (if it wasn't marked dirty in the interim).
This only works if the SliverChildDelegate subclasses don't wrap the child widget subtree with other widgets such as AutomaticKeepAlive and RepaintBoundary via
addAutomaticKeepAlives
andaddRepaintBoundaries
. -
Using AutomaticKeepAlive widgets (inserted by default in SliverChildListDelegate or SliverChildListDelegate). AutomaticKeepAlive allows descendant widgets to control whether the subtree is actually kept alive or not. This behavior is in contrast with KeepAlive, which will unconditionally keep the subtree alive.
As an example, the EditableText widget signals its sliver child element subtree to stay alive while its text field has input focus. If it doesn't have focus and no other descendants signaled for keepalive via a KeepAliveNotification, the sliver child element subtree will be destroyed when scrolled away.
AutomaticKeepAlive descendants typically signal it to be kept alive by using the AutomaticKeepAliveClientMixin, then implementing the AutomaticKeepAliveClientMixin.wantKeepAlive getter and calling AutomaticKeepAliveClientMixin.updateKeepAlive.
Using more than one delegate in a Viewport
If multiple delegates are used in a single scroll view, the first child of each delegate will always be laid out, even if it extends beyond the currently viewable area. This is because at least one child is required in order to estimateMaxScrollOffset for the whole scroll view, as it uses the currently built children to estimate the remaining children's extent.
See also:
- docs.flutter.dev/ui/layout/scrolling/slivers, a description of what slivers are and how to use them.
- SliverFixedExtentList, which is more efficient for children with the same extent in the main axis.
- SliverPrototypeExtentList, which is similar to SliverFixedExtentList except that it uses a prototype list item instead of a pixel value to define the main axis extent of each item.
- SliverAnimatedList, which animates items added to or removed from a list.
- SliverGrid, which places multiple children in a two dimensional grid.
- SliverAnimatedGrid, a sliver which animates items when they are inserted into or removed from a grid.
- Inheritance
Constructors
- SliverList({Key? key, required SliverChildDelegate delegate})
-
Creates a sliver that places box children in a linear array.
const
- SliverList.builder({Key? key, required NullableIndexedWidgetBuilder itemBuilder, ChildIndexGetter? findChildIndexCallback, int? itemCount, bool addAutomaticKeepAlives = true, bool addRepaintBoundaries = true, bool addSemanticIndexes = true})
- A sliver that places multiple box children in a linear array along the main axis.
-
SliverList.list({Key? key, required List<
Widget> children, bool addAutomaticKeepAlives = true, bool addRepaintBoundaries = true, bool addSemanticIndexes = true}) - A sliver that places multiple box children in a linear array along the main axis.
- SliverList.separated({Key? key, required NullableIndexedWidgetBuilder itemBuilder, ChildIndexGetter? findChildIndexCallback, required NullableIndexedWidgetBuilder separatorBuilder, int? itemCount, bool addAutomaticKeepAlives = true, bool addRepaintBoundaries = true, bool addSemanticIndexes = true})
- A sliver that places multiple box children, separated by box widgets, in a linear array along the main axis.
Properties
- delegate → SliverChildDelegate
-
The delegate that provides the children for this widget.
finalinherited
- hashCode → int
-
The hash code for this object.
no setterinherited
- key → Key?
-
Controls how one widget replaces another widget in the tree.
finalinherited
- runtimeType → Type
-
A representation of the runtime type of the object.
no setterinherited
Methods
-
createElement(
) → SliverMultiBoxAdaptorElement -
RenderObjectWidgets always inflate to a RenderObjectElement subclass.
override
-
createRenderObject(
BuildContext context) → RenderSliverList -
Creates an instance of the RenderObject class that this
RenderObjectWidget represents, using the configuration described by this
RenderObjectWidget.
override
-
debugDescribeChildren(
) → List< DiagnosticsNode> -
Returns a list of DiagnosticsNode objects describing this node's
children.
inherited
-
debugFillProperties(
DiagnosticPropertiesBuilder properties) → void -
Add additional properties associated with the node.
inherited
-
didUnmountRenderObject(
covariant RenderObject renderObject) → void -
A render object previously associated with this widget has been removed
from the tree. The given RenderObject will be of the same type as
returned by this object's createRenderObject.
inherited
-
estimateMaxScrollOffset(
SliverConstraints? constraints, int firstIndex, int lastIndex, double leadingScrollOffset, double trailingScrollOffset) → double? -
Returns an estimate of the max scroll extent for all the children.
inherited
-
noSuchMethod(
Invocation invocation) → dynamic -
Invoked when a nonexistent method or property is accessed.
inherited
-
toDiagnosticsNode(
{String? name, DiagnosticsTreeStyle? style}) → DiagnosticsNode -
Returns a debug representation of the object that is used by debugging
tools and by DiagnosticsNode.toStringDeep.
inherited
-
toString(
{DiagnosticLevel minLevel = DiagnosticLevel.info}) → String -
A string representation of this object.
inherited
-
toStringDeep(
{String prefixLineOne = '', String? prefixOtherLines, DiagnosticLevel minLevel = DiagnosticLevel.debug}) → String -
Returns a string representation of this node and its descendants.
inherited
-
toStringShallow(
{String joiner = ', ', DiagnosticLevel minLevel = DiagnosticLevel.debug}) → String -
Returns a one-line detailed description of the object.
inherited
-
toStringShort(
) → String -
A short, textual description of this widget.
inherited
-
updateRenderObject(
BuildContext context, covariant RenderObject renderObject) → void -
Copies the configuration described by this RenderObjectWidget to the
given RenderObject, which will be of the same type as returned by this
object's createRenderObject.
inherited
Operators
-
operator ==(
Object other) → bool -
The equality operator.
inherited