# 状态管理
TIP
flutter-provide官网状态管理
最简单的示例:
//这个model需要继承ChangeNotifier
class Counter with ChangeNotifier {
int _value;
int get value => _value;
Counter(this._value);
void increment() {
_value++;
notifyListeners();
}
}
// 界面
class CounterApp extends StatelessWidget {
Widget build(BuildContext context) {
final currentCounter = Provide.value<Counter>(context);
return Column(children: [
Provide<Counter>(
builder: (context, child, counter) => Text('${counter.value}'),
),
StreamBuilder<Counter>(
initialData: currentCounter,
stream: Provide.stream<Counter>(context)
.where((counter) => counter.value % 2 == 0),
builder: (context, snapshot) =>
Text('Last even value: ${snapshot.data.value}')),
FlatButton(child: Text('increment'), onPressed: currentCounter.increment),
Text('Another widget that does not depend on the Counter'),
]);
}
}
//定义一个全局的Provide
void main() {
final providers = Providers()
..provide(Provider.function((context) => Counter(0)));
runApp(ProviderNode(
providers: providers,
child: CounterApp(),
));
}
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
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