-
Notifications
You must be signed in to change notification settings - Fork 0
/
sub_changenotifier_provider.dart
232 lines (224 loc) · 7.91 KB
/
sub_changenotifier_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
import 'package:flutter/widgets.dart';
import 'package:flutter_sub/developer.dart';
import 'package:flutter_sub/flutter_sub.dart';
import 'package:flutter_sub_provider/src/sub_listenable_provider.dart';
import 'package:flutter_sub_provider/src/types.dart';
import 'package:provider/provider.dart';
/// {@macro subprovider.subchangenotifierprovider}
class SubChangeNotifierProvider0<R extends ChangeNotifier>
extends SubListenableProvider0<R> {
/// {@macro subprovider.subchangenotifierprovider}
SubChangeNotifierProvider0({
required super.create,
super.update,
super.keys,
super.builder,
super.child,
}) : super(
dispose: (context, value) => value.dispose(),
);
@override
Widget buildWithValue(BuildContext context, R value, Widget? child) {
return ChangeNotifierProvider.value(
value: value,
builder: builder,
child: child,
);
}
}
/// {@template subprovider.subchangenotifierprovider}
/// Manages a ChangeNotifier and exposes it to its descendants.
/// Allows for smart Provider-Provider dependencies.
///
/// This is a combination of a [SubValue] and a [ChangeNotifierProvider].
/// Unlike a [ChangeNotifierProxyProvider] where dependencies are sideloaded, this Widget completely recreates the [ChangeNotifier] when dependencies change.
///
/// An example of a [ChangeNotifierProxyProvider]
///
/// ```dart
/// ChangeNotifierProxyProvider<MyModel, MyChangeNotifier>(
/// create: (_) => MyChangeNotifier(),
/// update: (_, myModel, myNotifier) => myNotifier
/// ..update(myModel),
/// child: /* ... */,
/// );
/// ```
///
/// looks like this as a [SubChangeNotifierProvider]:
///
/// ```dart
/// SubChangeNotifierProvider<MyModel, MyChangeNotifier>(
/// create: (_, myModel) => MyChangeNotifier(myModel: myModel),
/// child: /* ... */,
/// );
/// ```
///
/// The notable difference being, that myModel can now once again be passed through the constructor,
/// and does not have to be sideloaded. MyChangeNotifier is automatically recreated whenever myModel changes.
///
/// All Providers this Widget depends on are keys of the underlying [SubValue].
///
/// Just like [Provider]s, [SubChangeNotifierProvider]s can be used inside of a [MultiProvider].
/// {@endtemplate}
class SubChangeNotifierProvider<T, R extends ChangeNotifier>
extends SubChangeNotifierProvider0<R> {
/// Manages a ChangeNotifier and exposes it to its descendants.
SubChangeNotifierProvider({
required SubProviderCreate<T, R> create,
SubProviderUpdate<T, R>? update,
SubValueBuilderKeys? keys,
super.builder,
super.child,
}) : super(
create: (context) => create(
context,
Provider.of<T>(context),
),
update: update != null
? (context, previous) => update(
context,
Provider.of<T>(context),
previous,
)
: null,
keys: (context) => [Provider.of<T>(context), ...?keys?.call(context)],
);
}
/// {@macro subprovider.subchangenotifierprovider}
class SubChangeNotifierProvider2<T, T2, R extends ChangeNotifier>
extends SubChangeNotifierProvider<T, R> {
/// Manages a ChangeNotifier and exposes it to its descendants.
SubChangeNotifierProvider2({
required SubProviderCreate2<T, T2, R> create,
SubProviderUpdate2<T, T2, R>? update,
SubValueBuilderKeys? keys,
super.builder,
super.child,
}) : super(
create: (context, value) => create(
context,
value,
Provider.of<T2>(context),
),
update: update != null
? (context, value, previous) =>
update(context, value, Provider.of<T2>(context), previous)
: null,
keys: (context) =>
[Provider.of<T2>(context), ...?keys?.call(context)],
);
}
/// {@macro subprovider.subchangenotifierprovider}
class SubChangeNotifierProvider3<T, T2, T3, R extends ChangeNotifier>
extends SubChangeNotifierProvider2<T, T2, R> {
/// Manages a ChangeNotifier and exposes it to its descendants.
SubChangeNotifierProvider3({
required SubProviderCreate3<T, T2, T3, R> create,
SubProviderUpdate3<T, T2, T3, R>? update,
SubValueBuilderKeys? keys,
super.builder,
super.child,
}) : super(
create: (context, value, value2) => create(
context,
value,
value2,
Provider.of<T3>(context),
),
update: update != null
? (context, value, value2, previous) => update(
context, value, value2, Provider.of<T3>(context), previous)
: null,
keys: (context) =>
[Provider.of<T3>(context), ...?keys?.call(context)],
);
}
/// {@macro subprovider.subchangenotifierprovider}
class SubChangeNotifierProvider4<T, T2, T3, T4, R extends ChangeNotifier>
extends SubChangeNotifierProvider3<T, T2, T3, R> {
/// Manages a ChangeNotifier and exposes it to its descendants.
SubChangeNotifierProvider4({
required SubProviderCreate4<T, T2, T3, T4, R> create,
SubProviderUpdate4<T, T2, T3, T4, R>? update,
SubValueBuilderKeys? keys,
super.builder,
super.child,
}) : super(
create: (context, value, value2, value3) => create(
context,
value,
value2,
value3,
Provider.of<T4>(context),
),
update: update != null
? (context, value, value2, value3, previous) => update(context,
value, value2, value3, Provider.of<T4>(context), previous)
: null,
keys: (context) =>
[Provider.of<T4>(context), ...?keys?.call(context)],
);
}
/// {@macro subprovider.subchangenotifierprovider}
class SubChangeNotifierProvider5<T, T2, T3, T4, T5, R extends ChangeNotifier>
extends SubChangeNotifierProvider4<T, T2, T3, T4, R> {
/// Manages a ChangeNotifier and exposes it to its descendants.
SubChangeNotifierProvider5({
required SubProviderCreate5<T, T2, T3, T4, T5, R> create,
SubProviderUpdate5<T, T2, T3, T4, T5, R>? update,
SubValueBuilderKeys? keys,
super.builder,
super.child,
}) : super(
create: (context, value, value2, value3, value4) => create(
context,
value,
value2,
value3,
value4,
Provider.of<T5>(context),
),
update: update != null
? (context, value, value2, value3, value4, previous) => update(
context,
value,
value2,
value3,
value4,
Provider.of<T5>(context),
previous)
: null,
keys: (context) =>
[Provider.of<T5>(context), ...?keys?.call(context)],
);
}
/// {@macro subprovider.subchangenotifierprovider}
class SubChangeNotifierProvider6<T, T2, T3, T4, T5, T6,
R extends ChangeNotifier>
extends SubChangeNotifierProvider5<T, T2, T3, T4, T5, R> {
/// Manages a ChangeNotifier and exposes it to its descendants.
SubChangeNotifierProvider6({
required SubProviderCreate6<T, T2, T3, T4, T5, T6, R> create,
SubProviderUpdate6<T, T2, T3, T4, T5, T6, R>? update,
SubValueBuilderKeys? keys,
super.builder,
super.child,
}) : super(
create: (context, value, value2, value3, value4, value5) => create(
context,
value,
value2,
value3,
value4,
value5,
Provider.of<T6>(context),
),
update: update != null
? (context, value, value2, value3, value4, value5, previous) =>
update(context, value, value2, value3, value4, value5,
Provider.of<T6>(context), previous)
: null,
keys: (context) =>
[Provider.of<T6>(context), ...?keys?.call(context)],
);
}