PageStorage class

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), to save all of their positions independently, one must give each of them unique PageStorageKeys, or set the keepScrollOffset property of some such widgets to 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:



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


bucket PageStorageBucket
The page storage bucket to use for this subtree.
child Widget
The widget below this widget in the tree.
hashCode int
The hash code for this object.
no setterinherited
key Key?
Controls how one widget replaces another widget in the tree.
runtimeType Type
A representation of the runtime type of the object.
no setterinherited


build(BuildContext context) Widget
Describes the part of the user interface represented by this widget.
createElement() StatelessElement
Creates a StatelessElement to manage this widget's location in the tree.
debugDescribeChildren() List<DiagnosticsNode>
Returns a list of DiagnosticsNode objects describing this node's children.
debugFillProperties(DiagnosticPropertiesBuilder properties) → void
Add additional properties associated with the node.
noSuchMethod(Invocation invocation) → dynamic
Invoked when a nonexistent method or property is accessed.
toDiagnosticsNode({String? name, DiagnosticsTreeStyle? style}) DiagnosticsNode
Returns a debug representation of the object that is used by debugging tools and by DiagnosticsNode.toStringDeep.
toString({DiagnosticLevel minLevel =}) String
A string representation of this object.
toStringDeep({String prefixLineOne = '', String? prefixOtherLines, DiagnosticLevel minLevel = DiagnosticLevel.debug}) String
Returns a string representation of this node and its descendants.
toStringShallow({String joiner = ', ', DiagnosticLevel minLevel = DiagnosticLevel.debug}) String
Returns a one-line detailed description of the object.
toStringShort() String
A short, textual description of this widget.


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

Static Methods

maybeOf(BuildContext context) PageStorageBucket?
The PageStorageBucket from the closest instance of a PageStorage widget that encloses the given context.
of(BuildContext context) PageStorageBucket
The PageStorageBucket from the closest instance of a PageStorage widget that encloses the given context.