1
- // Copyright 2018 The Chromium Authors. All rights reserved.
1
+ // Copyright 2019 The Chromium Authors. All rights reserved.
2
2
// Use of this source code is governed by a BSD-style license that can be
3
3
// found in the LICENSE file.
4
4
5
5
import 'package:flutter/material.dart' ;
6
- import 'package:scoped_model/scoped_model .dart' ;
6
+ import 'package:provider/provider .dart' ;
7
7
8
8
void main () => runApp (MyApp ());
9
9
10
- class IncrementValueModel extends Model {
11
- IncrementValueModel ( this ._incrementValue) : super ( );
10
+ class Incrementer with ChangeNotifier {
11
+ Incrementer ([ this ._incrementValue = 1 ] );
12
12
int _incrementValue;
13
13
int _counter = 0 ;
14
14
15
- int get incrementValue => _incrementValue;
16
15
int get counter => _counter;
17
16
18
- void updateIncrementValue (int value) {
17
+ int get incrementValue => _incrementValue;
18
+ set incrementValue (int value) {
19
19
_incrementValue = value;
20
20
notifyListeners ();
21
21
}
@@ -24,22 +24,15 @@ class IncrementValueModel extends Model {
24
24
_counter += _incrementValue;
25
25
notifyListeners ();
26
26
}
27
-
28
- /// Wraps [ModelFinder.of] for this [Model]
29
- static IncrementValueModel of (BuildContext context) =>
30
- ModelFinder <IncrementValueModel >().of (context);
31
27
}
32
28
33
29
class MyApp extends StatelessWidget {
34
30
@override
35
31
Widget build (BuildContext context) {
36
- return ScopedModel (
37
- model : IncrementValueModel ( 1 ),
32
+ return ChangeNotifierProvider (
33
+ notifier : Incrementer ( ),
38
34
child: MaterialApp (
39
35
title: 'Flutter Demo' ,
40
- theme: ThemeData (
41
- primarySwatch: Colors .blue,
42
- ),
43
36
home: MyHomePage (title: 'Modal Bottom Sheet Demo' ),
44
37
),
45
38
);
@@ -63,31 +56,17 @@ class MyHomePage extends StatelessWidget {
63
56
Text (
64
57
'You have pushed the button this many times:' ,
65
58
),
66
- ScopedModelDescendant < IncrementValueModel >(
67
- builder: (context, _, model ) => Text (
68
- '${model .counter }' ,
59
+ Consumer < Incrementer >(
60
+ builder: (context, value ) => Text (
61
+ '${value .counter }' ,
69
62
style: Theme .of (context).textTheme.display1,
70
63
),
71
64
),
72
65
],
73
66
),
74
67
),
75
- // Below is an alternative way of accessing the model in instances
76
- // where the widget doesn't care when the model is updated
77
- // (i.e. the widget doesn't rebuild when the model's data changes).
78
- //
79
- // Another way of doing this is using the rebuildOnChange flag
80
- // e.g.
81
- // floatingActionButton: ScopedModelDescendant<IncrementValueModel>(
82
- // rebuildOnChange: false,
83
- // builder: (context, _, model) => FloatingActionButton(
84
- // onPressed: model.increment,
85
- // tooltip: 'Increment',
86
- // child: Icon(Icons.add),
87
- // ),
88
- // ),
89
68
floatingActionButton: FloatingActionButton (
90
- onPressed: () => IncrementValueModel .of (context).increment (),
69
+ onPressed: () => Provider .of < Incrementer > (context).increment (),
91
70
tooltip: 'Increment' ,
92
71
child: Icon (Icons .add),
93
72
),
@@ -100,38 +79,30 @@ class MyHomePage extends StatelessWidget {
100
79
class BottomBar extends StatelessWidget {
101
80
@override
102
81
Widget build (BuildContext context) {
103
- return ScopedModelDescendant <IncrementValueModel >(
104
- builder: (context, _, model) => BottomAppBar (
105
- color: Theme .of (context).primaryColor,
106
- child: Row (
107
- children: [
108
- Padding (
109
- padding: const EdgeInsets .only (left: 20.0 ),
110
- child: IconButton (
111
- icon: Icon (Icons .settings,
112
- color: Theme .of (context).canvasColor),
113
- onPressed: () => _modalBottomSheet (context),
114
- ),
115
- ),
116
- ],
117
- )),
82
+ return BottomAppBar (
83
+ color: Theme .of (context).primaryColor,
84
+ child: Row (
85
+ children: [
86
+ Padding (
87
+ padding: const EdgeInsets .only (left: 20.0 ),
88
+ child: IconButton (
89
+ icon: Icon (Icons .settings, color: Theme .of (context).canvasColor),
90
+ onPressed: () => showModalBottomSheet (
91
+ context: context,
92
+ builder: (_) => CustomBottomSheet (),
93
+ ),
94
+ ),
95
+ ),
96
+ ],
97
+ ),
118
98
);
119
99
}
120
100
}
121
101
122
- void _modalBottomSheet (BuildContext context) {
123
- showModalBottomSheet (
124
- context: context,
125
- builder: (_) => CustomBottomSheet (),
126
- // maxHeightRatio: 0.4,
127
- );
128
- }
129
-
130
102
class CustomBottomSheet extends StatelessWidget {
131
103
@override
132
104
Widget build (BuildContext context) {
133
105
return Container (
134
- // height: 600.0,
135
106
child: Column (
136
107
mainAxisAlignment: MainAxisAlignment .start,
137
108
crossAxisAlignment: CrossAxisAlignment .stretch,
@@ -144,11 +115,7 @@ class CustomBottomSheet extends StatelessWidget {
144
115
),
145
116
),
146
117
Padding (
147
- padding: const EdgeInsets .only (
148
- top: 50.0 ,
149
- left: 20.0 ,
150
- right: 20.0 ,
151
- ),
118
+ padding: const EdgeInsets .only (top: 50.0 , left: 20.0 , right: 20.0 ),
152
119
child: Column (
153
120
crossAxisAlignment: CrossAxisAlignment .start,
154
121
children: [
@@ -167,24 +134,23 @@ class CustomBottomSheet extends StatelessWidget {
167
134
class CustomSlider extends StatelessWidget {
168
135
@override
169
136
Widget build (BuildContext context) {
170
- return ScopedModelDescendant <IncrementValueModel >(
171
- builder: (context, _, model) {
137
+ return Consumer <Incrementer >(builder: (context, incrementer) {
172
138
return Row (
173
139
children: [
174
140
Expanded (
175
141
child: Slider (
176
- value: model .incrementValue.toDouble (),
142
+ value: incrementer .incrementValue.toDouble (),
177
143
min: 1.0 ,
178
144
max: 10.0 ,
179
145
label: 'Increment Step' ,
180
146
onChanged: (double value) =>
181
- model. updateIncrementValue ( value.toInt () ),
147
+ incrementer.incrementValue = value.toInt (),
182
148
),
183
149
),
184
150
Padding (
185
151
padding: const EdgeInsets .only (left: 10.0 ),
186
- child:
187
- Container ( width: 30.0 , child: Text ('${model .incrementValue }' )),
152
+ child: Container (
153
+ width: 30.0 , child: Text ('${incrementer .incrementValue }' )),
188
154
),
189
155
],
190
156
);
0 commit comments