今天学习下Flutter中state管理的第三方provider.
其中,provider: 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
import 'package:flutter/material.dart';
import 'package:provider/provider.dart';

import 'bloc/counter_bloc.dart';
import 'bloc/color_bloc.dart';

import './tabbar.dart';

main() {
var counterBloc = CounterBloc();
var colorDataBloc = ColorDataBloc();
runApp(MultiProvider(providers: [
Provider<ColorDataBloc>.value(value: colorDataBloc),
Provider<CounterBloc>.value(value: counterBloc),
], child: MyApp()));
}

class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return StreamBuilder(
builder: (context, snapshot) {
return MaterialApp(
home: Tabbar(),
);
},
initialData: Provider.of<ColorDataBloc>(context).color,
stream: Provider.of<ColorDataBloc>(context).stream,
);
}
}

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 'pages/one.dart';
import 'pages/two.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的颜色和文字
*/
Text getTabTitle(int curIndex) {
if (curIndex == _tabIndex) {
return new Text(appBarTitles[curIndex],
style: new TextStyle(fontSize: 14.0, color: const Color(0xff1296db)));
} else {
return new Text(appBarTitles[curIndex],
style: new TextStyle(fontSize: 14.0, color: const Color(0xff515151)));
}
}

/*
* 根据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 One(),
new Two(),
];
}

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

color_bloc.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
import 'dart:async';
import 'package:flutter/material.dart';

class ColorDataBloc {
StreamController<Color> _streamController;
Stream<Color> _stream;
Color _color;

ColorDataBloc() {
_color = Colors.red;
_streamController = StreamController();
_stream = _streamController.stream;
}

Stream<Color> get stream => _stream;
Color get color => _color;

changeColor() {
_color = _color == Colors.red ? Colors.blue : Colors.red;
_streamController.sink.add(_color);
}

dispose() {
_streamController.close();
}
}

counter_bloc.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
import 'dart:async';

class CounterBloc {
StreamController<int> _streamController;
Stream<int> _stream;
int _count;

CounterBloc() {
_count = 0;
_streamController = StreamController.broadcast();
_stream = _streamController.stream;
}

Stream<int> get stream => _stream;
int get count => _count;

addCounter() {
_streamController.sink.add(++_count);
}
subCounter() {
_streamController.sink.add(--_count);
}
dispose() {
_streamController.close();
}
}

one.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
import 'package:flutter/material.dart';
import 'package:provider/provider.dart';

import '../bloc/counter_bloc.dart';
import '../bloc/color_bloc.dart';

class One extends StatelessWidget {
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('One'),
),
body: Center(
child: StreamBuilder(
builder: (context, snapshot) {
return Column(
mainAxisSize: MainAxisSize.min,
children: <Widget>[
IconButton(
icon: Icon(Icons.add),
onPressed: () {
Provider.of<CounterBloc>(context).addCounter();
}),
Chip(
backgroundColor: Provider.of<ColorDataBloc>(context).color,
padding: EdgeInsets.all(12.0),
label: Text(
Provider.of<CounterBloc>(context).count.toString(),
style: TextStyle(fontSize: 30.0),
),
),
IconButton(
icon: Icon(Icons.remove),
onPressed: () {
Provider.of<CounterBloc>(context).subCounter();
}),
],
);
},
initialData: Provider.of<CounterBloc>(context).count,
stream: Provider.of<CounterBloc>(context).stream,
),
),
floatingActionButton: FloatingActionButton(
onPressed: () {
Provider.of<ColorDataBloc>(context).changeColor();
},
child: Icon(
Icons.build,
color: Colors.white,
),
),
);
}
}

two.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
import 'package:flutter/material.dart';
import 'package:provider/provider.dart';

import '../bloc/counter_bloc.dart';
import '../bloc/color_bloc.dart';

class Two extends StatelessWidget {
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('Two'),
),
body: Center(
child: StreamBuilder(
builder: (context, snapshot) {
return Column(
mainAxisSize: MainAxisSize.min,
children: <Widget>[
IconButton(
icon: Icon(Icons.add),
onPressed: () {
Provider.of<CounterBloc>(context).addCounter();
}),
Chip(
backgroundColor: Provider.of<ColorDataBloc>(context).color,
padding: EdgeInsets.all(12.0),
label: Text(
Provider.of<CounterBloc>(context).count.toString(),
style: TextStyle(fontSize: 30.0),
),
),
IconButton(
icon: Icon(Icons.remove),
onPressed: () {
Provider.of<CounterBloc>(context).subCounter();
}),
],
);
},
initialData: Provider.of<CounterBloc>(context).count,
stream: Provider.of<CounterBloc>(context).stream,
),
),
floatingActionButton: FloatingActionButton(
onPressed: () {
Provider.of<ColorDataBloc>(context).changeColor();
},
child: Icon(
Icons.build,
color: Colors.white,
),
),
);
}
}

效果图:
provider_demo

源码

另外:其他管理库学习

另外:Flutter学习demo