今天学习下Flutter中state管理的第三方provider的4.x新版本.
其中,provider: 4.3.2+2, 3.1.0旧版本在文章末尾。

代码结构如图所示:
provider_demo代码结构

下面是各文件的源码.

main.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
import 'package:flutter/material.dart';
import 'package:provider/provider.dart';
import 'package:provider_demo1/tabbar.dart';
import 'package:provider_demo1/provider/color_provider.dart';
import 'package:provider_demo1/provider/counter_provider.dart';

void main() {
var colorProvider = ColorProvider();
var counterProvider = CounterProvider();
Provider.debugCheckInvalidValueType = null;

runApp(MultiProvider(
providers: [
ChangeNotifierProvider.value(value: colorProvider),
ChangeNotifierProvider.value(value: counterProvider),
],
child: MyApp(),
));
}

class MyApp extends StatelessWidget {
// This widget is the root of your application.
@override
Widget build(BuildContext context) {
return Consumer<CounterProvider>(
builder: (context, model, child) {
return MaterialApp(
title: 'Flutter Demo',
theme: ThemeData(
primarySwatch: Colors.blue,
visualDensity: VisualDensity.adaptivePlatformDensity,
),
home: Tabbar(),
);
},
);
}
}


tabbar.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
import 'package:flutter/material.dart';
import 'package:provider_demo1/pages/one_pege.dart';
import 'package:provider_demo1/pages/two_page.dart';

class Tabbar extends StatelessWidget {
@override
Widget build(BuildContext context) {
return new MaterialApp(
debugShowCheckedModeBanner: false, home: new MainPageWidget());
}
}

class MainPageWidget extends StatefulWidget {
@override
State<StatefulWidget> createState() {
return new MainPageState();
}
}

class MainPageState extends State<MainPageWidget> {
int _tabIndex = 0;
var tabImages;
var appBarTitles = ['One', 'Two'];
/*
* 存放二个页面,跟fragmentList一样
*/
var _pageList;

/*
* 根据选择获得对应的normal或是press的icon
*/
Image getTabIcon(int curIndex) {
if (curIndex == _tabIndex) {
return tabImages[curIndex][1];
}
return tabImages[curIndex][0];
}

/*
* 获取bottomTab的颜色和文字
*/
String getTabTitle(int curIndex) {
if (curIndex == _tabIndex) {
return appBarTitles[curIndex];
} else {
return appBarTitles[curIndex];
}
}

/*
* 根据image路径获取图片
*/
Image getTabImage(path) {
return new Image.asset(path, width: 24.0, height: 24.0);
}

void initData() {
/*
* 初始化选中和未选中的icon
*/
tabImages = [
[
getTabImage('images/tab/home.png'),
getTabImage('images/tab/home_select.png')
],
[
getTabImage('images/tab/show.png'),
getTabImage('images/tab/show_select.png')
],
];
/*
* 子界面
*/
_pageList = [
new OnePage(),
new TwoPage(),
];
}

@override
Widget build(BuildContext context) {
//初始化数据
initData();
return Scaffold(
body: _pageList[_tabIndex],
bottomNavigationBar: new BottomNavigationBar(
items: <BottomNavigationBarItem>[
new BottomNavigationBarItem(
icon: getTabIcon(0), label: getTabTitle(0)),
new BottomNavigationBarItem(
icon: getTabIcon(1), label: getTabTitle(1)),
],
type: BottomNavigationBarType.fixed,
//默认选中首页
currentIndex: _tabIndex,
iconSize: 24.0,
//点击事件
onTap: (index) {
setState(() {
_tabIndex = index;
});
},
));
}
}


color_provider.dart

1
2
3
4
5
6
7
8
9
10
11
12
13
import 'package:flutter/material.dart';

class ColorProvider with ChangeNotifier {
Color _color = Colors.red;

Color get color => _color;

changeColor() {
_color = _color == Colors.red ? Colors.blue : Colors.red;
notifyListeners();
}
}

counter_provider.dart

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
import 'package:flutter/material.dart';

class CounterProvider with ChangeNotifier {
int _count = 0;

int get count => _count;

addCounter() {
_count++;
notifyListeners();
}

subCounter() {
_count--;
notifyListeners();
}
}


one_page.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
import 'package:flutter/material.dart';
import 'package:provider/provider.dart';
import 'package:provider_demo1/provider/color_provider.dart';
import 'package:provider_demo1/provider/counter_provider.dart';

class OnePage extends StatelessWidget {
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('One'),
),
body: Consumer<CounterProvider>(
builder: (context, model, child) {
return ListView(
children: [
IconButton(
icon: Icon(Icons.add),
onPressed: () {
model.addCounter();
},
),
Chip(
backgroundColor: Provider.of<ColorProvider>(context).color,
padding: EdgeInsets.all(12.0),
label: Text(
model.count.toString(),
style: TextStyle(fontSize: 30.0),
),
),
IconButton(
icon: Icon(Icons.remove),
onPressed: () {
model.subCounter();
},
),
],
);
},
),
);
}
}


two_page.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
import 'package:flutter/material.dart';
import 'package:provider/provider.dart';
import 'package:provider_demo1/provider/color_provider.dart';
import 'package:provider_demo1/provider/counter_provider.dart';

class TwoPage extends StatelessWidget {
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('Two'),
),
body: Consumer<CounterProvider>(
builder: (context, model, child) {
return ListView(
children: [
IconButton(
icon: Icon(Icons.add),
onPressed: () {
model.addCounter();
},
),
Chip(
backgroundColor: Provider.of<ColorProvider>(context).color,
padding: EdgeInsets.all(12.0),
label: Text(
model.count.toString(),
style: TextStyle(fontSize: 30.0),
),
),
IconButton(
icon: Icon(Icons.remove),
onPressed: () {
model.subCounter();
},
),
],
);
},
),
floatingActionButton: FloatingActionButton(
onPressed: () {
Provider.of<ColorProvider>(context, listen: false).changeColor();
},
child: Icon(
Icons.build,
color: Colors.white,
),
),
);
}
}


效果图:
provider_demo

文章源码

3.1.0版本源码

另外:其他管理库学习

另外:Flutter学习demo