import 'package:flutter/material.dart'; import '../constants/app_colors.dart'; import '../constants/app_strings.dart'; import '../services/supabase_service.dart'; class SettingsScreen extends StatefulWidget { const SettingsScreen({super.key}); @override State createState() => _SettingsScreenState(); } class _SettingsScreenState extends State { late String _selectedLanguage; @override void initState() { super.initState(); _selectedLanguage = AppStrings.currentLanguageName; } @override Widget build(BuildContext context) { final user = SupabaseService.currentUser; final userName = user?.userMetadata?['name'] ?? user?.email?.split('@')[0] ?? AppStrings.userPlaceholder; final userEmail = user?.email ?? 'usuario@exemplo.com'; return Scaffold( backgroundColor: AppColors.background, appBar: AppBar( title: Text( AppStrings.settingsTitle, style: const TextStyle( color: Colors.white, fontSize: 20, fontWeight: FontWeight.bold, ), ), backgroundColor: AppColors.background, elevation: 0, iconTheme: const IconThemeData(color: Colors.white), leading: IconButton( icon: const Icon(Icons.arrow_back, color: Colors.white), onPressed: () => Navigator.pop(context), ), ), body: SingleChildScrollView( padding: const EdgeInsets.all(16), child: Column( children: [ // User Profile Section Container( padding: const EdgeInsets.all(20), decoration: BoxDecoration( color: AppColors.backgroundGrey, borderRadius: BorderRadius.circular(16), ), child: Row( children: [ Container( width: 60, height: 60, decoration: BoxDecoration( color: AppColors.buttonColor, borderRadius: BorderRadius.circular(30), ), child: const Icon( Icons.person, color: Colors.white, size: 30, ), ), const SizedBox(width: 16), Expanded( child: Column( crossAxisAlignment: CrossAxisAlignment.start, children: [ Text( userName, style: const TextStyle( color: Colors.white, fontSize: 18, fontWeight: FontWeight.bold, ), ), Text( userEmail, style: TextStyle( color: Colors.white.withValues(alpha: 0.7), fontSize: 14, ), ), ], ), ), IconButton( icon: const Icon(Icons.edit, color: AppColors.buttonColor), onPressed: () { _showEditProfileDialog(context, userName, userEmail); }, ), ], ), ), const SizedBox(height: 24), // Settings Items Container( decoration: BoxDecoration( color: AppColors.backgroundGrey, borderRadius: BorderRadius.circular(16), ), child: Column( children: [ _buildSettingsItem( icon: Icons.language, title: AppStrings.language, trailing: Text( _selectedLanguage, style: TextStyle( color: Colors.white.withValues(alpha: 0.7), fontSize: 16, ), ), onTap: () { _showLanguageSelector(context); }, ), _buildDivider(), _buildSettingsItem( icon: Icons.info, title: AppStrings.about, onTap: () { _showAboutDialog(context); }, ), ], ), ), const SizedBox(height: 24), // Logout Button SizedBox( width: double.infinity, child: ElevatedButton( onPressed: () { _showLogoutDialog(context); }, style: ElevatedButton.styleFrom( backgroundColor: Colors.red, padding: const EdgeInsets.symmetric(vertical: 16), shape: RoundedRectangleBorder( borderRadius: BorderRadius.circular(12), ), ), child: Text( AppStrings.logout, style: const TextStyle( color: Colors.white, fontSize: 16, fontWeight: FontWeight.bold, ), ), ), ), ], ), ), ); } Widget _buildSettingsItem({ required IconData icon, required String title, Widget? trailing, VoidCallback? onTap, }) { return ListTile( leading: Icon(icon, color: AppColors.buttonColor, size: 24), title: Text( title, style: const TextStyle( color: Colors.white, fontSize: 16, fontWeight: FontWeight.w500, ), ), trailing: trailing, onTap: onTap, ); } Widget _buildDivider() { return Divider( color: AppColors.buttonColor.withValues(alpha: 0.2), height: 1, indent: 16, endIndent: 16, ); } void _showEditProfileDialog(BuildContext context, String currentName, String currentEmail) { final nameController = TextEditingController(text: currentName); final emailController = TextEditingController(text: currentEmail); showDialog( context: context, builder: (context) => AlertDialog( backgroundColor: AppColors.backgroundGrey, title: Text( AppStrings.editProfile, style: const TextStyle(color: Colors.white), ), content: Column( mainAxisSize: MainAxisSize.min, children: [ TextField( controller: nameController, style: const TextStyle(color: Colors.white), decoration: InputDecoration( labelText: AppStrings.labelName, labelStyle: const TextStyle(color: Colors.white70), enabledBorder: const UnderlineInputBorder(borderSide: BorderSide(color: Colors.white24)), focusedBorder: const UnderlineInputBorder(borderSide: BorderSide(color: AppColors.buttonColor)), ), ), const SizedBox(height: 16), TextField( controller: emailController, style: const TextStyle(color: Colors.white), decoration: InputDecoration( labelText: AppStrings.labelEmail, labelStyle: const TextStyle(color: Colors.white70), enabledBorder: const UnderlineInputBorder(borderSide: BorderSide(color: Colors.white24)), focusedBorder: const UnderlineInputBorder(borderSide: BorderSide(color: AppColors.buttonColor)), ), ), ], ), actions: [ TextButton( onPressed: () => Navigator.pop(context), child: Text( AppStrings.btnCancel, style: const TextStyle(color: Colors.white54), ), ), ElevatedButton( onPressed: () async { try { await SupabaseService.updateProfile( name: nameController.text.trim(), email: emailController.text.trim(), ); if (context.mounted) { Navigator.pop(context); setState(() {}); ScaffoldMessenger.of(context).showSnackBar( const SnackBar(content: Text('Perfil atualizado!'), backgroundColor: Colors.green), ); } } catch (e) { if (context.mounted) { ScaffoldMessenger.of(context).showSnackBar( SnackBar(content: Text(e.toString()), backgroundColor: Colors.red), ); } } }, style: ElevatedButton.styleFrom(backgroundColor: AppColors.buttonColor), child: Text(AppStrings.btnDefine), ), ], ), ); } void _showLanguageSelector(BuildContext context) { showDialog( context: context, builder: (context) => StatefulBuilder( builder: (context, setDialogState) { return AlertDialog( backgroundColor: AppColors.backgroundGrey, title: Text( AppStrings.selectLanguage, style: const TextStyle(color: Colors.white), ), content: Column( mainAxisSize: MainAxisSize.min, children: [ _buildLanguageOption(context, setDialogState, 'Português'), _buildLanguageOption(context, setDialogState, 'English'), _buildLanguageOption(context, setDialogState, 'Español'), ], ), actions: [ TextButton( onPressed: () => Navigator.pop(context), child: Text( AppStrings.btnCancel, style: const TextStyle(color: AppColors.buttonColor), ), ), ], ); } ), ); } Widget _buildLanguageOption(BuildContext context, StateSetter setDialogState, String language) { return ListTile( title: Text(language, style: const TextStyle(color: Colors.white)), trailing: Radio( value: language, groupValue: _selectedLanguage, onChanged: (value) { if (value != null) { _updateLanguage(value); Navigator.pop(context); } }, fillColor: WidgetStateProperty.all(AppColors.buttonColor), ), onTap: () { _updateLanguage(language); Navigator.pop(context); }, ); } void _updateLanguage(String language) { setState(() { _selectedLanguage = language; AppStrings.setLanguage(language); }); } void _showAboutDialog(BuildContext context) { showDialog( context: context, builder: (context) => AlertDialog( backgroundColor: AppColors.backgroundGrey, title: Text(AppStrings.about, style: const TextStyle(color: Colors.white)), content: Column( mainAxisSize: MainAxisSize.min, crossAxisAlignment: CrossAxisAlignment.start, children: [ Text( AppStrings.appTitle, style: const TextStyle( color: Colors.white, fontWeight: FontWeight.bold, ), ), const SizedBox(height: 8), Text('${AppStrings.version}: 1.0.0', style: const TextStyle(color: Colors.white70)), const SizedBox(height: 8), Text( AppStrings.appDescription, style: const TextStyle(color: Colors.white70), ), ], ), actions: [ TextButton( onPressed: () => Navigator.pop(context), child: Text( AppStrings.btnOk, style: const TextStyle(color: AppColors.buttonColor), ), ), ], ), ); } void _showLogoutDialog(BuildContext context) { showDialog( context: context, builder: (context) => AlertDialog( backgroundColor: AppColors.backgroundGrey, title: Text( AppStrings.confirmLogout, style: const TextStyle(color: Colors.white), ), content: Text( AppStrings.confirmLogoutMessage, style: const TextStyle(color: Colors.white70), ), actions: [ TextButton( onPressed: () => Navigator.pop(context), child: Text( AppStrings.btnCancel, style: const TextStyle(color: AppColors.buttonColor), ), ), TextButton( onPressed: () { Navigator.pop(context); Navigator.pushReplacementNamed(context, '/'); }, child: Text(AppStrings.logout, style: const TextStyle(color: Colors.red)), ), ], ), ); } }