import 'package:flutter/material.dart'; import 'package:provider/provider.dart'; import '../../../core/theme/theme_provider.dart'; import '../../../data/providers/user_provider.dart'; import '../../../data/services/secure_storage.dart'; import '../../widgets/custom_button.dart'; import '../../widgets/custom_list_tile.dart'; import '../login/login_screen.dart'; import '../profile/profile_screen.dart'; /// Écran des paramètres avec toutes les options et design moderne. /// /// Cet écran permet de gérer les préférences utilisateur, les notifications, /// la sécurité, et d'autres paramètres avec une interface organisée et compacte. /// /// **Fonctionnalités:** /// - Gestion du profil et sécurité /// - Préférences (thème, langue) /// - Notifications /// - Aide et support /// - Déconnexion class SettingsScreen extends StatefulWidget { const SettingsScreen({super.key}); @override State createState() => _SettingsScreenState(); } class _SettingsScreenState extends State { // ============================================================================ // ÉTATS // ============================================================================ bool _notificationsEnabled = true; bool _locationEnabled = false; String _selectedLanguage = 'Français'; final SecureStorage _secureStorage = SecureStorage(); // ============================================================================ // BUILD // ============================================================================ @override Widget build(BuildContext context) { final theme = Theme.of(context); return Scaffold( appBar: AppBar( title: const Text('Paramètres'), ), body: ListView( padding: const EdgeInsets.symmetric(vertical: 8), children: [ _buildAccountSection(theme), _buildPreferencesSection(theme), _buildNotificationsSection(theme), _buildHelpSection(theme), _buildLogoutSection(theme), const SizedBox(height: 16), ], ), ); } // ============================================================================ // SECTIONS // ============================================================================ /// Construit la section compte. Widget _buildAccountSection(ThemeData theme) { return Column( crossAxisAlignment: CrossAxisAlignment.start, children: [ _buildSectionHeader(theme, 'Compte'), CustomListTile( icon: Icons.person, label: 'Profil', subtitle: 'Gérer vos informations personnelles', onTap: _handleProfile, ), CustomListTile( icon: Icons.security, label: 'Sécurité', subtitle: 'Mot de passe et authentification', onTap: _handleSecurity, ), CustomListTile( icon: Icons.privacy_tip, label: 'Confidentialité', subtitle: 'Contrôler votre vie privée', onTap: _handlePrivacy, ), const Divider(height: 32), ], ); } /// Construit la section préférences. Widget _buildPreferencesSection(ThemeData theme) { final themeProvider = Provider.of(context); return Column( crossAxisAlignment: CrossAxisAlignment.start, children: [ _buildSectionHeader(theme, 'Préférences'), SwitchListTile( secondary: Icon( Icons.dark_mode, color: theme.colorScheme.primary, ), title: const Text('Mode sombre'), subtitle: const Text('Activer le thème sombre'), value: themeProvider.isDarkMode, onChanged: (value) { themeProvider.toggleTheme(); }, ), ListTile( leading: Icon(Icons.language, color: theme.colorScheme.primary), title: const Text('Langue'), subtitle: Text(_selectedLanguage), trailing: DropdownButton( value: _selectedLanguage, underline: const SizedBox(), items: const [ DropdownMenuItem(value: 'Français', child: Text('Français')), DropdownMenuItem(value: 'English', child: Text('English')), ], onChanged: (value) { if (value != null) { setState(() { _selectedLanguage = value; }); } }, ), ), const Divider(height: 32), ], ); } /// Construit la section notifications. Widget _buildNotificationsSection(ThemeData theme) { return Column( crossAxisAlignment: CrossAxisAlignment.start, children: [ _buildSectionHeader(theme, 'Notifications'), SwitchListTile( secondary: Icon( Icons.notifications, color: theme.colorScheme.primary, ), title: const Text('Notifications push'), subtitle: const Text('Recevoir des notifications'), value: _notificationsEnabled, onChanged: (value) { setState(() { _notificationsEnabled = value; }); }, ), SwitchListTile( secondary: Icon( Icons.location_on, color: theme.colorScheme.primary, ), title: const Text('Localisation'), subtitle: const Text('Partager votre localisation'), value: _locationEnabled, onChanged: (value) { setState(() { _locationEnabled = value; }); }, ), const Divider(height: 32), ], ); } /// Construit la section aide. Widget _buildHelpSection(ThemeData theme) { return Column( crossAxisAlignment: CrossAxisAlignment.start, children: [ _buildSectionHeader(theme, 'Aide et Support'), CustomListTile( icon: Icons.help_outline, label: 'Centre d\'aide', subtitle: 'FAQ et support', onTap: _handleHelp, ), CustomListTile( icon: Icons.feedback, label: 'Envoyer un commentaire', subtitle: 'Partagez vos suggestions', onTap: _handleFeedback, ), CustomListTile( icon: Icons.info_outline, label: 'À propos', subtitle: 'Version 1.0.0', onTap: _handleAbout, ), const Divider(height: 32), ], ); } /// Construit la section déconnexion. Widget _buildLogoutSection(ThemeData theme) { return Padding( padding: const EdgeInsets.symmetric(horizontal: 16), child: CustomButton( text: 'Déconnexion', icon: Icons.logout, onPressed: _handleLogout, variant: ButtonVariant.outlined, size: ButtonSize.medium, ), ); } // ============================================================================ // WIDGETS UTILITAIRES // ============================================================================ /// Construit l'en-tête de section. Widget _buildSectionHeader(ThemeData theme, String title) { return Padding( padding: const EdgeInsets.fromLTRB(16, 16, 16, 8), child: Text( title, style: theme.textTheme.titleMedium?.copyWith( color: theme.colorScheme.primary, fontWeight: FontWeight.bold, ), ), ); } // ============================================================================ // ACTIONS // ============================================================================ /// Gère la navigation vers le profil. void _handleProfile() { Navigator.push( context, MaterialPageRoute( builder: (context) => const ProfileScreen(), ), ); } /// Gère la sécurité. void _handleSecurity() { _showDialog( title: 'Sécurité', content: 'Fonctionnalité de sécurité à venir', ); } /// Gère la confidentialité. void _handlePrivacy() { _showDialog( title: 'Confidentialité', content: 'Paramètres de confidentialité à venir', ); } /// Gère l'aide. void _handleHelp() { _showDialog( title: 'Centre d\'aide', content: 'FAQ et support à venir', ); } /// Gère le feedback. void _handleFeedback() { _showDialog( title: 'Envoyer un commentaire', content: 'Formulaire de feedback à venir', ); } /// Gère l'à propos. void _handleAbout() { showAboutDialog( context: context, applicationName: 'AfterWork', applicationVersion: '1.0.0', applicationIcon: const Icon(Icons.event, size: 48), children: const [ Text('Application de gestion d\'événements sociaux'), ], ); } /// Gère la déconnexion. void _handleLogout() { showDialog( context: context, builder: (context) => AlertDialog( title: const Text('Déconnexion'), content: const Text('Êtes-vous sûr de vouloir vous déconnecter ?'), actions: [ TextButton( onPressed: () => Navigator.pop(context), child: const Text('Annuler'), ), TextButton( onPressed: () async { Navigator.pop(context); await _performLogout(); }, style: TextButton.styleFrom( foregroundColor: Theme.of(context).colorScheme.error, ), child: const Text('Déconnexion'), ), ], ), ); } /// Affiche une boîte de dialogue simple. void _showDialog({required String title, required String content}) { showDialog( context: context, builder: (context) => AlertDialog( title: Text(title), content: Text(content), actions: [ TextButton( onPressed: () => Navigator.pop(context), child: const Text('Fermer'), ), ], ), ); } /// Effectue la déconnexion complète de l'utilisateur. Future _performLogout() async { try { // Réinitialiser le UserProvider Provider.of(context, listen: false).resetUser(); // Supprimer toutes les données utilisateur du stockage sécurisé await _secureStorage.deleteUserInfo(); if (mounted) { // Naviguer vers l'écran de connexion Navigator.of(context).pushAndRemoveUntil( MaterialPageRoute( builder: (context) => const LoginScreen(), ), (route) => false, ); ScaffoldMessenger.of(context).showSnackBar( const SnackBar( content: Text('Déconnexion réussie'), behavior: SnackBarBehavior.floating, ), ); } } catch (e) { if (mounted) { ScaffoldMessenger.of(context).showSnackBar( SnackBar( content: Text('Erreur lors de la déconnexion: ${e.toString()}'), behavior: SnackBarBehavior.floating, ), ); } } } }