Skip to content

Commit 72e6215

Browse files
committed
example
1 parent e9f6a07 commit 72e6215

File tree

1 file changed

+48
-45
lines changed

1 file changed

+48
-45
lines changed

example/lib/main.dart

Lines changed: 48 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -9,76 +9,79 @@ final SharedValue<int> counter = SharedValue(
99
);
1010

1111
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+
);
1323
}
1424

1525
class MyApp extends StatelessWidget {
1626
@override
1727
Widget build(BuildContext context) {
28+
print("MyApp.build()");
29+
1830
return MaterialApp(
1931
title: 'Flutter Demo',
2032
theme: ThemeData(
2133
primarySwatch: Colors.blue,
2234
),
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(),
2551
),
2652
);
2753
}
2854
}
2955

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 {
4057
@override
4158
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
4362
int counterValue = counter.of(context);
44-
counter.value = counterValue;
4563

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,
6967
);
7068
}
69+
}
7170

71+
class CounterButton extends StatelessWidget {
7272
@override
73-
void initState() {
74-
super.initState();
73+
Widget build(BuildContext context) {
74+
print("Button.build()");
7575

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+
);
7881
}
7982

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;
8386
}
8487
}

0 commit comments

Comments
 (0)