uaestats_fe/lib/config/theme/themeDialog.dart

85 lines
2.4 KiB
Dart
Executable File

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<ThemeSelectorDialog> createState() => _ThemeSelectorDialogState();
}
class _ThemeSelectorDialogState extends State<ThemeSelectorDialog> {
AppTheme? _selectedTheme;
@override
void initState() {
super.initState();
_loadTheme();
}
Future<void> _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) {
return AlertDialog(
title: Text(
_tr(en: 'Choose Theme', ar: 'اختر المظهر'),
),
content: Column(
mainAxisSize: MainAxisSize.min,
children: AppTheme.values.map((theme) {
return RadioListTile<AppTheme>(
title: Text(_themeLabel(theme)),
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: [
TextButton(
onPressed: () => Navigator.of(context).pop(null),
child: Text(_tr(en: 'Cancel', ar: 'إلغاء')),
),
ElevatedButton(
onPressed: () => Navigator.of(context).pop(_selectedTheme),
child: Text(_tr(en: 'OK', ar: 'حسناً')),
),
],
);
}
}