Skip to content

Commit e65c37e

Browse files
authored
More PageStorage clarity in the documentation (#131954)
Fixes flutter/flutter#10867
1 parent b29069e commit e65c37e

File tree

8 files changed

+55
-18
lines changed

8 files changed

+55
-18
lines changed

packages/flutter/lib/src/material/expansion_tile.dart

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -184,9 +184,9 @@ class ExpansionTileController {
184184
/// A single-line [ListTile] with an expansion arrow icon that expands or collapses
185185
/// the tile to reveal or hide the [children].
186186
///
187-
/// This widget is typically used with [ListView] to create an
188-
/// "expand / collapse" list entry. When used with scrolling widgets like
189-
/// [ListView], a unique [PageStorageKey] must be specified to enable the
187+
/// This widget is typically used with [ListView] to create an "expand /
188+
/// collapse" list entry. When used with scrolling widgets like [ListView], a
189+
/// unique [PageStorageKey] must be specified as the [key], to enable the
190190
/// [ExpansionTile] to save and restore its expanded state when it is scrolled
191191
/// in and out of view.
192192
///

packages/flutter/lib/src/material/paginated_data_table.dart

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,9 @@ import 'theme.dart';
2828
/// Data is read lazily from a [DataTableSource]. The widget is presented
2929
/// as a [Card].
3030
///
31+
/// If the [key] is a [PageStorageKey], the [initialFirstRowIndex] is persisted
32+
/// to [PageStorage].
33+
///
3134
/// See also:
3235
///
3336
/// * [DataTable], which is not paginated.

packages/flutter/lib/src/widgets/page_storage.dart

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -9,17 +9,19 @@ import 'framework.dart';
99
// Examples can assume:
1010
// late BuildContext context;
1111

12-
/// A key can be used to persist the widget state in storage after
13-
/// the destruction and will be restored when recreated.
12+
/// A [Key] that can be used to persist the widget state in storage after the
13+
/// destruction and will be restored when recreated.
1414
///
15-
/// Each key with its value plus the ancestor chain of other PageStorageKeys need to
16-
/// be unique within the widget's closest ancestor [PageStorage]. To make it possible for a
17-
/// saved value to be found when a widget is recreated, the key's value must
18-
/// not be objects whose identity will change each time the widget is created.
15+
/// Each key with its value plus the ancestor chain of other [PageStorageKey]s
16+
/// need to be unique within the widget's closest ancestor [PageStorage]. To
17+
/// make it possible for a saved value to be found when a widget is recreated,
18+
/// the key's value must not be objects whose identity will change each time the
19+
/// widget is created.
1920
///
2021
/// See also:
2122
///
22-
/// * [PageStorage], which is the closet ancestor for [PageStorageKey].
23+
/// * [PageStorage], which manages the data storage for widgets using
24+
/// [PageStorageKey]s.
2325
class PageStorageKey<T> extends ValueKey<T> {
2426
/// Creates a [ValueKey] that defines where [PageStorage] values will be saved.
2527
const PageStorageKey(super.value);
@@ -136,12 +138,12 @@ class PageStorageBucket {
136138
/// included in routes.
137139
///
138140
/// [PageStorageKey] is used by [Scrollable] if [ScrollController.keepScrollOffset]
139-
/// is enabled to save their [ScrollPosition]s. When more than one
140-
/// scrollable ([ListView], [SingleChildScrollView], [TextField], etc.) appears
141-
/// within the widget's closest ancestor [PageStorage] (such as within the same route),
142-
/// if you want to save all of their positions independently,
143-
/// you should give each of them unique [PageStorageKey]s, or set some of their
144-
/// `keepScrollOffset` false to prevent saving.
141+
/// is enabled to save their [ScrollPosition]s. When more than one scrollable
142+
/// ([ListView], [SingleChildScrollView], [TextField], etc.) appears within the
143+
/// widget's closest ancestor [PageStorage] (such as within the same route), to
144+
/// save all of their positions independently, one must give each of them unique
145+
/// [PageStorageKey]s, or set the `keepScrollOffset` property of some such
146+
/// widgets to false to prevent saving.
145147
///
146148
/// {@tool dartpad}
147149
/// This sample shows how to explicitly use a [PageStorage] to

packages/flutter/lib/src/widgets/page_view.dart

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -612,6 +612,14 @@ const PageScrollPhysics _kPagePhysics = PageScrollPhysics();
612612
/// ** See code in examples/api/lib/widgets/page_view/page_view.0.dart **
613613
/// {@end-tool}
614614
///
615+
/// ## Persisting the scroll position during a session
616+
///
617+
/// Scroll views attempt to persist their scroll position using [PageStorage].
618+
/// For a [PageView], this can be disabled by setting [PageController.keepPage]
619+
/// to false on the [controller]. If it is enabled, using a [PageStorageKey] for
620+
/// the [key] of this widget is recommended to help disambiguate different
621+
/// scroll views from each other.
622+
///
615623
/// See also:
616624
///
617625
/// * [PageController], which controls which page is visible in the view.

packages/flutter/lib/src/widgets/scroll_position.dart

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -178,8 +178,6 @@ enum ScrollPositionAlignmentPolicy {
178178
abstract class ScrollPosition extends ViewportOffset with ScrollMetrics {
179179
/// Creates an object that determines which portion of the content is visible
180180
/// in a scroll view.
181-
///
182-
/// The [physics], [context], and [keepScrollOffset] parameters must not be null.
183181
ScrollPosition({
184182
required this.physics,
185183
required this.context,

packages/flutter/lib/src/widgets/scroll_view.dart

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,16 @@ enum ScrollViewKeyboardDismissBehavior {
6161
/// To control the initial scroll offset of the scroll view, provide a
6262
/// [controller] with its [ScrollController.initialScrollOffset] property set.
6363
///
64+
/// {@template flutter.widgets.ScrollView.PageStorage}
65+
/// ## Persisting the scroll position during a session
66+
///
67+
/// Scroll views attempt to persist their scroll position using [PageStorage].
68+
/// This can be disabled by setting [ScrollController.keepScrollOffset] to false
69+
/// on the [controller]. If it is enabled, using a [PageStorageKey] for the
70+
/// [key] of this widget is recommended to help disambiguate different scroll
71+
/// views from each other.
72+
/// {@endtemplate}
73+
///
6474
/// See also:
6575
///
6676
/// * [ListView], which is a commonly used [ScrollView] that displays a
@@ -619,6 +629,8 @@ abstract class ScrollView extends StatelessWidget {
619629
/// parameter `semanticChildCount`. This should always be the same as the
620630
/// number of widgets wrapped in [IndexedSemantics].
621631
///
632+
/// {@macro flutter.widgets.ScrollView.PageStorage}
633+
///
622634
/// See also:
623635
///
624636
/// * [SliverList], which is a sliver that displays linear list of children.
@@ -1165,6 +1177,8 @@ abstract class BoxScrollView extends ScrollView {
11651177
///
11661178
/// {@macro flutter.widgets.BoxScroll.scrollBehaviour}
11671179
///
1180+
/// {@macro flutter.widgets.ScrollView.PageStorage}
1181+
///
11681182
/// See also:
11691183
///
11701184
/// * [SingleChildScrollView], which is a scrollable widget that has a single
@@ -1810,6 +1824,8 @@ class ListView extends BoxScrollView {
18101824
/// ** See code in examples/api/lib/widgets/scroll_view/list_view.0.dart **
18111825
/// {@end-tool}
18121826
///
1827+
/// {@macro flutter.widgets.ScrollView.PageStorage}
1828+
///
18131829
/// See also:
18141830
///
18151831
/// * [SingleChildScrollView], which is a scrollable widget that has a single

packages/flutter/lib/src/widgets/scrollable.dart

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,14 @@ typedef TwoDimensionalViewportBuilder = Widget Function(BuildContext context, Vi
7272
/// [PageController], which creates a page-oriented scroll position subclass
7373
/// that keeps the same page visible when the [Scrollable] resizes.
7474
///
75+
/// ## Persisting the scroll position during a session
76+
///
77+
/// Scrollables attempt to persist their scroll position using [PageStorage].
78+
/// This can be disabled by setting [ScrollController.keepScrollOffset] to false
79+
/// on the [controller]. If it is enabled, using a [PageStorageKey] for the
80+
/// [key] of this widget (or one of its ancestors, e.g. a [ScrollView]) is
81+
/// recommended to help disambiguate different [Scrollable]s from each other.
82+
///
7583
/// See also:
7684
///
7785
/// * [ListView], which is a commonly used [ScrollView] that displays a

packages/flutter/lib/src/widgets/single_child_scroll_view.dart

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,8 @@ import 'scrollable.dart';
130130
/// ** See code in examples/api/lib/widgets/single_child_scroll_view/single_child_scroll_view.1.dart **
131131
/// {@end-tool}
132132
///
133+
/// {@macro flutter.widgets.ScrollView.PageStorage}
134+
///
133135
/// See also:
134136
///
135137
/// * [ListView], which handles multiple children in a scrolling list.

0 commit comments

Comments
 (0)