|
| 1 | +import 'package:flutter/material.dart'; |
| 2 | +import 'package:mayr_storage/mayr_storage.dart'; |
| 3 | + |
| 4 | +/// [main.dart] |
| 5 | +void main() async { |
| 6 | + WidgetsFlutterBinding.ensureInitialized(); |
| 7 | + |
| 8 | + // Initialise MayrStorage |
| 9 | + await MayrStorage.init(); |
| 10 | + |
| 11 | + runApp(const MyApp()); |
| 12 | +} |
| 13 | + |
| 14 | +/// [storage.dart] |
| 15 | +class Storage { |
| 16 | + // Normal SharedPreferences storage |
| 17 | + static final normalToken = 'NORMAL_TOKEN'.storage<String>(); |
| 18 | + |
| 19 | + // Secure storage using encrypted shared preferences |
| 20 | + static final secureToken = 'SECURE_TOKEN'.secureStorage<String>(); |
| 21 | + |
| 22 | + // GetBox Storage |
| 23 | + static final boxToken = 'BOX_TOKEN'.boxStorage<String>(); |
| 24 | +} |
| 25 | + |
| 26 | +/// [my_app.dart] |
| 27 | +class MyApp extends StatelessWidget { |
| 28 | + const MyApp({super.key}); |
| 29 | + |
| 30 | + @override |
| 31 | + Widget build(BuildContext context) { |
| 32 | + return MaterialApp( |
| 33 | + home: Scaffold( |
| 34 | + appBar: AppBar(title: const Text('Mayr Storage Example')), |
| 35 | + body: const StorageDemo(), |
| 36 | + ), |
| 37 | + ); |
| 38 | + } |
| 39 | +} |
| 40 | + |
| 41 | +/// [storage_demo.dart] |
| 42 | +class StorageDemo extends StatefulWidget { |
| 43 | + const StorageDemo({super.key}); |
| 44 | + |
| 45 | + @override |
| 46 | + State<StorageDemo> createState() => _StorageDemoState(); |
| 47 | +} |
| 48 | + |
| 49 | +class _StorageDemoState extends State<StorageDemo> { |
| 50 | + String? normalValue; |
| 51 | + String? secureValue; |
| 52 | + String? boxValue; |
| 53 | + |
| 54 | + @override |
| 55 | + void initState() { |
| 56 | + super.initState(); |
| 57 | + loadValues(); |
| 58 | + } |
| 59 | + |
| 60 | + Future<void> loadValues() async { |
| 61 | + normalValue = await Storage.normalToken.read(); |
| 62 | + secureValue = await Storage.secureToken.read(); |
| 63 | + boxValue = Storage.boxToken.read(); |
| 64 | + |
| 65 | + setState(() {}); |
| 66 | + } |
| 67 | + |
| 68 | + Future<void> writeValues() async { |
| 69 | + await Storage.normalToken.write('NormalTokenValue'); |
| 70 | + await Storage.secureToken.write('SecureTokenValue'); |
| 71 | + Storage.boxToken.write('BoxTokenValue'); |
| 72 | + |
| 73 | + await loadValues(); |
| 74 | + } |
| 75 | + |
| 76 | + Future<void> clearValues() async { |
| 77 | + await Storage.normalToken.delete(); |
| 78 | + await Storage.secureToken.delete(); |
| 79 | + Storage.boxToken.delete(); |
| 80 | + |
| 81 | + await loadValues(); |
| 82 | + } |
| 83 | + |
| 84 | + @override |
| 85 | + Widget build(BuildContext context) { |
| 86 | + return Padding( |
| 87 | + padding: const EdgeInsets.all(16.0), |
| 88 | + child: Column( |
| 89 | + mainAxisAlignment: MainAxisAlignment.center, |
| 90 | + children: [ |
| 91 | + Text('Normal Token: $normalValue'), |
| 92 | + Text('Secure Token: $secureValue'), |
| 93 | + Text('Box Token: $boxValue'), |
| 94 | + const SizedBox(height: 20), |
| 95 | + ElevatedButton( |
| 96 | + onPressed: writeValues, |
| 97 | + child: const Text('Write Values'), |
| 98 | + ), |
| 99 | + ElevatedButton( |
| 100 | + onPressed: clearValues, |
| 101 | + child: const Text('Clear Values'), |
| 102 | + ), |
| 103 | + ], |
| 104 | + ), |
| 105 | + ); |
| 106 | + } |
| 107 | +} |
0 commit comments