feat: Integrate ErrorService for improved error handling and add imageUrl field to Trip model

This commit is contained in:
Van Leemput Dayron
2025-11-03 11:33:25 +01:00
parent 882f7c8963
commit 83aed85fea
3 changed files with 223 additions and 174 deletions

View File

@@ -10,7 +10,7 @@ import '../../blocs/trip/trip_event.dart';
import '../../models/trip.dart';
/// Home content widget for the main application dashboard.
///
///
/// This widget serves as the primary content area of the home screen,
/// displaying user trips and providing navigation to trip management
/// features. Key functionality includes:
@@ -18,7 +18,7 @@ import '../../models/trip.dart';
/// - Creating new trips
/// - Viewing trip details
/// - Managing trip state with proper user authentication
///
///
/// The widget maintains state persistence using AutomaticKeepAliveClientMixin
/// to preserve content when switching between tabs.
class HomeContent extends StatefulWidget {
@@ -29,11 +29,12 @@ class HomeContent extends StatefulWidget {
State<HomeContent> createState() => _HomeContentState();
}
class _HomeContentState extends State<HomeContent> with AutomaticKeepAliveClientMixin {
class _HomeContentState extends State<HomeContent>
with AutomaticKeepAliveClientMixin {
/// Preserves widget state when switching between tabs
@override
bool get wantKeepAlive => true;
/// Flag to prevent duplicate trip loading operations
bool _hasLoadedTrips = false;
@@ -47,7 +48,7 @@ class _HomeContentState extends State<HomeContent> with AutomaticKeepAliveClient
}
/// Loads trips if a user is currently loaded and trips haven't been loaded yet.
///
///
/// Checks the current user state and initiates trip loading if the user is
/// authenticated and trips haven't been loaded previously. This prevents
/// duplicate loading operations.
@@ -56,7 +57,9 @@ class _HomeContentState extends State<HomeContent> with AutomaticKeepAliveClient
final userState = context.read<UserBloc>().state;
if (userState is UserLoaded) {
_hasLoadedTrips = true;
context.read<TripBloc>().add(LoadTripsByUserId(userId: userState.user.id));
context.read<TripBloc>().add(
LoadTripsByUserId(userId: userState.user.id),
);
}
}
}
@@ -64,15 +67,11 @@ class _HomeContentState extends State<HomeContent> with AutomaticKeepAliveClient
@override
Widget build(BuildContext context) {
super.build(context); // Important pour AutomaticKeepAliveClientMixin
return BlocBuilder<UserBloc, UserState>(
builder: (context, userState) {
if (userState is UserLoading) {
return Scaffold(
body: Center(
child: CircularProgressIndicator(),
),
);
return Scaffold(body: Center(child: CircularProgressIndicator()));
}
if (userState is UserError) {
@@ -91,11 +90,7 @@ class _HomeContentState extends State<HomeContent> with AutomaticKeepAliveClient
}
if (userState is! UserLoaded) {
return Scaffold(
body: Center(
child: Text('Veuillez vous connecter'),
),
);
return Scaffold(body: Center(child: Text('Veuillez vous connecter')));
}
final user = userState.user;
@@ -137,7 +132,9 @@ class _HomeContentState extends State<HomeContent> with AutomaticKeepAliveClient
return Scaffold(
body: RefreshIndicator(
onRefresh: () async {
context.read<TripBloc>().add(LoadTripsByUserId(userId: user.id));
context.read<TripBloc>().add(
LoadTripsByUserId(userId: user.id),
);
await Future.delayed(Duration(milliseconds: 500));
},
child: SingleChildScrollView(
@@ -149,21 +146,21 @@ class _HomeContentState extends State<HomeContent> with AutomaticKeepAliveClient
Text(
'Bonjour ${user.prenom} !',
style: TextStyle(
fontSize: 28,
fontSize: 28,
fontWeight: FontWeight.bold,
color: Theme.of(context).brightness == Brightness.dark
? Colors.white
: Colors.black,
color: Theme.of(context).brightness == Brightness.dark
? Colors.white
: Colors.black,
),
),
const SizedBox(height: 8),
Text(
'Vos voyages',
style: TextStyle(
fontSize: 16,
color: Theme.of(context).brightness == Brightness.dark
? Colors.white70
: Colors.grey[600],
fontSize: 16,
color: Theme.of(context).brightness == Brightness.dark
? Colors.white70
: Colors.grey[600],
),
),
const SizedBox(height: 20),
@@ -192,7 +189,9 @@ class _HomeContentState extends State<HomeContent> with AutomaticKeepAliveClient
final result = await Navigator.push(
context,
MaterialPageRoute(builder: (context) => const CreateTripContent()),
MaterialPageRoute(
builder: (context) => const CreateTripContent(),
),
);
if (result == true && mounted) {
@@ -203,7 +202,8 @@ class _HomeContentState extends State<HomeContent> with AutomaticKeepAliveClient
foregroundColor: Colors.white,
child: const Icon(Icons.add),
),
floatingActionButtonLocation: FloatingActionButtonLocation.endFloat,
floatingActionButtonLocation:
FloatingActionButtonLocation.endFloat,
);
},
);
@@ -274,9 +274,7 @@ class _HomeContentState extends State<HomeContent> with AutomaticKeepAliveClient
}
Widget _buildTripsList(List<Trip> trips) {
return Column(
children: trips.map((trip) => _buildTripCard(trip)).toList(),
);
return Column(children: trips.map((trip) => _buildTripCard(trip)).toList());
}
Widget _buildTripCard(Trip trip) {
@@ -287,141 +285,186 @@ class _HomeContentState extends State<HomeContent> with AutomaticKeepAliveClient
return Card(
margin: EdgeInsets.only(bottom: 12),
elevation: 2,
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(12),
),
child: InkWell(
onTap: () async {
final result = await Navigator.push(
context,
MaterialPageRoute(
builder: (context) => ShowTripDetailsContent(trip: trip),
shape: RoundedRectangleBorder(borderRadius: BorderRadius.circular(12)),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
// Image en haut de la carte
Container(
height: 200,
width: double.infinity,
decoration: BoxDecoration(
borderRadius: BorderRadius.only(
topLeft: Radius.circular(12),
topRight: Radius.circular(12),
),
color: Colors.grey[300],
),
);
if (result == true && mounted) {
final userState = context.read<UserBloc>().state;
if (userState is UserLoaded) {
context.read<TripBloc>().add(LoadTripsByUserId(userId: userState.user.id));
}
}
},
borderRadius: BorderRadius.circular(12),
child: Padding(
padding: EdgeInsets.all(16),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Row(
children: [
Expanded(
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Text(
trip.title,
style: TextStyle(
fontSize: 18,
fontWeight: FontWeight.bold,
color: textColor,
child: ClipRRect(
borderRadius: BorderRadius.only(
topLeft: Radius.circular(12),
topRight: Radius.circular(12),
),
child: trip.imageUrl != null && trip.imageUrl!.isNotEmpty
? Image.network(
trip.imageUrl!,
fit: BoxFit.cover,
errorBuilder: (context, error, stackTrace) {
return Container(
color: Colors.grey[300],
child: Icon(
Icons.image_not_supported,
size: 50,
color: Colors.grey[600],
),
),
SizedBox(height: 4),
Row(
children: [
Icon(Icons.location_on, size: 16, color: subtextColor),
SizedBox(width: 4),
Text(
trip.location,
style: TextStyle(color: subtextColor),
),
],
),
],
),
),
Container(
padding: EdgeInsets.symmetric(horizontal: 12, vertical: 6),
decoration: BoxDecoration(
color: _getStatusColor(trip).withValues(alpha: 0.2),
borderRadius: BorderRadius.circular(12),
),
child: Text(
_getStatusText(trip),
style: TextStyle(
color: _getStatusColor(trip),
fontWeight: FontWeight.bold,
fontSize: 12,
);
},
)
: Container(
color: Colors.grey[300],
child: Icon(
Icons.travel_explore,
size: 50,
color: Colors.grey[600],
),
),
),
),
// Contenu de la carte
Padding(
padding: EdgeInsets.all(16),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
// Nom du voyage
Text(
trip.title,
style: TextStyle(
fontSize: 18,
fontWeight: FontWeight.bold,
color: textColor,
),
],
),
SizedBox(height: 12),
Row(
children: [
Icon(Icons.calendar_today, size: 16, color: subtextColor),
SizedBox(width: 4),
Text(
'${_formatDate(trip.startDate)} - ${_formatDate(trip.endDate)}',
style: TextStyle(fontSize: 14, color: subtextColor),
),
],
),
if (trip.budget != null) ...[
),
SizedBox(height: 8),
// Section dates, participants et bouton
Row(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Icon(Icons.euro, size: 16, color: subtextColor),
SizedBox(width: 4),
Text(
'${trip.budget!.toStringAsFixed(2)}',
style: TextStyle(fontSize: 14, color: subtextColor),
// Colonne gauche : dates et participants
Expanded(
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
// Dates
Row(
children: [
Icon(
Icons.calendar_today,
size: 16,
color: subtextColor,
),
SizedBox(width: 4),
Text(
'${_formatDate(trip.startDate)} - ${_formatDate(trip.endDate)}',
style: TextStyle(
fontSize: 14,
color: subtextColor,
),
),
],
),
SizedBox(height: 8),
// Nombre de participants
Row(
children: [
Icon(Icons.people, size: 16, color: subtextColor),
SizedBox(width: 4),
Text(
'${trip.participants.length} participant${trip.participants.length > 1 ? 's' : ''}',
style: TextStyle(
fontSize: 14,
color: subtextColor,
),
),
],
),
],
),
),
// Bouton "Voir" à droite
ElevatedButton(
onPressed: () async {
final result = await Navigator.push(
context,
MaterialPageRoute(
builder: (context) =>
ShowTripDetailsContent(trip: trip),
),
);
if (result == true && mounted) {
final userState = context.read<UserBloc>().state;
if (userState is UserLoaded) {
context.read<TripBloc>().add(
LoadTripsByUserId(userId: userState.user.id),
);
}
}
},
style: ElevatedButton.styleFrom(
backgroundColor: Theme.of(context).colorScheme.primary,
foregroundColor: Theme.of(
context,
).colorScheme.onPrimary,
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(8),
),
padding: EdgeInsets.symmetric(
horizontal: 16,
vertical: 8,
),
),
child: Text('Voir', style: TextStyle(fontSize: 14)),
),
],
),
],
SizedBox(height: 8),
Row(
children: [
Icon(Icons.people, size: 16, color: subtextColor),
SizedBox(width: 4),
Text(
'${trip.participants.length} participant${trip.participants.length > 1 ? 's' : ''}',
style: TextStyle(fontSize: 14, color: subtextColor),
),
],
),
],
),
),
),
],
),
);
}
Color _getStatusColor(Trip trip) {
final now = DateTime.now();
if (now.isBefore(trip.startDate)) {
return Colors.blue;
} else if (now.isAfter(trip.endDate)) {
return Colors.grey;
} else {
return Colors.green;
}
}
// Color _getStatusColor(Trip trip) {
// final now = DateTime.now();
// if (now.isBefore(trip.startDate)) {
// return Colors.blue;
// } else if (now.isAfter(trip.endDate)) {
// return Colors.grey;
// } else {
// return Colors.green;
// }
// }
String _getStatusText(Trip trip) {
final now = DateTime.now();
if (now.isBefore(trip.startDate)) {
return 'À venir';
} else if (now.isAfter(trip.endDate)) {
return 'Terminé';
} else {
return 'En cours';
}
}
// String _getStatusText(Trip trip) {
// final now = DateTime.now();
// if (now.isBefore(trip.startDate)) {
// return 'À venir';
// } else if (now.isAfter(trip.endDate)) {
// return 'Terminé';
// } else {
// return 'En cours';
// }
// }
String _formatDate(DateTime date) {
return '${date.day}/${date.month}/${date.year}';
return '${date.day.toString().padLeft(2, '0')}/${date.month.toString().padLeft(2, '0')}/${date.year}';
}
}
}

View File

@@ -4,6 +4,7 @@ import 'package:travel_mate/blocs/trip/trip_bloc.dart';
import 'package:travel_mate/blocs/trip/trip_event.dart';
import 'package:travel_mate/components/home/create_trip_content.dart';
import 'package:travel_mate/models/trip.dart';
import 'package:travel_mate/services/error_service.dart';
import 'package:url_launcher/url_launcher.dart'; // Ajouter cet import
import 'package:travel_mate/components/map/map_content.dart'; // Ajouter cet import si la page carte existe
@@ -16,6 +17,8 @@ class ShowTripDetailsContent extends StatefulWidget {
}
class _ShowTripDetailsContentState extends State<ShowTripDetailsContent> {
final ErrorService _errorService = ErrorService();
// Méthode pour ouvrir la carte interne
void _openInternalMap() {
Navigator.push(
@@ -45,13 +48,9 @@ class _ShowTripDetailsContentState extends State<ShowTripDetailsContent> {
} else {
// Si rien ne marche, afficher un message d'erreur
if (mounted) {
ScaffoldMessenger.of(context).showSnackBar(
const SnackBar(
content: Text(
'Impossible d\'ouvrir Google Maps. Vérifiez que l\'application est installée.',
),
backgroundColor: Colors.red,
),
_errorService.showError(
message:
'Impossible d\'ouvrir Google Maps, veuillez vérifier que l\'application est installée.',
);
}
}