feat: Refactor CreateTripContent with modernized UI components and improved error handling

This commit is contained in:
Dayron
2025-11-03 15:55:15 +01:00
parent 4836514223
commit 64fcc88984

View File

@@ -80,7 +80,6 @@ class _CreateTripContentState extends State<CreateTripContent> {
bool _isLoading = false; bool _isLoading = false;
String? _createdTripId; String? _createdTripId;
String? _selectedImageUrl; String? _selectedImageUrl;
bool _isLoadingImage = false;
/// Google Maps API key for location services /// Google Maps API key for location services
static final String _apiKey = dotenv.env['GOOGLE_MAPS_API_KEY'] ?? ''; static final String _apiKey = dotenv.env['GOOGLE_MAPS_API_KEY'] ?? '';
@@ -263,9 +262,6 @@ class _CreateTripContentState extends State<CreateTripContent> {
/// Charge l'image du lieu depuis Google Places API /// Charge l'image du lieu depuis Google Places API
Future<void> _loadPlaceImage(String location) async { Future<void> _loadPlaceImage(String location) async {
print('CreateTripContent: Chargement de l\'image pour: $location'); print('CreateTripContent: Chargement de l\'image pour: $location');
setState(() {
_isLoadingImage = true;
});
try { try {
final imageUrl = await _placeImageService.getPlaceImageUrl(location); final imageUrl = await _placeImageService.getPlaceImageUrl(location);
@@ -273,16 +269,12 @@ class _CreateTripContentState extends State<CreateTripContent> {
if (mounted) { if (mounted) {
setState(() { setState(() {
_selectedImageUrl = imageUrl; _selectedImageUrl = imageUrl;
_isLoadingImage = false;
}); });
print('CreateTripContent: État mis à jour avec imageUrl: $_selectedImageUrl'); print('CreateTripContent: État mis à jour avec imageUrl: $_selectedImageUrl');
} }
} catch (e) { } catch (e) {
print('CreateTripContent: Erreur lors du chargement de l\'image: $e'); print('CreateTripContent: Erreur lors du chargement de l\'image: $e');
if (mounted) { if (mounted) {
setState(() {
_isLoadingImage = false;
});
_errorService.logError( _errorService.logError(
'create_trip_content.dart', 'create_trip_content.dart',
'Erreur lors du chargement de l\'image: $e', 'Erreur lors du chargement de l\'image: $e',
@@ -325,41 +317,151 @@ class _CreateTripContentState extends State<CreateTripContent> {
_locationController.dispose(); _locationController.dispose();
_budgetController.dispose(); _budgetController.dispose();
_participantController.dispose(); _participantController.dispose();
_hideSuggestions();
super.dispose(); super.dispose();
} }
// Nouveau widget pour les champs de texte modernes
Widget _buildModernTextField({
required TextEditingController controller,
required String label,
required IconData icon,
String? Function(String?)? validator,
TextInputType? keyboardType,
int maxLines = 1,
Widget? suffixIcon,
}) {
final theme = Theme.of(context);
final isDarkMode = theme.brightness == Brightness.dark;
return TextFormField(
controller: controller,
validator: validator,
keyboardType: keyboardType,
maxLines: maxLines,
style: theme.textTheme.bodyLarge?.copyWith(
color: theme.colorScheme.onSurface,
),
decoration: InputDecoration(
hintText: label,
hintStyle: theme.textTheme.bodyLarge?.copyWith(
color: theme.colorScheme.onSurface.withOpacity(0.5),
),
prefixIcon: Icon(
icon,
color: theme.colorScheme.onSurface.withOpacity(0.5),
),
suffixIcon: suffixIcon,
border: OutlineInputBorder(
borderRadius: BorderRadius.circular(12),
borderSide: BorderSide(
color: isDarkMode
? Colors.white.withOpacity(0.2)
: Colors.black.withOpacity(0.2),
),
),
enabledBorder: OutlineInputBorder(
borderRadius: BorderRadius.circular(12),
borderSide: BorderSide(
color: isDarkMode
? Colors.white.withOpacity(0.2)
: Colors.black.withOpacity(0.2),
),
),
focusedBorder: OutlineInputBorder(
borderRadius: BorderRadius.circular(12),
borderSide: BorderSide(
color: Colors.teal,
width: 2,
),
),
errorBorder: OutlineInputBorder(
borderRadius: BorderRadius.circular(12),
borderSide: const BorderSide(
color: Colors.red,
width: 2,
),
),
filled: true,
fillColor: theme.cardColor,
contentPadding: const EdgeInsets.symmetric(
horizontal: 16,
vertical: 16,
),
),
);
}
// Nouveau widget pour les champs de date modernes
Widget _buildDateField({
required DateTime? date,
required VoidCallback onTap,
}) {
final theme = Theme.of(context);
final isDarkMode = theme.brightness == Brightness.dark;
return InkWell(
onTap: onTap,
borderRadius: BorderRadius.circular(12),
child: Container(
padding: const EdgeInsets.all(16),
decoration: BoxDecoration(
color: theme.cardColor,
borderRadius: BorderRadius.circular(12),
border: Border.all(
color: isDarkMode
? Colors.white.withOpacity(0.2)
: Colors.black.withOpacity(0.2),
),
),
child: Row(
children: [
Icon(
Icons.calendar_today,
color: theme.colorScheme.onSurface.withOpacity(0.5),
size: 20,
),
const SizedBox(width: 12),
Text(
date != null
? '${date.day.toString().padLeft(2, '0')}/${date.month.toString().padLeft(2, '0')}/${date.year}'
: 'mm/dd/yyyy',
style: theme.textTheme.bodyLarge?.copyWith(
color: date != null
? theme.colorScheme.onSurface
: theme.colorScheme.onSurface.withOpacity(0.5),
),
),
],
),
),
);
}
@override @override
Widget build(BuildContext context) { Widget build(BuildContext context) {
final theme = Theme.of(context);
final isDarkMode = theme.brightness == Brightness.dark;
return BlocListener<TripBloc, TripState>( return BlocListener<TripBloc, TripState>(
listener: (context, tripState) { listener: (context, tripState) {
if (tripState is TripCreated) { if (tripState is TripCreated) {
// Stocker l'ID du trip et créer le groupe
_createdTripId = tripState.tripId; _createdTripId = tripState.tripId;
_createGroupAndAccountForTrip(_createdTripId!); _createGroupAndAccountForTrip(_createdTripId!);
} else if (tripState is TripOperationSuccess) { } else if (tripState is TripOperationSuccess) {
if (mounted) { if (mounted) {
ScaffoldMessenger.of(context).showSnackBar( _errorService.showSnackbar(message: tripState.message, isError: false);
SnackBar(
content: Text(tripState.message),
backgroundColor: Colors.green,
),
);
setState(() { setState(() {
_isLoading = false; _isLoading = false;
}); });
Navigator.pop(context); Navigator.pop(context, true);
if (isEditing) { if (isEditing) {
Navigator.pop(context); Navigator.pop(context, true);
} }
} }
} else if (tripState is TripError) { } else if (tripState is TripError) {
if (mounted) { if (mounted) {
ScaffoldMessenger.of(context).showSnackBar( _errorService.showSnackbar(message: tripState.message, isError: true);
SnackBar(
content: Text(tripState.message),
backgroundColor: Colors.red,
),
);
setState(() { setState(() {
_isLoading = false; _isLoading = false;
}); });
@@ -370,300 +472,320 @@ class _CreateTripContentState extends State<CreateTripContent> {
builder: (context, userState) { builder: (context, userState) {
if (userState is! user_state.UserLoaded) { if (userState is! user_state.UserLoaded) {
return Scaffold( return Scaffold(
backgroundColor: theme.scaffoldBackgroundColor,
appBar: AppBar( appBar: AppBar(
title: Text(isEditing ? 'Modifier le voyage' : 'Créer un voyage'), title: Text(isEditing ? 'Modifier le voyage' : 'Créer un voyage'),
backgroundColor: theme.appBarTheme.backgroundColor,
foregroundColor: theme.appBarTheme.foregroundColor,
),
body: Center(
child: Text(
'Veuillez vous connecter',
style: theme.textTheme.bodyLarge?.copyWith(
color: theme.colorScheme.onSurface,
),
),
), ),
body: Center(child: Text('Veuillez vous connecter')),
); );
} }
return Scaffold( return Scaffold(
backgroundColor: theme.scaffoldBackgroundColor,
appBar: AppBar( appBar: AppBar(
title: Text(isEditing ? 'Modifier le voyage' : 'Créer un voyage'), title: Text(
backgroundColor: Theme.of(context).colorScheme.primary, isEditing ? 'Modifier le voyage' : 'Créer un voyage',
foregroundColor: Colors.white, style: theme.textTheme.titleLarge?.copyWith(
color: theme.colorScheme.onSurface,
fontWeight: FontWeight.w600,
),
),
backgroundColor: Colors.transparent,
elevation: 0,
leading: IconButton(
icon: Icon(Icons.arrow_back, color: theme.colorScheme.onSurface),
onPressed: () => Navigator.pop(context),
),
), ),
body: GestureDetector( body: GestureDetector(
onTap: _hideSuggestions, // Masquer les suggestions en tapant ailleurs onTap: _hideSuggestions,
child: SingleChildScrollView( child: Container(
padding: const EdgeInsets.all(16), margin: const EdgeInsets.all(16),
child: Form( decoration: BoxDecoration(
key: _formKey, color: theme.cardColor,
child: Column( borderRadius: BorderRadius.circular(20),
crossAxisAlignment: CrossAxisAlignment.start, boxShadow: [
children: [ BoxShadow(
_buildSectionTitle('Informations générales'), color: Colors.black.withOpacity(isDarkMode ? 0.3 : 0.1),
const SizedBox(height: 16), blurRadius: 10,
offset: const Offset(0, 5),
TextFormField( ),
controller: _titleController, ],
validator: (value) { ),
if (value == null || value.trim().isEmpty) { child: SingleChildScrollView(
return 'Titre requis'; padding: const EdgeInsets.all(24),
} child: Form(
return null; key: _formKey,
}, child: Column(
decoration: InputDecoration( crossAxisAlignment: CrossAxisAlignment.start,
labelText: 'Titre du voyage *', children: [
hintText: 'ex: Voyage à Paris', // Titre principal
border: OutlineInputBorder( Text(
borderRadius: BorderRadius.circular(12), 'Nouveau Voyage',
style: theme.textTheme.headlineMedium?.copyWith(
fontWeight: FontWeight.bold,
color: theme.colorScheme.onSurface,
), ),
prefixIcon: const Icon(Icons.travel_explore),
), ),
), const SizedBox(height: 8),
Text(
const SizedBox(height: 16), 'Donne un nom à ton voyage',
style: theme.textTheme.bodyMedium?.copyWith(
TextFormField( color: theme.colorScheme.onSurface.withOpacity(0.7),
controller: _descriptionController,
maxLines: 3,
decoration: InputDecoration(
labelText: 'Description',
hintText: 'Décrivez votre voyage...',
border: OutlineInputBorder(
borderRadius: BorderRadius.circular(12),
), ),
prefixIcon: const Icon(Icons.description),
), ),
), const SizedBox(height: 24),
const SizedBox(height: 16), // Champ nom du voyage
_buildModernTextField(
// Champ de localisation avec suggestions controller: _titleController,
CompositedTransformTarget( label: 'Ex : Week-end à Lisbonne',
link: _layerLink, icon: Icons.edit,
child: TextFormField(
controller: _locationController,
validator: (value) { validator: (value) {
if (value == null || value.trim().isEmpty) { if (value == null || value.trim().isEmpty) {
return 'Destination requise'; return 'Nom du voyage requis';
} }
return null; return null;
}, },
decoration: InputDecoration( ),
labelText: 'Destination *', const SizedBox(height: 20),
hintText: 'ex: Paris, France',
border: OutlineInputBorder( // Destination
borderRadius: BorderRadius.circular(12), Text(
), 'Destination',
prefixIcon: const Icon(Icons.location_on), style: theme.textTheme.titleMedium?.copyWith(
suffixIcon: _isLoadingSuggestions fontWeight: FontWeight.w600,
? const SizedBox( color: theme.colorScheme.onSurface,
width: 20,
height: 20,
child: CircularProgressIndicator(strokeWidth: 2),
)
: null,
), ),
), ),
), const SizedBox(height: 12),
CompositedTransformTarget(
const SizedBox(height: 16), link: _layerLink,
child: _buildModernTextField(
// Aperçu de l'image du lieu controller: _locationController,
if (_isLoadingImage || _selectedImageUrl != null) ...[ label: 'Rechercher une ville ou un pays',
_buildSectionTitle('Aperçu de la destination'), icon: Icons.public,
const SizedBox(height: 8), validator: (value) {
Container( if (value == null || value.trim().isEmpty) {
width: double.infinity, return 'Destination requise';
height: 200, }
decoration: BoxDecoration( return null;
borderRadius: BorderRadius.circular(12), },
border: Border.all(color: Colors.grey[300]!), suffixIcon: _isLoadingSuggestions
? const SizedBox(
width: 20,
height: 20,
child: CircularProgressIndicator(strokeWidth: 2),
)
: null,
), ),
child: _isLoadingImage ),
? const Center( const SizedBox(height: 20),
child: Column(
mainAxisAlignment: MainAxisAlignment.center, // Description
children: [ Text(
CircularProgressIndicator(), 'Description (Optionnel)',
SizedBox(height: 8), style: theme.textTheme.titleMedium?.copyWith(
Text('Chargement de l\'image...'), fontWeight: FontWeight.w600,
], color: theme.colorScheme.onSurface,
),
),
const SizedBox(height: 12),
_buildModernTextField(
controller: _descriptionController,
label: 'Décris ton voyage en quelques mots',
icon: Icons.description,
maxLines: 4,
),
const SizedBox(height: 20),
// Dates
Row(
children: [
Expanded(
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Text(
'Début du voyage',
style: theme.textTheme.titleMedium?.copyWith(
fontWeight: FontWeight.w600,
color: theme.colorScheme.onSurface,
),
), ),
) const SizedBox(height: 12),
: _selectedImageUrl != null _buildDateField(
? ClipRRect( date: _startDate,
borderRadius: BorderRadius.circular(12), onTap: () => _selectStartDate(context),
child: Image.network( ),
_selectedImageUrl!, ],
width: double.infinity,
height: 200,
fit: BoxFit.cover,
errorBuilder: (context, error, stackTrace) {
return Container(
width: double.infinity,
height: 200,
decoration: BoxDecoration(
color: Colors.grey[200],
borderRadius: BorderRadius.circular(12),
),
child: const Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
Icon(Icons.error, color: Colors.grey),
Text('Erreur de chargement'),
],
),
),
);
},
),
)
: const SizedBox(),
),
const SizedBox(height: 16),
],
const SizedBox(height: 24),
_buildSectionTitle('Dates du voyage'),
const SizedBox(height: 16),
Row(
children: [
Expanded(
child: _buildDateField(
label: 'Date de début *',
date: _startDate,
onTap: () => _selectStartDate(context),
),
),
const SizedBox(width: 16),
Expanded(
child: _buildDateField(
label: 'Date de fin *',
date: _endDate,
onTap: () => _selectEndDate(context),
),
),
],
),
const SizedBox(height: 24),
_buildSectionTitle('Budget'),
const SizedBox(height: 16),
TextFormField(
controller: _budgetController,
keyboardType: TextInputType.numberWithOptions(decimal: true),
decoration: InputDecoration(
labelText: 'Budget estimé',
hintText: 'ex: 1200.50',
border: OutlineInputBorder(
borderRadius: BorderRadius.circular(12),
),
prefixIcon: const Icon(Icons.euro),
suffixText: '',
),
),
const SizedBox(height: 24),
_buildSectionTitle('Participants'),
const SizedBox(height: 8),
Text(
'Ajoutez les emails des personnes que vous souhaitez inviter',
style: TextStyle(color: Colors.grey[600], fontSize: 14),
),
const SizedBox(height: 16),
Row(
children: [
Expanded(
child: TextFormField(
controller: _participantController,
keyboardType: TextInputType.emailAddress,
decoration: InputDecoration(
labelText: 'Email du participant',
hintText: 'ex: ami@email.com',
border: OutlineInputBorder(
borderRadius: BorderRadius.circular(12),
),
prefixIcon: const Icon(Icons.person_add),
), ),
), ),
const SizedBox(width: 16),
Expanded(
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Text(
'Fin du voyage',
style: theme.textTheme.titleMedium?.copyWith(
fontWeight: FontWeight.w600,
color: theme.colorScheme.onSurface,
),
),
const SizedBox(height: 12),
_buildDateField(
date: _endDate,
onTap: () => _selectEndDate(context),
),
],
),
),
],
),
const SizedBox(height: 20),
// Budget
Text(
'Budget estimé par personne (€)',
style: theme.textTheme.titleMedium?.copyWith(
fontWeight: FontWeight.w600,
color: theme.colorScheme.onSurface,
), ),
const SizedBox(width: 8), ),
ElevatedButton( const SizedBox(height: 12),
onPressed: _addParticipant, _buildModernTextField(
controller: _budgetController,
label: 'Ex : 500',
icon: Icons.euro,
keyboardType: TextInputType.numberWithOptions(decimal: true),
),
const SizedBox(height: 20),
// Inviter des amis
Text(
'Invite tes amis',
style: theme.textTheme.titleMedium?.copyWith(
fontWeight: FontWeight.w600,
color: theme.colorScheme.onSurface,
),
),
const SizedBox(height: 12),
Row(
children: [
Expanded(
child: _buildModernTextField(
controller: _participantController,
label: 'adresse@email.com',
icon: Icons.alternate_email,
keyboardType: TextInputType.emailAddress,
),
),
const SizedBox(width: 12),
Container(
height: 56,
width: 56,
decoration: BoxDecoration(
color: Colors.teal,
borderRadius: BorderRadius.circular(12),
),
child: IconButton(
onPressed: _addParticipant,
icon: const Icon(Icons.add, color: Colors.white),
),
),
],
),
const SizedBox(height: 16),
// Participants ajoutés
if (_participants.isNotEmpty) ...[
Wrap(
spacing: 8,
runSpacing: 8,
children: _participants.map((email) {
return Container(
padding: const EdgeInsets.symmetric(
horizontal: 12,
vertical: 8,
),
decoration: BoxDecoration(
color: Colors.teal.withOpacity(0.1),
borderRadius: BorderRadius.circular(20),
),
child: Row(
mainAxisSize: MainAxisSize.min,
children: [
Text(
email,
style: theme.textTheme.bodySmall?.copyWith(
color: Colors.teal,
fontWeight: FontWeight.w500,
),
),
const SizedBox(width: 8),
GestureDetector(
onTap: () => _removeParticipant(email),
child: const Icon(
Icons.close,
size: 16,
color: Colors.teal,
),
),
],
),
);
}).toList(),
),
const SizedBox(height: 20),
],
const SizedBox(height: 32),
// Bouton créer
SizedBox(
width: double.infinity,
height: 56,
child: ElevatedButton(
onPressed: _isLoading ? null : () => _saveTrip(userState.user),
style: ElevatedButton.styleFrom( style: ElevatedButton.styleFrom(
backgroundColor: Colors.teal,
foregroundColor: Colors.white,
shape: RoundedRectangleBorder( shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(12), borderRadius: BorderRadius.circular(12),
), ),
padding: const EdgeInsets.all(16), elevation: 0,
), ),
child: const Icon(Icons.add), child: _isLoading
), ? const SizedBox(
], width: 20,
), height: 20,
child: CircularProgressIndicator(
const SizedBox(height: 16), strokeWidth: 2,
valueColor: AlwaysStoppedAnimation<Color>(Colors.white),
if (_participants.isNotEmpty) ...[ ),
Text( )
'Participants ajoutés (${_participants.length})', : Text(
style: const TextStyle(fontWeight: FontWeight.w500), isEditing ? 'Modifier le voyage' : 'Créer le voyage',
), style: theme.textTheme.titleMedium?.copyWith(
const SizedBox(height: 8), color: Colors.white,
Container( fontWeight: FontWeight.w600,
width: double.infinity, ),
padding: const EdgeInsets.all(12),
decoration: BoxDecoration(
border: Border.all(color: Colors.grey[300]!),
borderRadius: BorderRadius.circular(12),
),
child: Wrap(
spacing: 8,
runSpacing: 8,
children: _participants
.map(
(email) => Chip(
label: Text(email, style: const TextStyle(fontSize: 12)),
deleteIcon: const Icon(Icons.close, size: 18),
onDeleted: () => _removeParticipant(email),
backgroundColor: Theme.of(context)
.colorScheme
.primary
.withValues(alpha: 0.1),
), ),
)
.toList(),
), ),
), ),
const SizedBox(height: 20),
], ],
),
const SizedBox(height: 32),
SizedBox(
width: double.infinity,
height: 50,
child: ElevatedButton(
onPressed: _isLoading ? null : () => _saveTrip(userState.user),
style: ElevatedButton.styleFrom(
backgroundColor: Theme.of(context).colorScheme.primary,
foregroundColor: Colors.white,
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(12),
),
),
child: _isLoading
? const CircularProgressIndicator(color: Colors.white)
: Text(
isEditing ? 'Mettre à jour le voyage' : 'Créer le voyage',
style: const TextStyle(
fontSize: 16,
fontWeight: FontWeight.bold,
),
),
),
),
const SizedBox(height: 20),
],
), ),
), ),
), ),
@@ -674,60 +796,6 @@ class _CreateTripContentState extends State<CreateTripContent> {
); );
} }
Widget _buildSectionTitle(String title) {
return Text(
title,
style: TextStyle(
fontSize: 18,
fontWeight: FontWeight.bold,
color: Colors.grey[700],
),
);
}
Widget _buildDateField({
required String label,
required DateTime? date,
required VoidCallback onTap,
}) {
final isDarkMode = Theme.of(context).brightness == Brightness.dark;
final textColor = isDarkMode ? Colors.white : Colors.black;
final labelColor = isDarkMode ? Colors.white70 : Colors.grey[600];
final iconColor = isDarkMode ? Colors.white70 : Colors.grey[600];
final placeholderColor = isDarkMode ? Colors.white38 : Colors.grey[500];
return InkWell(
onTap: onTap,
child: Container(
padding: EdgeInsets.all(16),
decoration: BoxDecoration(
border: Border.all(color: isDarkMode ? Colors.white24 : Colors.grey[400]!),
borderRadius: BorderRadius.circular(12),
),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Text(label, style: TextStyle(fontSize: 12, color: labelColor)),
SizedBox(height: 8),
Row(
children: [
Icon(Icons.calendar_today, size: 16, color: iconColor),
SizedBox(width: 8),
Text(
date != null ? '${date.day}/${date.month}/${date.year}' : 'Sélectionner',
style: TextStyle(
fontSize: 16,
color: date != null ? textColor : placeholderColor,
),
),
],
),
],
),
),
);
}
Future<void> _selectStartDate(BuildContext context) async { Future<void> _selectStartDate(BuildContext context) async {
final DateTime? picked = await showDatePicker( final DateTime? picked = await showDatePicker(
context: context, context: context,
@@ -748,8 +816,9 @@ class _CreateTripContentState extends State<CreateTripContent> {
Future<void> _selectEndDate(BuildContext context) async { Future<void> _selectEndDate(BuildContext context) async {
if (_startDate == null) { if (_startDate == null) {
if (mounted) { if (mounted) {
ScaffoldMessenger.of(context).showSnackBar( _errorService.showSnackbar(
SnackBar(content: Text('Veuillez d\'abord sélectionner la date de début')), message: 'Veuillez d\'abord sélectionner la date de début',
isError: true,
); );
} }
return; return;