KeepAlive class
Mark a child as needing to stay alive even when it's in a lazy list that would otherwise remove it.
This widget is used in RenderAbstractViewports, such as Viewport or TwoDimensionalViewport, to manage the lifecycle of widgets that need to remain alive even when scrolled out of view.
The SliverChildBuilderDelegate and SliverChildListDelegate delegates,
used with SliverList and SliverGrid, as well as the scroll view
counterparts ListView and GridView, have an addAutomaticKeepAlives
feature, which is enabled by default. This feature inserts
AutomaticKeepAlive widgets around each child, which in turn configure
KeepAlive widgets in response to KeepAliveNotifications.
The same addAutomaticKeepAlives
feature is supported by
TwoDimensionalChildBuilderDelegate and TwoDimensionalChildListDelegate.
Keep-alive behavior can be managed by using KeepAlive directly or by relying on notifications. For convenience, AutomaticKeepAliveClientMixin may be mixed into a State subclass. Further details are available in the documentation for AutomaticKeepAliveClientMixin.
By default, ListView.builder only keeps the widgets currently visible in the viewport alive. When an item scrolls out of view, it may be disposed to free up resources. This can cause the state of StatefulWidgets to be lost if not explicitly preserved.
In this example, each item in the list is a StatefulWidget that maintains a counter. Tapping the "+" button increments the counter. To selectively preserve the state, each item is wrapped in a KeepAlive widget, with the keepAlive parameter set based on the item’s index:
- For even-indexed items,
keepAlive: true
, so their state is preserved even when scrolled off-screen. - For odd-indexed items,
keepAlive: false
, so their state is discarded when they are no longer visible.
To create a local project with this code sample, run:
flutter create --sample=widgets.KeepAlive.1 mysample
See also:
- AutomaticKeepAlive, which allows subtrees to request to be kept alive in lazy lists.
- AutomaticKeepAliveClientMixin, which is a mixin with convenience methods for clients of AutomaticKeepAlive. Used with State subclasses.
- Inheritance
Constructors
- KeepAlive.new({Key? key, required bool keepAlive, required Widget child})
-
Marks a child as needing to remain alive.
const
Properties
- child → Widget
-
The widget below this widget in the tree.
finalinherited
- debugTypicalAncestorWidgetClass → Type
-
Describes the RenderObjectWidget that is typically used to set up the
ParentData that applyParentData will write to.
no setteroverride
- debugTypicalAncestorWidgetDescription → String
-
Describes the RenderObjectWidget that is typically used to set up the
ParentData that applyParentData will write to.
no setteroverride
- hashCode → int
-
The hash code for this object.
no setterinherited
- keepAlive → bool
-
Whether to keep the child alive.
final
- 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
-
applyParentData(
RenderObject renderObject) → void -
Write the data from this widget into the given render object's parent data.
override
-
createElement(
) → ParentDataElement< KeepAliveParentDataMixin> -
Inflates this configuration to a concrete instance.
inherited
-
debugCanApplyOutOfTurn(
) → bool -
Whether the ParentDataElement.applyWidgetOutOfTurn method is allowed
with this widget.
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.
override
-
debugIsValidRenderObject(
RenderObject renderObject) → bool -
Checks if this widget can apply its parent data to the provided
renderObject
.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, int wrapWidth = 65}) → 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
Operators
-
operator ==(
Object other) → bool -
The equality operator.
inherited