import 'package:flutter/material.dart'; import 'package:shared_preferences/shared_preferences.dart'; import 'package:uae_stat/config/theme/app_theme.dart'; class ThemeSelectorDialog extends StatefulWidget { final AppTheme initialTheme; const ThemeSelectorDialog({super.key, required this.initialTheme}); @override State createState() => _ThemeSelectorDialogState(); } class _ThemeSelectorDialogState extends State { AppTheme? _selectedTheme; @override void initState() { super.initState(); _loadTheme(); } Future _loadTheme() async { final prefs = await SharedPreferences.getInstance(); final storedTheme = prefs.getString('ThemeType') ?? widget.initialTheme; setState(() { _selectedTheme = storedTheme == 'system' ? AppTheme.system : widget.initialTheme; }); } String _tr({ required String en, required String ar, }) { final code = Localizations.localeOf(context).languageCode; return code == 'ar' ? ar : en; } String _themeLabel(AppTheme theme) { switch (theme) { case AppTheme.light: return _tr(en: 'Light', ar: 'فاتح'); case AppTheme.dark: return _tr(en: 'Dark', ar: 'داكن'); case AppTheme.system: return _tr(en: 'System', ar: 'النظام'); } } @override Widget build(BuildContext context) { final isDarkTheme = Theme.of(context).brightness == Brightness.dark; const accentColor = Color(0xFFB68A34); return AlertDialog( backgroundColor: isDarkTheme ? Colors.black : Colors.white, title: Text( _tr(en: 'Choose Theme', ar: 'اختر المظهر'), style: TextStyle( color: isDarkTheme ? Colors.white : Colors.black, ), ), content: SizedBox( width: double.maxFinite, child: Column( mainAxisSize: MainAxisSize.min, crossAxisAlignment: CrossAxisAlignment.start, children: AppTheme.values.map((theme) { return RadioListTile( contentPadding: EdgeInsets.zero, controlAffinity: ListTileControlAffinity.leading, activeColor: accentColor, fillColor: WidgetStateProperty.all(accentColor), title: Text( _themeLabel(theme), style: TextStyle( color: isDarkTheme ? Colors.white : Colors.black, ), ), value: theme, groupValue: _selectedTheme, onChanged: (value) async { final prefs = await SharedPreferences.getInstance(); await prefs.setString( 'ThemeType', value.toString().split('.').last); setState(() => _selectedTheme = value!); }); }).toList(), ), ), actions: [ Row( children: [ Expanded( child: TextButton( onPressed: () => Navigator.of(context).pop(null), style: TextButton.styleFrom( shape: RoundedRectangleBorder( borderRadius: BorderRadius.circular(10.0), ), side: const BorderSide( color: accentColor, width: 1, ), ), child: Text( _tr(en: 'Cancel', ar: 'إلغاء'), style: const TextStyle( color: accentColor, fontSize: 16, ), ), ), ), const SizedBox(width: 12), Expanded( child: TextButton( onPressed: () => Navigator.of(context).pop(_selectedTheme), style: TextButton.styleFrom( backgroundColor: accentColor, foregroundColor: Colors.white, shape: RoundedRectangleBorder( borderRadius: BorderRadius.circular(10.0), ), ), child: FittedBox( fit: BoxFit.scaleDown, child: Text( _tr(en: 'OK', ar: 'حسناً'), style: const TextStyle( color: Colors.white, fontSize: 16, ), textAlign: TextAlign.center, ), ), ), ), ], ), ], ); } }