1
1
import 'package:flutter/material.dart' ;
2
2
import 'package:provider/provider.dart' ;
3
3
import 'counter_model.dart' ;
4
+ import 'color_model.dart' ;
4
5
5
6
class FirstScreen extends StatelessWidget {
6
7
@override
7
8
Widget build (BuildContext context) {
8
- final counter = Provider .of <CounterModel >(context,);
9
+ final counter = Provider .of <CounterModel >(
10
+ context,
11
+ );
9
12
final textSize = Provider .of <int >(context);
10
13
11
14
return Scaffold (
@@ -29,32 +32,116 @@ class FirstScreen extends StatelessWidget {
29
32
}
30
33
}
31
34
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
+
32
60
class SecondScreen extends StatelessWidget {
33
61
@override
34
62
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
+ ),
38
75
),
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 (),
47
115
),
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 ,
48
142
);
49
143
},
50
144
),
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
- ),
58
145
);
59
146
}
60
147
}
0 commit comments