-
-
Notifications
You must be signed in to change notification settings - Fork 500
/
provider.dart
282 lines (255 loc) · 9.32 KB
/
provider.dart
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
import 'package:flutter/foundation.dart';
import 'package:flutter/widgets.dart';
import 'package:flutter_hooks/flutter_hooks.dart';
/// Necessary to obtain generic [Type]
/// see https://stackoverflow.com/questions/52891537/how-to-get-generic-type
/// and https://github.com/dart-lang/sdk/issues/11923
Type _typeOf<T>() => T;
typedef UpdateShouldNotify<T> = bool Function(T previous, T current);
/// A generic implementation of an [InheritedWidget]
///
/// It is possible to customize the behavior of [InheritedWidget.updateShouldNotify]
/// by passing a callback with the desired behavior.
class Provider<T> extends InheritedWidget {
/// The value exposed to other widgets.
///
/// You can obtain this value this widget's descendants
/// using [Provider.of] method
final T value;
// has a different name then the actual method
// so that it can have documentation
/// A callback called whenever [InheritedWidget.updateShouldNotify] is called.
/// It should return `false` when there's no need to update its dependents.
///
/// The default behavior is `previous == current`
final bool Function(T previous, T current) _updateShouldNotify;
/// Creates a [Provider] and pass down [value] to all of its descendants
const Provider({
Key key,
@required this.value,
@required Widget child,
UpdateShouldNotify<T> updateShouldNotify,
}) : _updateShouldNotify = updateShouldNotify,
super(key: key, child: child);
/// Obtain the nearest Provider<T> and returns its value.
///
/// If [listen] is true (default), later value changes will
/// trigger a new [State.build] to widgets, and [State.didChangeDependencies] for [StatefulWidget]
static T of<T>(BuildContext context, {bool listen = true}) {
// this is required to get generic Type
final type = _typeOf<Provider<T>>();
final provider = listen
? context.inheritFromWidgetOfExactType(type) as Provider<T>
: context.ancestorInheritedElementForWidgetOfExactType(type)?.widget
as Provider<T>;
return provider?.value;
}
@override
bool updateShouldNotify(Provider<T> oldWidget) {
if (_updateShouldNotify != null) {
return _updateShouldNotify(oldWidget.value, value);
}
return oldWidget.value != value;
}
@override
void debugFillProperties(DiagnosticPropertiesBuilder properties) {
super.debugFillProperties(properties);
properties.add(DiagnosticsProperty<T>('value', value));
}
}
@visibleForTesting
// ignore: public_member_api_docs
UpdateShouldNotify<T> debugGetProviderUpdateShouldNotify<T>(
Provider<T> provider) =>
provider._updateShouldNotify;
/// Obtain [Provider<T>] from its ancestors and pass its value to [builder].
///
/// [builder] must not be null and may be called multiple times (such as when provided value change).
class Consumer<T> extends StatelessWidget {
/// Build a widget tree based on the value from a [Provider<T>].
///
/// Must not be null.
final Widget Function(BuildContext context, T value) builder;
/// Consumes a [Provider<T>]
Consumer({Key key, @required this.builder})
: assert(builder != null),
super(key: key);
@override
Widget build(BuildContext context) {
return builder(context, Provider.of<T>(context));
}
}
/// A [Provider] that can also create and dispose an object.
///
/// It is usually used to avoid making a [StatefulWidget] for something trivial, such as instanciating a BLoC.
///
/// [StatefulBuilder] is the equivalent of a [State.initState] combined with [State.dispose].
/// As such, [valueBuilder] is called only once and is unable to use [InheritedWidget]; which makes it impossible to update the created value.
///
/// If this is too limiting, consider instead [HookProvider], which offer a much more advanced control over the created value.
///
/// The following example instanciate a `Model` once, and dispose it when [StatefulProvider] is removed from the tree.
///
/// ```
/// class Model {
/// void dispose() {}
/// }
///
/// class Stateless extends StatelessWidget {
/// @override
/// Widget build(BuildContext context) {
/// return StatefulProvider<Model>(
/// valueBuilder: (context) => Model(),
/// dispose: (context, value) => value.dispose(),
/// child: ...,
/// );
/// }
/// }
/// ```
class StatefulProvider<T> extends StatefulWidget {
/// A function that creates the provided value.
///
/// [valueBuilder] must not be null and is called only once for the life-cycle of [StatefulProvider].
///
/// It is not possible to obtain an [InheritedWidget] from [valueBuilder].
final T Function(BuildContext context) valueBuilder;
/// [onDispose] is a callback called when [StatefulProvider] is
/// removed for the widget tree, and pass the current value as parameter.
/// It is useful when the provided object needs to have a custom dipose behavior,
/// such as closing streams.
final void Function(BuildContext context, T value) onDispose;
/// The widget below this widget in the tree.
///
/// {@macro flutter.widgets.child}
final Widget child;
/// A customizable implementation for [InheritedWidget.updateShouldNotify]
final bool Function(T previous, T current) updateShouldNotify;
/// Allows to specify parameters to [StatefulProvider]
StatefulProvider({
Key key,
@required this.valueBuilder,
@required this.child,
this.onDispose,
this.updateShouldNotify,
}) : assert(valueBuilder != null),
super(key: key);
@override
_StatefulProviderState<T> createState() => _StatefulProviderState<T>();
}
class _StatefulProviderState<T> extends State<StatefulProvider<T>> {
T _value;
@override
void initState() {
super.initState();
_value = widget.valueBuilder(context);
}
@override
void dispose() {
if (widget.onDispose != null) {
widget.onDispose(context, _value);
}
super.dispose();
}
@override
Widget build(BuildContext context) {
return Provider(
value: _value,
updateShouldNotify: widget.updateShouldNotify,
child: widget.child,
);
}
}
/// A provider which can use hooks from [flutter_hooks](https://github.com/rrousselGit/flutter_hooks)
///
/// This is especially useful to create complex providers, without having to make a `StatefulWidget`.
///
/// The following example uses BLoC pattern to create a BLoC, provide its value, and dispose it when the provider is removed from the tree.
///
/// ```dart
/// HookProvider<MyBloc>(
/// hook: () {
/// final bloc = useMemoized(() => MyBloc());
/// useEffect(() => bloc.dispose, [bloc]);
/// return bloc;
/// },
/// child: // ...
/// )
/// ```
class HookProvider<T> extends HookWidget {
/// A provider which can use hooks from [flutter_hooks](https://github.com/rrousselGit/flutter_hooks)
///
/// This is especially useful to create complex providers, without having to make a `StatefulWidget`.
///
/// The following example uses BLoC pattern to create a BLoC, provide its value, and dispose it when the provider is removed from the tree.
///
/// ```dart
/// HookProvider<MyBloc>(
/// hook: () {
/// final bloc = useMemoized(() => MyBloc());
/// useEffect(() => bloc.dispose, [bloc]);
/// return bloc;
/// },
/// child: // ...
/// )
/// ```
const HookProvider(
{Key key, this.hook, @required this.child, this.updateShouldNotify})
: super(key: key);
/// A provider which can use hooks from [flutter_hooks](https://github.com/rrousselGit/flutter_hooks)
///
/// This is especially useful to create complex providers, without having to make a `StatefulWidget`.
///
/// The following example uses BLoC pattern to create a BLoC, provide its value, and dispose it when the provider is removed from the tree.
///
/// ```dart
/// HookProvider<MyBloc>(
/// hook: () {
/// final bloc = useMemoized(() => MyBloc());
/// useEffect(() => bloc.dispose, [bloc]);
/// return bloc;
/// },
/// child: // ...
/// )
/// ```
final T Function() hook;
/// A provider which can use hooks from [flutter_hooks](https://github.com/rrousselGit/flutter_hooks)
///
/// This is especially useful to create complex providers, without having to make a `StatefulWidget`.
///
/// The following example uses BLoC pattern to create a BLoC, provide its value, and dispose it when the provider is removed from the tree.
///
/// ```dart
/// HookProvider<MyBloc>(
/// hook: () {
/// final bloc = useMemoized(() => MyBloc());
/// useEffect(() => bloc.dispose, [bloc]);
/// return bloc;
/// },
/// child: // ...
/// )
/// ```
final Widget child;
/// A provider which can use hooks from [flutter_hooks](https://github.com/rrousselGit/flutter_hooks)
///
/// This is especially useful to create complex providers, without having to make a `StatefulWidget`.
///
/// The following example uses BLoC pattern to create a BLoC, provide its value, and dispose it when the provider is removed from the tree.
///
/// ```dart
/// HookProvider<MyBloc>(
/// hook: () {
/// final bloc = useMemoized(() => MyBloc());
/// useEffect(() => bloc.dispose, [bloc]);
/// return bloc;
/// },
/// child: // ...
/// )
/// ```
final bool Function(T, T) updateShouldNotify;
@override
Widget build(BuildContext context) => Provider<T>(
value: hook(),
child: child,
updateShouldNotify: updateShouldNotify,
);
}