-
Notifications
You must be signed in to change notification settings - Fork 11
/
counter.dart
65 lines (55 loc) · 1.59 KB
/
counter.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
import 'package:flutter/widgets.dart';
import 'package:mobx/mobx.dart';
import 'package:rc_cross_platform/rc_cross_platform.dart';
import 'package:rc_cross_preferences/rc_cross_preferences.dart';
import 'package:stashall/src/stores/app/app.dart';
import 'package:stashall/src/stores/counter/counter.dart';
class Counter extends StatefulWidget {
final AppStore appStore;
final CounterStore counterStore;
final Platform platform;
final Widget child;
Counter({
@required this.child,
@required this.appStore,
@required this.counterStore,
@required this.platform,
});
@override
_CounterState createState() => _CounterState();
}
class _CounterState extends State<Counter> {
RcCrossPreferences rcCrossPreferences;
ReactionDisposer _saveReactionDisposer;
@override
void initState() {
super.initState();
_init();
}
@override
void dispose() {
_saveReactionDisposer();
super.dispose();
}
Future<void> _init() async {
rcCrossPreferences = await RcCrossPreferences.getInstance(path: 'test.db');
await _loadCounter();
await _initReactions();
}
Future<void> _loadCounter() async {
widget.counterStore.counter = rcCrossPreferences.getInt(
'counter',
defaultValue: 0,
);
widget.appStore.finishLoadingCounter();
}
Future<void> _initReactions() async {
_saveReactionDisposer =
reaction((_) => widget.counterStore.counter, (counter) {
rcCrossPreferences.setInt('counter', counter);
});
widget.appStore.finishSaveCounterReaction();
}
@override
Widget build(BuildContext context) => widget.child;
}