Skip to content

Commit 27df0d8

Browse files
committed
更新了 provider example
1 parent 42e646e commit 27df0d8

File tree

5 files changed

+125
-142
lines changed

5 files changed

+125
-142
lines changed

image/provider_example.gif

486 KB
Loading

mecury_project/example/provider_example/README.md

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,12 @@
33
## 简介
44
使用 Google 官方推荐的状态管理 [Provider](https://pub.dev/packages/provider#-installing-tab-) 的 demo。详细了解请看这里:[Flutter | 状态管理指南篇——Provider](https://juejin.im/editor/posts/5d00a84fe51d455a2f22023f)
55

6+
7+
8+
[更新]:Demo 新增单页面状态提供方式,供参考
9+
610
## 样例
7-
![](https://user-gold-cdn.xitu.io/2019/6/12/16b4c691828e5a7a?w=333&h=676&f=gif&s=235209)
11+
![provider_example](../../../image/provider_example.gif)
812

913
## Getting Started
1014

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
import 'package:flutter/material.dart';
2+
3+
class ColorModel with ChangeNotifier {
4+
int _seed = 0xFFFF9000;
5+
Color _color = Color(0xFFFF9000);
6+
7+
get color => _color;
8+
9+
changeColor() {
10+
_seed += 30;
11+
_color = Color(_seed);
12+
notifyListeners();
13+
}
14+
}

mecury_project/example/provider_example/lib/generated/i18n.dart

Lines changed: 0 additions & 122 deletions
This file was deleted.
Lines changed: 106 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,14 @@
11
import 'package:flutter/material.dart';
22
import 'package:provider/provider.dart';
33
import 'counter_model.dart';
4+
import 'color_model.dart';
45

56
class FirstScreen extends StatelessWidget {
67
@override
78
Widget build(BuildContext context) {
8-
final counter = Provider.of<CounterModel>(context,);
9+
final counter = Provider.of<CounterModel>(
10+
context,
11+
);
912
final textSize = Provider.of<int>(context);
1013

1114
return Scaffold(
@@ -29,32 +32,116 @@ class FirstScreen extends StatelessWidget {
2932
}
3033
}
3134

35+
/// This is a single page provider,
36+
/// it will be destroyed when the page is removed,
37+
/// and the status will be reset.
38+
39+
class SecondScreenProvider extends StatefulWidget {
40+
final Widget child;
41+
42+
SecondScreenProvider({@required this.child});
43+
44+
@override
45+
_SecondScreenProviderState createState() => _SecondScreenProviderState();
46+
}
47+
48+
class _SecondScreenProviderState extends State<SecondScreenProvider> {
49+
ColorModel _colorModel = ColorModel();
50+
51+
@override
52+
Widget build(BuildContext context) {
53+
return ChangeNotifierProvider.value(
54+
value: _colorModel,
55+
child: widget.child,
56+
);
57+
}
58+
}
59+
3260
class SecondScreen extends StatelessWidget {
3361
@override
3462
Widget build(BuildContext context) {
35-
return Scaffold(
36-
appBar: AppBar(
37-
title: Text('Second Page'),
63+
return SecondScreenProvider(
64+
child: DefaultTabController(
65+
length: 3,
66+
child: Scaffold(
67+
appBar: buildAppBar(),
68+
body: TabBarView(children: [
69+
buildCounterText(),
70+
showIconWithColor(),
71+
showIconWithColor(),
72+
]),
73+
floatingActionButton: buildActionButtons(),
74+
),
3875
),
39-
body: Consumer2<CounterModel, int>(
40-
builder: (context, CounterModel counter, int textSize, child) {
41-
return Center(
42-
child: Text(
43-
'Value: ${counter.value}',
44-
style: TextStyle(
45-
fontSize: textSize.toDouble(),
46-
),
76+
);
77+
}
78+
79+
Column buildActionButtons() {
80+
return Column(
81+
mainAxisAlignment: MainAxisAlignment.end,
82+
children: <Widget>[
83+
Consumer<ColorModel>(
84+
builder: (context, ColorModel colorModel, child) {
85+
return FloatingActionButton(
86+
heroTag: 'no_hero',
87+
child: Icon(Icons.swap_horizontal_circle),
88+
onPressed: () {
89+
colorModel.changeColor();
90+
},
91+
);
92+
},
93+
),
94+
SizedBox(height: 20),
95+
Consumer<CounterModel>(
96+
builder: (context, CounterModel counter, child) =>
97+
FloatingActionButton(
98+
onPressed: () => counter.increment(),
99+
child: child,
100+
),
101+
child: Icon(Icons.add),
102+
),
103+
],
104+
);
105+
}
106+
107+
Consumer2<CounterModel, int> buildCounterText() {
108+
return Consumer2<CounterModel, int>(
109+
builder: (context, CounterModel counter, int textSize, child) {
110+
return Center(
111+
child: Text(
112+
'Value: ${counter.value}',
113+
style: TextStyle(
114+
fontSize: textSize.toDouble(),
47115
),
116+
),
117+
);
118+
},
119+
);
120+
}
121+
122+
AppBar buildAppBar() {
123+
return AppBar(
124+
title: Text('Second Page'),
125+
bottom: TabBar(tabs: [
126+
Tab(text: 'show counter'),
127+
Tab(text: 'show color'),
128+
Tab(text: 'show color'),
129+
]),
130+
);
131+
}
132+
133+
Container showIconWithColor() {
134+
return Container(
135+
alignment: Alignment.center,
136+
child: Consumer<ColorModel>(
137+
builder: (context, ColorModel colorModel, child) {
138+
return Icon(
139+
Icons.stars,
140+
color: colorModel.color,
141+
size: 100,
48142
);
49143
},
50144
),
51-
floatingActionButton: Consumer<CounterModel>(
52-
builder: (context, CounterModel counter, child) => FloatingActionButton(
53-
onPressed: () => counter.increment(),
54-
child: child,
55-
),
56-
child: Icon(Icons.add),
57-
),
58145
);
59146
}
60147
}

0 commit comments

Comments
 (0)