-
Notifications
You must be signed in to change notification settings - Fork 3
/
preferences_screen.dart
50 lines (45 loc) · 1.43 KB
/
preferences_screen.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
import 'package:flutter/material.dart';
import 'package:local_storage/shared_preferences/counter_preferences.dart';
class PreferencesScreen extends StatefulWidget {
@override
_PreferencesScreenState createState() => _PreferencesScreenState();
}
class _PreferencesScreenState extends State<PreferencesScreen> {
Future<CounterPreferences> _preferences = CounterPreferences.getInstance();
Future<int> _counter;
@override
void initState() {
_counter = _preferences.then((pref) => pref.getCount());
super.initState();
}
void _incrementCounter() async {
_counter = _preferences.then((pref) => pref.updateCount());
setState(() {});
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('Preferences Example'),
),
body: Center(
child: FutureBuilder<int>(
future: _counter,
builder: (context, snapshot) {
if (snapshot.hasError)
return Text('Error: ${snapshot.error}');
else
return Text(
'Button tapped ${snapshot.data} time${snapshot.data == 1 ? '' : 's'}.\n\n'
'This should persist across restarts.',
);
}),
),
floatingActionButton: FloatingActionButton(
onPressed: _incrementCounter,
tooltip: 'Increment',
child: Icon(Icons.add), //Change Icon
),
);
}
}