/
main.dart
320 lines (291 loc) · 9.15 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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
import 'dart:async';
import 'dart:io';
import 'dart:math';
import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
import 'package:flutter_secure_storage/flutter_secure_storage.dart';
void main() {
runApp(const MaterialApp(home: ItemsWidget()));
}
class ItemsWidget extends StatefulWidget {
const ItemsWidget({Key? key}) : super(key: key);
@override
ItemsWidgetState createState() => ItemsWidgetState();
}
enum _Actions { deleteAll, isProtectedDataAvailable }
enum _ItemActions { delete, edit, containsKey, read }
class ItemsWidgetState extends State<ItemsWidget> {
final _storage = const FlutterSecureStorage();
final _accountNameController =
TextEditingController(text: 'flutter_secure_storage_service');
List<_SecItem> _items = [];
@override
void initState() {
super.initState();
_accountNameController.addListener(() => _readAll());
_readAll();
}
Future<void> _readAll() async {
final all = await _storage.readAll(
iOptions: _getIOSOptions(),
aOptions: _getAndroidOptions(),
);
setState(() {
_items = all.entries
.map((entry) => _SecItem(entry.key, entry.value))
.toList(growable: false);
});
}
Future<void> _deleteAll() async {
await _storage.deleteAll(
iOptions: _getIOSOptions(),
aOptions: _getAndroidOptions(),
);
_readAll();
}
Future<void> _isProtectedDataAvailable() async {
await _storage.isCupertinoProtectedDataAvailable();
}
Future<void> _addNewItem() async {
final String key = _randomValue();
final String value = _randomValue();
await _storage.write(
key: key,
value: value,
iOptions: _getIOSOptions(),
aOptions: _getAndroidOptions(),
);
_readAll();
}
IOSOptions _getIOSOptions() => IOSOptions(
accountName: _getAccountName(),
);
AndroidOptions _getAndroidOptions() => const AndroidOptions(
encryptedSharedPreferences: true,
// sharedPreferencesName: 'Test2',
// preferencesKeyPrefix: 'Test'
);
String? _getAccountName() =>
_accountNameController.text.isEmpty ? null : _accountNameController.text;
@override
Widget build(BuildContext context) => Scaffold(
appBar: AppBar(
title: const Text('Plugin example app'),
actions: <Widget>[
IconButton(
key: const Key('add_random'),
onPressed: _addNewItem,
icon: const Icon(Icons.add),
),
PopupMenuButton<_Actions>(
key: const Key('popup_menu'),
onSelected: (action) {
switch (action) {
case _Actions.deleteAll:
_deleteAll();
break;
case _Actions.isProtectedDataAvailable:
_isProtectedDataAvailable();
break;
}
},
itemBuilder: (BuildContext context) => <PopupMenuEntry<_Actions>>[
const PopupMenuItem(
key: Key('delete_all'),
value: _Actions.deleteAll,
child: Text('Delete all'),
),
const PopupMenuItem(
key: Key('is_protected_data_available'),
value: _Actions.isProtectedDataAvailable,
child: Text('IsProtectedDataAvailable'),
),
],
),
],
),
body: Column(
children: [
if (!kIsWeb && Platform.isIOS)
Padding(
padding: const EdgeInsets.symmetric(horizontal: 16),
child: TextFormField(
controller: _accountNameController,
decoration:
const InputDecoration(labelText: 'kSecAttrService'),
),
),
Expanded(
child: ListView.builder(
itemCount: _items.length,
itemBuilder: (BuildContext context, int index) => ListTile(
trailing: PopupMenuButton(
key: Key('popup_row_$index'),
onSelected: (_ItemActions action) =>
_performAction(action, _items[index], context),
itemBuilder: (BuildContext context) =>
<PopupMenuEntry<_ItemActions>>[
PopupMenuItem(
value: _ItemActions.delete,
child: Text(
'Delete',
key: Key('delete_row_$index'),
),
),
PopupMenuItem(
value: _ItemActions.edit,
child: Text(
'Edit',
key: Key('edit_row_$index'),
),
),
PopupMenuItem(
value: _ItemActions.containsKey,
child: Text(
'Contains Key',
key: Key('contains_row_$index'),
),
),
PopupMenuItem(
value: _ItemActions.read,
child: Text(
'Read',
key: Key('contains_row_$index'),
),
),
],
),
title: Text(
_items[index].value,
key: Key('title_row_$index'),
),
subtitle: Text(
_items[index].key,
key: Key('subtitle_row_$index'),
),
),
),
),
],
),
);
Future<void> _performAction(
_ItemActions action,
_SecItem item,
BuildContext context,
) async {
switch (action) {
case _ItemActions.delete:
await _storage.delete(
key: item.key,
iOptions: _getIOSOptions(),
aOptions: _getAndroidOptions(),
);
_readAll();
break;
case _ItemActions.edit:
if (!context.mounted) return;
final result = await showDialog<String>(
context: context,
builder: (context) => _EditItemWidget(item.value),
);
if (result != null) {
await _storage.write(
key: item.key,
value: result,
iOptions: _getIOSOptions(),
aOptions: _getAndroidOptions(),
);
_readAll();
}
break;
case _ItemActions.containsKey:
final key = await _displayTextInputDialog(context, item.key);
final result = await _storage.containsKey(key: key);
if (!context.mounted) return;
ScaffoldMessenger.of(context).showSnackBar(
SnackBar(
content: Text('Contains Key: $result, key checked: $key'),
backgroundColor: result ? Colors.green : Colors.red,
),
);
break;
case _ItemActions.read:
final key = await _displayTextInputDialog(context, item.key);
final result =
await _storage.read(key: key, aOptions: _getAndroidOptions());
if (!context.mounted) return;
ScaffoldMessenger.of(context).showSnackBar(
SnackBar(
content: Text('value: $result'),
),
);
break;
}
}
Future<String> _displayTextInputDialog(
BuildContext context,
String key,
) async {
final controller = TextEditingController();
controller.text = key;
await showDialog(
context: context,
builder: (context) {
return AlertDialog(
title: const Text('Check if key exists'),
actions: [
TextButton(
onPressed: () => Navigator.of(context).pop(),
child: const Text('OK'),
),
],
content: TextField(
controller: controller,
),
);
},
);
return controller.text;
}
String _randomValue() {
final rand = Random();
final codeUnits = List.generate(20, (index) {
return rand.nextInt(26) + 65;
});
return String.fromCharCodes(codeUnits);
}
}
class _EditItemWidget extends StatelessWidget {
_EditItemWidget(String text)
: _controller = TextEditingController(text: text);
final TextEditingController _controller;
@override
Widget build(BuildContext context) {
return AlertDialog(
title: const Text('Edit item'),
content: TextField(
key: const Key('title_field'),
controller: _controller,
autofocus: true,
),
actions: <Widget>[
TextButton(
key: const Key('cancel'),
onPressed: () => Navigator.of(context).pop(),
child: const Text('Cancel'),
),
TextButton(
key: const Key('save'),
onPressed: () => Navigator.of(context).pop(_controller.text),
child: const Text('Save'),
),
],
);
}
}
class _SecItem {
_SecItem(this.key, this.value);
final String key;
final String value;
}