/
main.dart
183 lines (168 loc) · 5.74 KB
/
main.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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:prefs/prefs.dart';
void main() => runApp(const PrefsDemoApp());
///
class PrefsDemoApp extends StatelessWidget {
///
const PrefsDemoApp({Key? key}) : super(key: key);
@override
Widget build(BuildContext context) =>
const MaterialApp(home: SharedPreferencesDemo());
}
///
class SharedPreferencesDemo extends StatefulWidget {
///
const SharedPreferencesDemo({Key? key}) : super(key: key);
@override
State<StatefulWidget> createState() => _SharedPreferencesDemoState();
}
class _SharedPreferencesDemoState extends State<SharedPreferencesDemo> {
@override
void dispose() {
/// Clean up
Prefs.dispose();
super.dispose();
}
final _formKey = GlobalKey<FormState>();
bool get radioButton => _radioButton ??= Prefs.getBool('boolean', false);
set radioButton(bool radio) => _radioButton = radio;
bool? _radioButton;
@override
Widget build(BuildContext context) => Scaffold(
appBar: AppBar(
title: const Text('Preferences Demo'),
),
body: FutureBuilder<bool>(
future: initPrefs(),
builder: (_, snapshot) {
//
if (snapshot.hasData) {
//
if (snapshot.data != null) {
return form;
} else {
//
return const Text('Failed to startup');
}
} else if (snapshot.hasError) {
//
return Text('${snapshot.error}');
}
// By default, show a loading spinner.
return const Center(child: CircularProgressIndicator());
}),
);
Widget get form => Form(
key: _formKey,
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: <Widget>[
TextFormField(
key: const Key('string'),
initialValue: Prefs.getString('string'),
onSaved: (String? newValue) =>
Prefs.setString('string', newValue),
decoration: const InputDecoration(
labelText: 'Enter one or two words',
),
keyboardType: TextInputType.text,
inputFormatters: <TextInputFormatter>[
FilteringTextInputFormatter.singleLineFormatter,
],
),
TextFormField(
key: const Key('integer'),
initialValue: Prefs.getInt('integer').toString(),
onSaved: (String? newValue) {
if (newValue!.isNotEmpty) {
Prefs.setInt('integer', int.parse(newValue));
}
},
decoration: const InputDecoration(
labelText: 'Enter a integer',
),
keyboardType: const TextInputType.numberWithOptions(),
inputFormatters: <TextInputFormatter>[
FilteringTextInputFormatter.digitsOnly,
],
),
TextFormField(
key: const Key('double'),
initialValue: Prefs.getDouble('double').toString(),
onSaved: (String? newValue) {
if (newValue!.isNotEmpty) {
Prefs.setDouble('double', double.parse(newValue));
}
},
decoration: const InputDecoration(
labelText: 'Enter a Double number',
),
keyboardType:
const TextInputType.numberWithOptions(decimal: true),
// inputFormatters: <TextInputFormatter>[
// FilteringTextInputFormatter.allow(RegExp(r'[0-9]')),
// ],
),
// True - False Radio Button
_RadioButton(this),
Align(
alignment: Alignment.bottomRight,
child: Padding(
padding: const EdgeInsets.all(20),
child: ElevatedButton(
key: const Key('Save'),
onPressed: () {
_formKey.currentState!.save();
Prefs.setBool('boolean', _radioButton);
},
child: const Text('Save'),
),
),
),
]),
);
Future<bool> initPrefs() async {
// Initialize SharedPreferences
await Prefs.init();
return true;
}
}
class _RadioButton extends StatefulWidget {
const _RadioButton(this.state, {Key? key}) : super(key: key);
final _SharedPreferencesDemoState state;
@override
State<StatefulWidget> createState() => _RadioButtonState();
}
class _RadioButtonState extends State<_RadioButton> {
@override
Widget build(BuildContext context) => Row(
mainAxisAlignment: MainAxisAlignment.center,
children: [
Flexible(
child: RadioListTile<bool>(
key: const Key('true'),
value: true,
groupValue: widget.state.radioButton,
onChanged: (newValue) {
widget.state.radioButton = newValue as bool;
setState(() {});
},
title: const Text('True'),
),
),
Flexible(
child: RadioListTile<bool>(
key: const Key('false'),
value: false,
groupValue: widget.state.radioButton,
onChanged: (newValue) {
widget.state.radioButton = newValue as bool;
setState(() {});
},
title: const Text('False'),
),
),
],
);
}