PageStorage class Null safety

Establish a subtree in which widgets can opt into persisting states after being destroyed.

PageStorage is used to save and restore values that can outlive the widget. For example, when multiple pages are grouped in tabs, when a page is switched out, its widget is destroyed and its state is lost. By adding a PageStorage at the root and adding a PageStorageKey to each page, some of the page's state (e.g. the scroll position of a Scrollable widget) will be stored automatically in its closest ancestor PageStorage, and restored when it's switched back.

Usually you don't need to explicitly use a PageStorage, since it's already included in routes.

PageStorageKey is used by Scrollable if ScrollController.keepScrollOffset is enabled to save their ScrollPositions. When more than one scrollable (ListView, SingleChildScrollView, TextField, etc.) appears within the widget's closest ancestor PageStorage (such as within the same route), if you want to save all of their positions independently, you should give each of them unique PageStorageKeys, or set some of their keepScrollOffset false to prevent saving.

This sample shows how to explicitly use a PageStorage to store the states of its children pages. Each page includes a scrollable list, whose position is preserved when switching between the tabs thanks to the help of PageStorageKey.
To create a local project with this code sample, run:
flutter create --sample=widgets.PageStorage.1 mysample

See also:

Inheritance

Constructors

PageStorage({Key? key, required PageStorageBucket bucket, required Widget child})
Creates a widget that provides a storage bucket for its descendants.
const

Properties

bucket PageStorageBucket
The page storage bucket to use for this subtree.
final
child Widget
The widget below this widget in the tree.
final
hashCode int
The hash code for this object.
@nonVirtual, read-only, inherited
key Key?
Controls how one widget replaces another widget in the tree.
final, inherited
runtimeType Type
A representation of the runtime type of the object.
read-only, inherited

Methods

build(BuildContext context) Widget
Describes the part of the user interface represented by this widget.
override
createElement() StatelessElement
Creates a StatelessElement to manage this widget's location in the tree.
inherited
debugDescribeChildren() List<DiagnosticsNode>
Returns a list of DiagnosticsNode objects describing this node's children.
@protected, inherited
debugFillProperties(DiagnosticPropertiesBuilder properties) → void
Add additional properties associated with the node.
inherited
noSuchMethod(Invocation invocation) → dynamic
Invoked when a non-existent 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

Operators

operator ==(Object other) bool
The equality operator.
@nonVirtual, inherited

Static Methods

of(BuildContext context) PageStorageBucket?
The bucket from the closest instance of this class that encloses the given context.