settings_page.dart (2498B)
1 import 'package:firebase_auth/firebase_auth.dart'; 2 import 'package:flutter/material.dart'; 3 import 'package:penny/pages/about_page.dart'; 4 import 'package:penny/pages/username_page.dart'; 5 import 'package:shared_preferences/shared_preferences.dart'; 6 7 class SettingsScreen extends StatefulWidget { 8 const SettingsScreen({super.key}); 9 10 @override 11 State<SettingsScreen> createState() => _SettingsScreenState(); 12 } 13 14 class _SettingsScreenState extends State<SettingsScreen> { 15 final TextEditingController _usernameController = TextEditingController(); 16 String _displayedUsername = ''; 17 bool _isDarkMode = false; 18 19 @override 20 void initState() { 21 super.initState(); 22 _loadSettings(); 23 } 24 25 Future<void> _loadSettings() async { 26 final prefs = await SharedPreferences.getInstance(); 27 final username = prefs.getString('username'); 28 final isDarkMode = 29 prefs.getBool('isDarkMode') ?? false; 30 setState(() { 31 _displayedUsername = username ?? ''; 32 _usernameController.text = username ?? ''; 33 _isDarkMode = isDarkMode; 34 }); 35 } 36 37 @override 38 Widget build(BuildContext context) { 39 return Scaffold( 40 appBar: AppBar( 41 title: const Text( 42 'Settings', 43 style: TextStyle(color: Colors.black), 44 ), 45 actions: [ 46 IconButton( 47 onPressed: () => FirebaseAuth.instance.signOut(), 48 icon: const Icon(Icons.logout, color: Colors.black), 49 ), 50 ], 51 ), 52 body: ListView( 53 children: [ 54 ListTile( 55 title: const Text( 56 'Username', 57 style: TextStyle(color: Colors.black), 58 ), 59 subtitle: Text(_displayedUsername), 60 trailing: const Icon(Icons.edit), 61 onTap: () => Navigator.push( 62 context, 63 MaterialPageRoute(builder: (context) => const UsernamePage()), 64 ), 65 ), 66 SwitchListTile( 67 title: const Text('Dark Mode'), 68 value: _isDarkMode, 69 onChanged: (value) => setState(() => _isDarkMode = value), 70 ), 71 ListTile( 72 title: const Text( 73 'About', 74 style: TextStyle(color: Colors.black), 75 ), 76 trailing: const Icon(Icons.info_outline), 77 onTap: () => Navigator.push( 78 context, 79 MaterialPageRoute(builder: (context) => const AboutPage()), 80 ), 81 ), 82 ], 83 ), 84 ); 85 } 86 }