| 1 | import 'package:flutter/material.dart'; |
| 2 | import 'package:flutter/services.dart'; |
| 3 | import 'package:flutter_dynamic_icon_plus/flutter_dynamic_icon_plus.dart'; |
| 4 | import 'package:aero/constants/main.dart'; |
| 5 | import 'package:shared_preferences/shared_preferences.dart'; |
| 6 | |
| 7 | class AppIconScreen extends StatefulWidget |
| 8 | const AppIconScreen({super |
| 9 | |
| 10 | @override |
| 11 | State<AppIconScreen> createState |
| 12 | } |
| 13 | |
| 14 | class _AppIconScreenState extends State |
| 15 | String? _selectedIcon; |
| 16 | bool _loading = false; |
| 17 | |
| 18 | final List<Map |
| 19 | {'id': 'ZERO_TWO', 'name': |
| 20 | {'id': 'ZERO_THREE', 'name': |
| 21 | {'id': 'ZERO_FOUR', 'name': |
| 22 | {'id': 'DEFAULT', 'name': |
| 23 | {'id': 'ZERO_FIVE', 'name': |
| 24 | {'id': 'ZERO_SIX', 'name': |
| 25 | {'id': 'ONE_ONE', 'name': |
| 26 | {'id': 'ONE_TWO', 'name': |
| 27 | {'id': 'ONE_THREE', 'name': |
| 28 | {'id': 'ONE_FOUR', 'name': |
| 29 | {'id': 'ONE_FIVE', 'name': |
| 30 | {'id': 'ONE_SIX', 'name': |
| 31 | ]; |
| 32 | |
| 33 | @override |
| 34 | void initState() { |
| 35 | super.initState(); |
| 36 | _loadSelectedIcon(); |
| 37 | } |
| 38 | |
| 39 | Future<void> _loadSelectedIcon |
| 40 | final prefs = await SharedPreferences |
| 41 | final selected = prefs.getString |
| 42 | setState(() { |
| 43 | _selectedIcon = selected; |
| 44 | }); |
| 45 | } |
| 46 | |
| 47 | Future<void> _changeIcon |
| 48 | if (_loading) return; |
| 49 | |
| 50 | setState(() { |
| 51 | _loading = true; |
| 52 | }); |
| 53 | |
| 54 | try { |
| 55 | // Change the app icon using VariableAppIcon |
| 56 | final iconName = iconId == 'DEFAULT' |
| 57 | if (await FlutterDynamicIconPlus.supportsAlternateIcons) { |
| 58 | final channel = MethodChannel( |
| 59 | await channel.invokeMethod("changeAppIcon", |
| 60 | print("changed icon to $iconName" |
| 61 | } |
| 62 | |
| 63 | // Save the selection |
| 64 | final prefs = await SharedPreferences |
| 65 | await prefs.setString('selected_app_icon', |
| 66 | |
| 67 | setState(() { |
| 68 | _selectedIcon = iconId; |
| 69 | }); |
| 70 | |
| 71 | if (mounted) { |
| 72 | ScaffoldMessenger.of(context).showSnackBar |
| 73 | const SnackBar( |
| 74 | content: Text('App icon changed successfully'), |
| 75 | duration: Duration(seconds: 2 |
| 76 | ), |
| 77 | ); |
| 78 | } |
| 79 | } catch (e) { |
| 80 | if (mounted) { |
| 81 | ScaffoldMessenger.of(context).showSnackBar |
| 82 | SnackBar( |
| 83 | content: Text('Failed to change app icon: $e |
| 84 | duration: const Duration(seconds |
| 85 | ), |
| 86 | ); |
| 87 | } |
| 88 | } finally { |
| 89 | if (mounted) { |
| 90 | setState(() { |
| 91 | _loading = false; |
| 92 | }); |
| 93 | } |
| 94 | } |
| 95 | } |
| 96 | |
| 97 | @override |
| 98 | Widget build(BuildContext |
| 99 | return Scaffold( |
| 100 | backgroundColor: Colors.white, |
| 101 | appBar: AppBar( |
| 102 | title: const Text( |
| 103 | 'App Icon', |
| 104 | style: TextStyle( |
| 105 | fontFamily: "CalSans", |
| 106 | fontSize: 20, |
| 107 | fontWeight: FontWeight.bold, |
| 108 | color: Colors.black, |
| 109 | ), |
| 110 | ), |
| 111 | centerTitle: false, |
| 112 | backgroundColor: Colors.white, |
| 113 | elevation: 0, |
| 114 | scrolledUnderElevation: 0, |
| 115 | ), |
| 116 | body: SafeArea( |
| 117 | child: _loading |
| 118 | ? const Center( |
| 119 | child: CircularProgressIndicator( |
| 120 | color: Colors.black, |
| 121 | ), |
| 122 | ) |
| 123 | : SingleChildScrollView( |
| 124 | padding: const EdgeInsets. |
| 125 | child: Column( |
| 126 | crossAxisAlignment: CrossAxisAlignment.start, |
| 127 | children: [ |
| 128 | Text( |
| 129 | 'Select an app icon', |
| 130 | style: TextStyle( |
| 131 | fontSize: 14, |
| 132 | color: Colors.grey.shade600 |
| 133 | fontFamily: "DM Sans", |
| 134 | ), |
| 135 | ), |
| 136 | const SizedBox(height: |
| 137 | GridView.builder( |
| 138 | shrinkWrap: true, |
| 139 | physics: const NeverScrollableScrollPhysics() |
| 140 | gridDelegate: |
| 141 | const SliverGridDelegateWithFixedCrossAxisCount( |
| 142 | crossAxisCount: 3, |
| 143 | crossAxisSpacing: 16, |
| 144 | mainAxisSpacing: 16, |
| 145 | childAspectRatio: 1, |
| 146 | ), |
| 147 | itemCount: _icons.length, |
| 148 | itemBuilder: (context, index) { |
| 149 | final icon = _icons[index]; |
| 150 | final isSelected = _selectedIcon == icon['id' |
| 151 | |
| 152 | return InkWell( |
| 153 | onTap: () => _changeIcon(icon['id' |
| 154 | borderRadius: BorderRadius.circular( |
| 155 | child: Container( |
| 156 | decoration: BoxDecoration( |
| 157 | border: Border.all( |
| 158 | color: isSelected |
| 159 | ? Colors.black |
| 160 | : Colors.grey.shade300 |
| 161 | width: isSelected ? 2 : 1 |
| 162 | ), |
| 163 | borderRadius: BorderRadius.circular( |
| 164 | ), |
| 165 | child: Stack( |
| 166 | children: [ |
| 167 | Center( |
| 168 | child: Padding( |
| 169 | padding: const EdgeInsets. |
| 170 | child: Image.asset( |
| 171 | icon['asset']!, |
| 172 | fit: BoxFit.contain, |
| 173 | ), |
| 174 | ), |
| 175 | ), |
| 176 | if (isSelected) |
| 177 | Positioned( |
| 178 | top: 8, |
| 179 | right: 8, |
| 180 | child: Container( |
| 181 | padding: const EdgeInsets. |
| 182 | decoration: const BoxDecoration( |
| 183 | color: Colors.black, |
| 184 | shape: BoxShape.circle, |
| 185 | ), |
| 186 | child: const Icon( |
| 187 | Icons.check, |
| 188 | color: Colors.white, |
| 189 | size: 16, |
| 190 | ), |
| 191 | ), |
| 192 | ), |
| 193 | ], |
| 194 | ), |
| 195 | ), |
| 196 | ); |
| 197 | }, |
| 198 | ), |
| 199 | ], |
| 200 | ), |
| 201 | ), |
| 202 | ), |
| 203 | ); |
| 204 | } |
| 205 | } |