416 lines
14 KiB
Dart
416 lines
14 KiB
Dart
import 'package:flutter/material.dart';
|
|
import 'package:flutter_bloc/flutter_bloc.dart';
|
|
import 'package:travel_mate/components/widgets/user_state_widget.dart';
|
|
import 'package:travel_mate/services/error_service.dart';
|
|
import '../../../blocs/user/user_bloc.dart';
|
|
import '../../../blocs/user/user_state.dart' as user_state;
|
|
import '../../../blocs/user/user_event.dart' as user_event;
|
|
import '../../../services/auth_service.dart';
|
|
|
|
class ProfileContent extends StatelessWidget {
|
|
ProfileContent({super.key});
|
|
|
|
final _errorService = ErrorService();
|
|
|
|
@override
|
|
Widget build(BuildContext context) {
|
|
return UserStateWrapper(
|
|
builder: (context, user) {
|
|
final isEmailAuth = user.authMethod == 'email' || user.authMethod == null;
|
|
|
|
return Column(
|
|
children: [
|
|
// Section titre
|
|
Padding(
|
|
padding: EdgeInsets.symmetric(horizontal: 16, vertical: 8),
|
|
child: Row(
|
|
children: [
|
|
Text(
|
|
'Profil utilisateur',
|
|
style: TextStyle(
|
|
fontSize: 18,
|
|
fontWeight: FontWeight.bold,
|
|
color: Colors.grey[600],
|
|
),
|
|
),
|
|
],
|
|
),
|
|
),
|
|
|
|
// Card du profil
|
|
Card(
|
|
margin: EdgeInsets.symmetric(horizontal: 16, vertical: 8),
|
|
child: Padding(
|
|
padding: EdgeInsets.all(16),
|
|
child: Column(
|
|
children: [
|
|
// Photo de profil
|
|
CircleAvatar(
|
|
radius: 40,
|
|
backgroundColor: Theme.of(context).colorScheme.primary,
|
|
child: Text(
|
|
user.prenom.isNotEmpty
|
|
? user.prenom[0].toUpperCase()
|
|
: 'U',
|
|
style: TextStyle(
|
|
fontSize: 24,
|
|
fontWeight: FontWeight.bold,
|
|
color: Colors.white,
|
|
),
|
|
),
|
|
),
|
|
|
|
SizedBox(height: 16),
|
|
|
|
// Nom complet
|
|
Text(
|
|
'${user.prenom} ${user.nom ?? ''}',
|
|
style: TextStyle(
|
|
fontSize: 20,
|
|
fontWeight: FontWeight.bold,
|
|
),
|
|
),
|
|
|
|
SizedBox(height: 8),
|
|
|
|
// Email
|
|
Text(
|
|
user.email,
|
|
style: TextStyle(fontSize: 14, color: Colors.grey[600]),
|
|
),
|
|
|
|
SizedBox(height: 12),
|
|
|
|
// Badge de méthode de connexion
|
|
Container(
|
|
padding: EdgeInsets.symmetric(horizontal: 12, vertical: 6),
|
|
decoration: BoxDecoration(
|
|
color: _getAuthMethodColor(user.authMethod, context),
|
|
borderRadius: BorderRadius.circular(12),
|
|
),
|
|
child: Row(
|
|
mainAxisSize: MainAxisSize.min,
|
|
children: [
|
|
Image.asset(
|
|
_getAuthMethodIcon(user.authMethod),
|
|
height: 16,
|
|
width: 16,
|
|
),
|
|
SizedBox(width: 8),
|
|
Text(
|
|
_getAuthMethodLabel(user.authMethod),
|
|
style: TextStyle(
|
|
fontSize: 12,
|
|
fontWeight: FontWeight.w600,
|
|
color: _getAuthMethodTextColor(user.authMethod, context),
|
|
),
|
|
),
|
|
],
|
|
),
|
|
),
|
|
],
|
|
),
|
|
),
|
|
),
|
|
|
|
// Actions du profil
|
|
ListTile(
|
|
leading: Icon(Icons.edit),
|
|
title: Text('Modifier le profil'),
|
|
trailing: Icon(Icons.arrow_forward_ios),
|
|
onTap: () {
|
|
_showEditProfileDialog(context, user);
|
|
},
|
|
),
|
|
|
|
// Afficher l'option de changement de mot de passe seulement pour email
|
|
if (isEmailAuth)
|
|
ListTile(
|
|
leading: Icon(Icons.lock),
|
|
title: Text('Changer le mot de passe'),
|
|
trailing: Icon(Icons.arrow_forward_ios),
|
|
onTap: () {
|
|
_showChangePasswordDialog(context, user);
|
|
},
|
|
),
|
|
|
|
Divider(),
|
|
|
|
ListTile(
|
|
leading: Icon(Icons.delete, color: Colors.red),
|
|
title: Text('Supprimer le compte'),
|
|
trailing: Icon(Icons.arrow_forward_ios),
|
|
onTap: () {
|
|
_showDeleteAccountDialog(context, user);
|
|
},
|
|
),
|
|
],
|
|
);
|
|
},
|
|
);
|
|
}
|
|
|
|
String _getAuthMethodLabel(String? authMethod) {
|
|
switch (authMethod) {
|
|
case 'apple':
|
|
return 'Connecté avec Apple';
|
|
case 'google':
|
|
return 'Connecté avec Google';
|
|
default:
|
|
return 'Connecté avec Email';
|
|
}
|
|
}
|
|
|
|
String _getAuthMethodIcon(String? authMethod) {
|
|
switch (authMethod) {
|
|
case 'apple':
|
|
return 'assets/icons/apple_white.png';
|
|
case 'google':
|
|
return 'assets/icons/google.png';
|
|
default:
|
|
return '';
|
|
}
|
|
}
|
|
|
|
Color _getAuthMethodColor(String? authMethod, BuildContext context) {
|
|
final isDarkMode = Theme.of(context).brightness == Brightness.dark;
|
|
|
|
switch (authMethod) {
|
|
case 'apple':
|
|
return isDarkMode ? Colors.white : Colors.black87;
|
|
case 'google':
|
|
return isDarkMode ? Colors.white : Colors.black87;
|
|
default:
|
|
return isDarkMode ? Colors.white : Colors.blue;
|
|
}
|
|
}
|
|
|
|
Color _getAuthMethodTextColor(String? authMethod, BuildContext context) {
|
|
final isDarkMode = Theme.of(context).brightness == Brightness.dark;
|
|
|
|
switch (authMethod) {
|
|
case 'apple':
|
|
return isDarkMode ? Colors.black87 : Colors.white;
|
|
case 'google':
|
|
return isDarkMode ? Colors.black87 : Colors.white;
|
|
default:
|
|
return isDarkMode ? Colors.black87 : Colors.white;
|
|
}
|
|
}
|
|
|
|
void _showEditProfileDialog(BuildContext context, user_state.UserModel user) {
|
|
final nomController = TextEditingController(text: user.nom);
|
|
final prenomController = TextEditingController(text: user.prenom);
|
|
|
|
showDialog(
|
|
context: context,
|
|
builder: (BuildContext dialogContext) {
|
|
return AlertDialog(
|
|
title: Text('Modifier le profil'),
|
|
content: Column(
|
|
mainAxisSize: MainAxisSize.min,
|
|
children: [
|
|
TextField(
|
|
controller: prenomController,
|
|
decoration: InputDecoration(labelText: 'Prénom'),
|
|
),
|
|
SizedBox(height: 16),
|
|
TextField(
|
|
controller: nomController,
|
|
decoration: InputDecoration(labelText: 'Nom'),
|
|
),
|
|
],
|
|
),
|
|
actions: [
|
|
TextButton(
|
|
onPressed: () => Navigator.of(dialogContext).pop(),
|
|
child: Text('Annuler'),
|
|
),
|
|
TextButton(
|
|
onPressed: () {
|
|
if (prenomController.text.trim().isNotEmpty) {
|
|
context.read<UserBloc>().add(
|
|
user_event.UserUpdated({
|
|
'prenom': prenomController.text.trim(),
|
|
'nom': nomController.text.trim(),
|
|
}),
|
|
);
|
|
|
|
Navigator.of(dialogContext).pop();
|
|
ScaffoldMessenger.of(context).showSnackBar(
|
|
SnackBar(
|
|
content: Text('Profil mis à jour !'),
|
|
backgroundColor: Colors.green,
|
|
),
|
|
);
|
|
}
|
|
},
|
|
child: Text('Sauvegarder'),
|
|
),
|
|
],
|
|
);
|
|
},
|
|
);
|
|
}
|
|
|
|
void _showChangePasswordDialog(
|
|
BuildContext context,
|
|
user_state.UserModel user,
|
|
) {
|
|
final currentPasswordController = TextEditingController();
|
|
final newPasswordController = TextEditingController();
|
|
final confirmPasswordController = TextEditingController();
|
|
final authService = AuthService();
|
|
|
|
showDialog(
|
|
context: context,
|
|
builder: (BuildContext dialogContext) {
|
|
return AlertDialog(
|
|
title: Text('Changer le mot de passe'),
|
|
content: Column(
|
|
mainAxisSize: MainAxisSize.min,
|
|
children: [
|
|
TextField(
|
|
controller: currentPasswordController,
|
|
obscureText: true,
|
|
decoration: InputDecoration(labelText: 'Mot de passe actuel'),
|
|
),
|
|
SizedBox(height: 16),
|
|
TextField(
|
|
controller: newPasswordController,
|
|
obscureText: true,
|
|
decoration: InputDecoration(labelText: 'Nouveau mot de passe'),
|
|
),
|
|
SizedBox(height: 16),
|
|
TextField(
|
|
controller: confirmPasswordController,
|
|
obscureText: true,
|
|
decoration: InputDecoration(
|
|
labelText: 'Confirmer le mot de passe',
|
|
),
|
|
),
|
|
],
|
|
),
|
|
actions: [
|
|
TextButton(
|
|
onPressed: () => Navigator.of(dialogContext).pop(),
|
|
child: Text('Annuler'),
|
|
),
|
|
TextButton(
|
|
onPressed: () async {
|
|
if (currentPasswordController.text.isEmpty ||
|
|
newPasswordController.text.isEmpty ||
|
|
confirmPasswordController.text.isEmpty) {
|
|
ScaffoldMessenger.of(context).showSnackBar(
|
|
SnackBar(
|
|
content: Text('Tous les champs sont requis'),
|
|
backgroundColor: Colors.red,
|
|
),
|
|
);
|
|
return;
|
|
}
|
|
|
|
if (newPasswordController.text !=
|
|
confirmPasswordController.text) {
|
|
ScaffoldMessenger.of(context).showSnackBar(
|
|
SnackBar(
|
|
content: Text('Les mots de passe ne correspondent pas'),
|
|
backgroundColor: Colors.red,
|
|
),
|
|
);
|
|
return;
|
|
}
|
|
|
|
try {
|
|
await authService.resetPasswordFromCurrentPassword(
|
|
currentPassword: currentPasswordController.text,
|
|
newPassword: newPasswordController.text,
|
|
email: user.email,
|
|
);
|
|
|
|
Navigator.of(dialogContext).pop();
|
|
ScaffoldMessenger.of(context).showSnackBar(
|
|
SnackBar(
|
|
content: Text('Mot de passe changé !'),
|
|
backgroundColor: Colors.green,
|
|
),
|
|
);
|
|
} catch (e) {
|
|
_errorService.showError(
|
|
message: 'Erreur: Mot de passe actuel incorrect',
|
|
);
|
|
}
|
|
},
|
|
child: Text('Changer'),
|
|
),
|
|
],
|
|
);
|
|
},
|
|
);
|
|
}
|
|
|
|
void _showDeleteAccountDialog(
|
|
BuildContext context,
|
|
user_state.UserModel user,
|
|
) {
|
|
final passwordController = TextEditingController();
|
|
final authService = AuthService();
|
|
|
|
showDialog(
|
|
context: context,
|
|
builder: (BuildContext dialogContext) {
|
|
return AlertDialog(
|
|
title: Text('Supprimer le compte'),
|
|
content: Column(
|
|
mainAxisSize: MainAxisSize.min,
|
|
children: [
|
|
Text(
|
|
'Êtes-vous sûr de vouloir supprimer votre compte ? Cette action est irréversible.',
|
|
),
|
|
SizedBox(height: 16),
|
|
TextField(
|
|
controller: passwordController,
|
|
obscureText: true,
|
|
decoration: InputDecoration(
|
|
labelText: 'Confirmez votre mot de passe',
|
|
border: OutlineInputBorder(),
|
|
),
|
|
),
|
|
],
|
|
),
|
|
actions: [
|
|
TextButton(
|
|
onPressed: () => Navigator.of(dialogContext).pop(),
|
|
child: Text('Annuler'),
|
|
),
|
|
TextButton(
|
|
onPressed: () async {
|
|
try {
|
|
await authService.deleteAccount(
|
|
password: passwordController.text,
|
|
email: user.email,
|
|
);
|
|
|
|
Navigator.of(dialogContext).pop();
|
|
context.read<UserBloc>().add(user_event.UserLoggedOut());
|
|
Navigator.pushNamedAndRemoveUntil(
|
|
context,
|
|
'/login',
|
|
(route) => false,
|
|
);
|
|
} catch (e) {
|
|
_errorService.showError(
|
|
message: 'Erreur: Mot de passe incorrect',
|
|
);
|
|
}
|
|
},
|
|
style: TextButton.styleFrom(foregroundColor: Colors.red),
|
|
child: Text('Supprimer'),
|
|
),
|
|
],
|
|
);
|
|
},
|
|
);
|
|
}
|
|
}
|