@@ -9,76 +9,79 @@ final SharedValue<int> counter = SharedValue(
9
9
);
10
10
11
11
Future <void > main () async {
12
- runApp (SharedValue .wrapApp (MyApp ())); // don't forget this!
12
+ WidgetsFlutterBinding .ensureInitialized ();
13
+
14
+ // load previous value from shared prefs
15
+ counter.load ();
16
+
17
+ runApp (
18
+ // don't forget this!
19
+ SharedValue .wrapApp (
20
+ MyApp (),
21
+ ),
22
+ );
13
23
}
14
24
15
25
class MyApp extends StatelessWidget {
16
26
@override
17
27
Widget build (BuildContext context) {
28
+ print ("MyApp.build()" );
29
+
18
30
return MaterialApp (
19
31
title: 'Flutter Demo' ,
20
32
theme: ThemeData (
21
33
primarySwatch: Colors .blue,
22
34
),
23
- home: MyHomePage (
24
- title: 'Flutter Demo Home Page' ,
35
+ home: Scaffold (
36
+ appBar: AppBar (
37
+ title: Text ("Shared value demo" ),
38
+ ),
39
+ body: Center (
40
+ child: Column (
41
+ mainAxisAlignment: MainAxisAlignment .center,
42
+ children: < Widget > [
43
+ Text (
44
+ 'You have pushed the button this many times:' ,
45
+ ),
46
+ CounterText (),
47
+ ],
48
+ ),
49
+ ),
50
+ floatingActionButton: CounterButton (),
25
51
),
26
52
);
27
53
}
28
54
}
29
55
30
- class MyHomePage extends StatefulWidget {
31
- MyHomePage ({Key key, this .title}) : super (key: key);
32
-
33
- final String title;
34
-
35
- @override
36
- _MyHomePageState createState () => _MyHomePageState ();
37
- }
38
-
39
- class _MyHomePageState extends State <MyHomePage > {
56
+ class CounterText extends StatelessWidget {
40
57
@override
41
58
Widget build (BuildContext context) {
42
- // The .of(context) bit makes this widget rebuild everytime counter is changed
59
+ print ("CounterText.build()" );
60
+
61
+ // The .of(context) bit makes this widget rebuild automatically
43
62
int counterValue = counter.of (context);
44
- counter.value = counterValue;
45
63
46
- return Scaffold (
47
- appBar: AppBar (
48
- title: Text (widget.title),
49
- ),
50
- body: Center (
51
- child: Column (
52
- mainAxisAlignment: MainAxisAlignment .center,
53
- children: < Widget > [
54
- Text (
55
- 'You have pushed the button this many times:' ,
56
- ),
57
- Text (
58
- '$counterValue ' ,
59
- style: Theme .of (context).textTheme.display1,
60
- ),
61
- ],
62
- ),
63
- ),
64
- floatingActionButton: FloatingActionButton (
65
- onPressed: _incrementCounter,
66
- tooltip: 'Increment' ,
67
- child: Icon (Icons .add),
68
- ),
64
+ return Text (
65
+ '$counterValue ' ,
66
+ style: Theme .of (context).textTheme.headline4,
69
67
);
70
68
}
69
+ }
71
70
71
+ class CounterButton extends StatelessWidget {
72
72
@override
73
- void initState ( ) {
74
- super . initState ( );
73
+ Widget build ( BuildContext context ) {
74
+ print ( "Button.build()" );
75
75
76
- // load previous value from shared prefs
77
- counter.load ();
76
+ return FloatingActionButton (
77
+ onPressed: _incrementCounter,
78
+ tooltip: 'Increment' ,
79
+ child: Icon (Icons .add),
80
+ );
78
81
}
79
82
80
- Future < void > _incrementCounter () async {
81
- // update counter value and rebuild all widgets using that value
82
- counter.update ((value) => value + 1 ) ;
83
+ void _incrementCounter () {
84
+ // update counter value and rebuild widgets
85
+ counter.$ += 1 ;
83
86
}
84
87
}
0 commit comments