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