Skip to content

Commit c2e21b1

Browse files
gnpricechrisbobbe
authored andcommitted
store [nfc]: Document store widgets and their of static methods
1 parent 6cb7cd4 commit c2e21b1

File tree

1 file changed

+72
-0
lines changed

1 file changed

+72
-0
lines changed

lib/widgets/store.dart

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,41 @@ import 'package:flutter/material.dart';
22

33
import '../model/store.dart';
44

5+
/// Provides access to the app's data.
6+
///
7+
/// There should be one of this widget, near the root of the tree.
8+
///
9+
/// See also:
10+
/// * [GlobalStoreWidget.of], to get access to the data.
11+
/// * [PerAccountStoreWidget], for the user's data associated with a
12+
/// particular Zulip account.
513
class GlobalStoreWidget extends StatefulWidget {
614
const GlobalStoreWidget({super.key, required this.child});
715

816
final Widget child;
917

18+
/// The app's global data store.
19+
///
20+
/// The given build context will be registered as a dependency of the
21+
/// store. This means that when the data in the store changes,
22+
/// the element at that build context will be rebuilt.
23+
///
24+
/// This method is typically called near the top of a build method or a
25+
/// [State.didChangeDependencies] method, like so:
26+
/// ```
27+
/// @override
28+
/// Widget build(BuildContext context) {
29+
/// final globalStore = GlobalStoreWidget.of(context);
30+
/// ```
31+
///
32+
/// This method should not be called from a [State.initState] method;
33+
/// use [State.didChangeDependencies] instead. For discussion, see
34+
/// [BuildContext.dependOnInheritedWidgetOfExactType].
35+
///
36+
/// See also:
37+
/// * [InheritedNotifier], which provides the "dependency" mechanism.
38+
/// * [PerAccountStoreWidget.of], for the user's data associated with a
39+
/// particular Zulip account.
1040
static GlobalStore of(BuildContext context) {
1141
final widget = context
1242
.dependOnInheritedWidgetOfExactType<_GlobalStoreInheritedWidget>();
@@ -56,13 +86,55 @@ class _GlobalStoreInheritedWidget extends InheritedNotifier<GlobalStore> {
5686
store != oldWidget.store;
5787
}
5888

89+
/// Provides access to the user's data for a particular Zulip account.
90+
///
91+
/// Widgets that need information that comes from the Zulip server, or need to
92+
/// interact with the Zulip server, should use [PerAccountStoreWidget.of] to get
93+
/// the [PerAccountStore] for the relevant account.
94+
///
95+
/// A page that is all about a single Zulip account (which includes most of
96+
/// the pages in the app) should have one of this widget, near the root of
97+
/// the page's tree. Where the UI shows information from several accounts,
98+
/// this widget can be used to specify the account that each subtree should
99+
/// interact with.
100+
///
101+
/// See also:
102+
/// * [PerAccountStoreWidget.of], to get access to the data.
103+
/// * [GlobalStoreWidget], for the app's data beyond that of a
104+
/// particular account.
59105
class PerAccountStoreWidget extends StatefulWidget {
60106
const PerAccountStoreWidget(
61107
{super.key, required this.accountId, required this.child});
62108

63109
final int accountId;
64110
final Widget child;
65111

112+
/// The user's data for the relevant Zulip account for this widget.
113+
///
114+
/// The data is taken from the closest [PerAccountStoreWidget] that encloses
115+
/// the given context. Throws an error if there is no enclosing
116+
/// [PerAccountStoreWidget].
117+
///
118+
/// The given build context will be registered as a dependency of the
119+
/// returned store. This means that when the data in the store changes,
120+
/// the element at that build context will be rebuilt.
121+
///
122+
/// This method is typically called near the top of a build method or a
123+
/// [State.didChangeDependencies] method, like so:
124+
/// ```
125+
/// @override
126+
/// Widget build(BuildContext context) {
127+
/// final store = PerAccountStoreWidget.of(context);
128+
/// ```
129+
///
130+
/// This method should not be called from a [State.initState] method;
131+
/// use [State.didChangeDependencies] instead. For discussion, see
132+
/// [BuildContext.dependOnInheritedWidgetOfExactType].
133+
///
134+
/// See also:
135+
/// * [InheritedNotifier], which provides the "dependency" mechanism.
136+
/// * [GlobalStoreWidget.of], for the app's data beyond that of a
137+
/// particular account.
66138
static PerAccountStore of(BuildContext context) {
67139
final widget = context
68140
.dependOnInheritedWidgetOfExactType<_PerAccountStoreInheritedWidget>();

0 commit comments

Comments
 (0)