|
| 1 | +import 'package:flutter/material.dart'; |
| 2 | +import 'package:shared_preferences/shared_preferences.dart'; |
| 3 | + |
| 4 | +class SettingsScreen extends StatefulWidget { |
| 5 | + const SettingsScreen({super.key}); |
| 6 | + |
| 7 | + @override |
| 8 | + State<SettingsScreen> createState() => _SettingsScreenState(); |
| 9 | +} |
| 10 | + |
| 11 | +class _SettingsScreenState extends State<SettingsScreen> { |
| 12 | + bool _isDarkMode = false; |
| 13 | + final String _themePrefKey = 'isDarkMode'; |
| 14 | + |
| 15 | + @override |
| 16 | + void initState() { |
| 17 | + super.initState(); |
| 18 | + _loadThemePreference(); |
| 19 | + } |
| 20 | + |
| 21 | + // Load the theme preference from shared preferences |
| 22 | + Future<void> _loadThemePreference() async { |
| 23 | + final prefs = await SharedPreferences.getInstance(); |
| 24 | + setState(() { |
| 25 | + _isDarkMode = prefs.getBool(_themePrefKey) ?? |
| 26 | + MediaQuery.platformBrightnessOf(context) == Brightness.dark; |
| 27 | + }); |
| 28 | + } |
| 29 | + |
| 30 | + // Save the theme preference to shared preferences |
| 31 | + Future<void> _saveThemePreference(bool isDark) async { |
| 32 | + final prefs = await SharedPreferences.getInstance(); |
| 33 | + await prefs.setBool(_themePrefKey, isDark); |
| 34 | + } |
| 35 | + |
| 36 | + @override |
| 37 | + Widget build(BuildContext context) { |
| 38 | + return Scaffold( |
| 39 | + appBar: AppBar( |
| 40 | + backgroundColor: Theme.of(context).colorScheme.inversePrimary, |
| 41 | + title: const Text('Settings'), |
| 42 | + ), |
| 43 | + body: Padding( |
| 44 | + padding: const EdgeInsets.all(16.0), |
| 45 | + child: Column( |
| 46 | + crossAxisAlignment: CrossAxisAlignment.start, |
| 47 | + children: [ |
| 48 | + Text( |
| 49 | + 'Appearance', |
| 50 | + style: Theme.of(context).textTheme.titleLarge, |
| 51 | + ), |
| 52 | + const SizedBox(height: 16), |
| 53 | + SwitchListTile( |
| 54 | + title: const Text('Dark Mode'), |
| 55 | + subtitle: const Text('Toggle between light and dark theme'), |
| 56 | + value: _isDarkMode, |
| 57 | + onChanged: (bool value) { |
| 58 | + setState(() { |
| 59 | + _isDarkMode = value; |
| 60 | + }); |
| 61 | + _saveThemePreference(value); |
| 62 | + |
| 63 | + // Show a dialog that requires app restart to apply theme changes |
| 64 | + showDialog( |
| 65 | + context: context, |
| 66 | + builder: (BuildContext context) { |
| 67 | + return AlertDialog( |
| 68 | + title: const Text('Theme Changed'), |
| 69 | + content: const Text( |
| 70 | + 'The app needs to restart to apply theme changes. Please restart the app.', |
| 71 | + ), |
| 72 | + actions: [ |
| 73 | + TextButton( |
| 74 | + onPressed: () { |
| 75 | + Navigator.of(context).pop(); |
| 76 | + }, |
| 77 | + child: const Text('OK'), |
| 78 | + ), |
| 79 | + ], |
| 80 | + ); |
| 81 | + }, |
| 82 | + ); |
| 83 | + }, |
| 84 | + ), |
| 85 | + const Divider(), |
| 86 | + const SizedBox(height: 16), |
| 87 | + Text( |
| 88 | + 'Other Settings', |
| 89 | + style: Theme.of(context).textTheme.titleLarge, |
| 90 | + ), |
| 91 | + // Add more settings options here |
| 92 | + ], |
| 93 | + ), |
| 94 | + ), |
| 95 | + ); |
| 96 | + } |
| 97 | +} |
0 commit comments