Refactor user and theme management to use BLoC pattern; remove provider classes and integrate new services for user and group functionalities
This commit is contained in:
@@ -1,8 +1,11 @@
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:travel_mate/models/group.dart';
|
||||
import 'package:provider/provider.dart';
|
||||
import '../../providers/user_provider.dart';
|
||||
import '../../services/group_service.dart';
|
||||
import 'package:flutter_bloc/flutter_bloc.dart';
|
||||
import 'package:travel_mate/data/models/group.dart';
|
||||
import '../../blocs/user/user_bloc.dart';
|
||||
import '../../blocs/user/user_state.dart' as user_state;
|
||||
import '../../blocs/group/group_bloc.dart';
|
||||
import '../../blocs/group/group_state.dart';
|
||||
import '../../blocs/group/group_event.dart';
|
||||
|
||||
class GroupContent extends StatefulWidget {
|
||||
const GroupContent({super.key});
|
||||
@@ -12,51 +15,82 @@ class GroupContent extends StatefulWidget {
|
||||
}
|
||||
|
||||
class _GroupContentState extends State<GroupContent> {
|
||||
@override
|
||||
void initState() {
|
||||
super.initState();
|
||||
_loadGroupsIfUserLoaded();
|
||||
}
|
||||
|
||||
void _loadGroupsIfUserLoaded() {
|
||||
final userState = context.read<UserBloc>().state;
|
||||
if (userState is user_state.UserLoaded) {
|
||||
context.read<GroupBloc>().add(GroupLoadRequested(userId: userState.user.id));
|
||||
}
|
||||
}
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return Scaffold(
|
||||
body: Consumer<UserProvider>(
|
||||
builder: (context, userProvider, child) {
|
||||
final user = userProvider.currentUser;
|
||||
if (user == null || user.id == null) {
|
||||
return const Center(
|
||||
child: Text('Utilisateur non connecté'),
|
||||
);
|
||||
}
|
||||
return BlocBuilder<UserBloc, user_state.UserState>(
|
||||
builder: (context, userState) {
|
||||
if (userState is user_state.UserLoading) {
|
||||
return Scaffold(
|
||||
body: Center(child: CircularProgressIndicator()),
|
||||
);
|
||||
}
|
||||
|
||||
return StreamBuilder<List<Group>>(
|
||||
stream: GroupService().getGroupsStreamByUser(user.id!), // Filtrer par utilisateur
|
||||
builder: (context, snapshot) {
|
||||
if (snapshot.connectionState == ConnectionState.waiting) {
|
||||
return _buildLoadingState();
|
||||
} else if (snapshot.hasError) {
|
||||
print('Erreur du stream: ${snapshot.error}');
|
||||
return Center(
|
||||
child: Column(
|
||||
mainAxisAlignment: MainAxisAlignment.center,
|
||||
children: [
|
||||
const Text('Erreur lors du chargement des groupes.'),
|
||||
const SizedBox(height: 8),
|
||||
ElevatedButton(
|
||||
onPressed: () {
|
||||
setState(() {
|
||||
// Forcer le rechargement du stream
|
||||
});
|
||||
},
|
||||
child: const Text('Réessayer'),
|
||||
),
|
||||
],
|
||||
),
|
||||
);
|
||||
}
|
||||
if (userState is user_state.UserError) {
|
||||
return Scaffold(
|
||||
body: Center(
|
||||
child: Column(
|
||||
mainAxisAlignment: MainAxisAlignment.center,
|
||||
children: [
|
||||
Icon(Icons.error, size: 64, color: Colors.red),
|
||||
SizedBox(height: 16),
|
||||
Text('Erreur: ${userState.message}'),
|
||||
],
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
final groups = snapshot.data ?? [];
|
||||
print("Groupes reçus pour l'utilisateur ${user.id}: ${groups.length}");
|
||||
if (userState is! user_state.UserLoaded) {
|
||||
return Scaffold(
|
||||
body: Center(child: Text('Utilisateur non connecté')),
|
||||
);
|
||||
}
|
||||
|
||||
return RefreshIndicator(
|
||||
final user = userState.user;
|
||||
|
||||
// Charger les groupes si ce n'est pas déjà fait
|
||||
if (context.read<GroupBloc>().state is GroupInitial) {
|
||||
context.read<GroupBloc>().add(GroupLoadRequested(userId: user.id));
|
||||
}
|
||||
|
||||
return BlocConsumer<GroupBloc, GroupState>(
|
||||
listener: (context, groupState) {
|
||||
if (groupState is GroupOperationSuccess) {
|
||||
ScaffoldMessenger.of(context).showSnackBar(
|
||||
SnackBar(
|
||||
content: Text(groupState.message),
|
||||
backgroundColor: Colors.green,
|
||||
),
|
||||
);
|
||||
// Recharger les groupes
|
||||
context.read<GroupBloc>().add(GroupLoadRequested(userId: user.id));
|
||||
} else if (groupState is GroupError) {
|
||||
ScaffoldMessenger.of(context).showSnackBar(
|
||||
SnackBar(
|
||||
content: Text(groupState.message),
|
||||
backgroundColor: Colors.red,
|
||||
),
|
||||
);
|
||||
}
|
||||
},
|
||||
builder: (context, groupState) {
|
||||
return Scaffold(
|
||||
body: RefreshIndicator(
|
||||
onRefresh: () async {
|
||||
setState(() {});
|
||||
context.read<GroupBloc>().add(GroupLoadRequested(userId: user.id));
|
||||
},
|
||||
child: SingleChildScrollView(
|
||||
physics: const AlwaysScrollableScrollPhysics(),
|
||||
@@ -69,31 +103,74 @@ class _GroupContentState extends State<GroupContent> {
|
||||
style: TextStyle(
|
||||
fontSize: 24,
|
||||
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: 20),
|
||||
groups.isEmpty ? _buildEmptyState() : _buildGroupList(groups),
|
||||
|
||||
if (groupState is GroupLoading)
|
||||
_buildLoadingState()
|
||||
else if (groupState is GroupError)
|
||||
_buildErrorState(groupState.message, user.id)
|
||||
else if (groupState is GroupLoaded)
|
||||
groupState.groups.isEmpty
|
||||
? _buildEmptyState()
|
||||
: _buildGroupList(groupState.groups)
|
||||
else
|
||||
_buildEmptyState(),
|
||||
|
||||
const SizedBox(height: 80),
|
||||
],
|
||||
),
|
||||
),
|
||||
);
|
||||
},
|
||||
);
|
||||
},
|
||||
),
|
||||
),
|
||||
);
|
||||
},
|
||||
);
|
||||
},
|
||||
);
|
||||
}
|
||||
|
||||
Widget _buildLoadingState() {
|
||||
return const Center(
|
||||
child: Padding(padding: EdgeInsets.all(16.0),
|
||||
child: Padding(
|
||||
padding: EdgeInsets.all(16.0),
|
||||
child: CircularProgressIndicator(),
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
Widget _buildErrorState(String error, String userId) {
|
||||
return Center(
|
||||
child: Padding(
|
||||
padding: EdgeInsets.all(32),
|
||||
child: Column(
|
||||
mainAxisAlignment: MainAxisAlignment.center,
|
||||
children: [
|
||||
const Icon(Icons.error, size: 64, color: Colors.red),
|
||||
const SizedBox(height: 16),
|
||||
const Text('Erreur lors du chargement des groupes.'),
|
||||
const SizedBox(height: 8),
|
||||
Text(
|
||||
error,
|
||||
textAlign: TextAlign.center,
|
||||
style: TextStyle(fontSize: 12, color: Colors.grey),
|
||||
),
|
||||
const SizedBox(height: 16),
|
||||
ElevatedButton(
|
||||
onPressed: () {
|
||||
context.read<GroupBloc>().add(GroupLoadRequested(userId: userId));
|
||||
},
|
||||
child: const Text('Réessayer'),
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
Widget _buildEmptyState() {
|
||||
return const Center(
|
||||
child: Text(
|
||||
|
||||
@@ -1,11 +1,14 @@
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:provider/provider.dart';
|
||||
import 'package:flutter_bloc/flutter_bloc.dart';
|
||||
import 'package:travel_mate/data/models/trip.dart';
|
||||
import 'package:travel_mate/providers/user_provider.dart';
|
||||
import 'package:travel_mate/services/trip_service.dart';
|
||||
import 'package:travel_mate/services/group_service.dart';
|
||||
import 'package:travel_mate/data/models/group.dart';
|
||||
|
||||
import '../../blocs/user/user_bloc.dart';
|
||||
import '../../blocs/user/user_state.dart' as user_state;
|
||||
import '../../blocs/trip/trip_bloc.dart';
|
||||
import '../../blocs/trip/trip_event.dart';
|
||||
import '../../blocs/group/group_bloc.dart';
|
||||
import '../../blocs/group/group_event.dart';
|
||||
import '../../data/models/group.dart';
|
||||
import '../../services/user_service.dart';
|
||||
|
||||
class CreateTripContent extends StatefulWidget {
|
||||
const CreateTripContent({super.key});
|
||||
@@ -20,12 +23,12 @@ class _CreateTripContentState extends State<CreateTripContent> {
|
||||
final _descriptionController = TextEditingController();
|
||||
final _locationController = TextEditingController();
|
||||
final _budgetController = TextEditingController();
|
||||
final _userService = UserService();
|
||||
|
||||
DateTime? _startDate;
|
||||
DateTime? _endDate;
|
||||
bool _isLoading = false;
|
||||
|
||||
// Liste des participants (emails)
|
||||
final List<String> _participants = [];
|
||||
final _participantController = TextEditingController();
|
||||
|
||||
@@ -41,233 +44,235 @@ class _CreateTripContentState extends State<CreateTripContent> {
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return Scaffold(
|
||||
appBar: AppBar(
|
||||
title: Text('Créer un voyage'),
|
||||
backgroundColor: Theme.of(context).colorScheme.primary,
|
||||
foregroundColor: Colors.white,
|
||||
),
|
||||
body: SingleChildScrollView(
|
||||
padding: EdgeInsets.all(16),
|
||||
child: Form(
|
||||
key: _formKey,
|
||||
child: Column(
|
||||
crossAxisAlignment: CrossAxisAlignment.start,
|
||||
children: [
|
||||
// Titre du voyage
|
||||
_buildSectionTitle('Informations générales'),
|
||||
SizedBox(height: 16),
|
||||
return BlocBuilder<UserBloc, user_state.UserState>(
|
||||
builder: (context, userState) {
|
||||
if (userState is! user_state.UserLoaded) {
|
||||
return Scaffold(
|
||||
appBar: AppBar(title: Text('Créer un voyage')),
|
||||
body: Center(child: Text('Veuillez vous connecter')),
|
||||
);
|
||||
}
|
||||
|
||||
TextFormField(
|
||||
controller: _titleController,
|
||||
validator: (value) {
|
||||
if (value == null || value.trim().isEmpty) {
|
||||
return 'Titre requis';
|
||||
}
|
||||
return null;
|
||||
},
|
||||
decoration: InputDecoration(
|
||||
labelText: 'Titre du voyage *',
|
||||
hintText: 'ex: Voyage à Paris',
|
||||
border: OutlineInputBorder(
|
||||
borderRadius: BorderRadius.circular(12),
|
||||
),
|
||||
prefixIcon: Icon(Icons.travel_explore),
|
||||
),
|
||||
),
|
||||
|
||||
SizedBox(height: 16),
|
||||
|
||||
// Description
|
||||
TextFormField(
|
||||
controller: _descriptionController,
|
||||
maxLines: 3,
|
||||
decoration: InputDecoration(
|
||||
labelText: 'Description',
|
||||
hintText: 'Décrivez votre voyage...',
|
||||
border: OutlineInputBorder(
|
||||
borderRadius: BorderRadius.circular(12),
|
||||
),
|
||||
prefixIcon: Icon(Icons.description),
|
||||
),
|
||||
),
|
||||
|
||||
SizedBox(height: 16),
|
||||
|
||||
// Destination
|
||||
TextFormField(
|
||||
controller: _locationController,
|
||||
validator: (value) {
|
||||
if (value == null || value.trim().isEmpty) {
|
||||
return 'Destination requise';
|
||||
}
|
||||
return null;
|
||||
},
|
||||
decoration: InputDecoration(
|
||||
labelText: 'Destination *',
|
||||
hintText: 'ex: Paris, France',
|
||||
border: OutlineInputBorder(
|
||||
borderRadius: BorderRadius.circular(12),
|
||||
),
|
||||
prefixIcon: Icon(Icons.location_on),
|
||||
),
|
||||
),
|
||||
|
||||
SizedBox(height: 24),
|
||||
|
||||
// Dates
|
||||
_buildSectionTitle('Dates du voyage'),
|
||||
SizedBox(height: 16),
|
||||
|
||||
Row(
|
||||
return Scaffold(
|
||||
appBar: AppBar(
|
||||
title: Text('Créer un voyage'),
|
||||
backgroundColor: Theme.of(context).colorScheme.primary,
|
||||
foregroundColor: Colors.white,
|
||||
),
|
||||
body: SingleChildScrollView(
|
||||
padding: EdgeInsets.all(16),
|
||||
child: Form(
|
||||
key: _formKey,
|
||||
child: Column(
|
||||
crossAxisAlignment: CrossAxisAlignment.start,
|
||||
children: [
|
||||
Expanded(
|
||||
child: _buildDateField(
|
||||
label: 'Date de début *',
|
||||
date: _startDate,
|
||||
onTap: () => _selectStartDate(context),
|
||||
),
|
||||
),
|
||||
SizedBox(width: 16),
|
||||
Expanded(
|
||||
child: _buildDateField(
|
||||
label: 'Date de fin *',
|
||||
date: _endDate,
|
||||
onTap: () => _selectEndDate(context),
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
_buildSectionTitle('Informations générales'),
|
||||
SizedBox(height: 16),
|
||||
|
||||
SizedBox(height: 24),
|
||||
|
||||
// Budget
|
||||
_buildSectionTitle('Budget'),
|
||||
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: Icon(Icons.euro),
|
||||
suffixText: '€',
|
||||
),
|
||||
),
|
||||
|
||||
SizedBox(height: 24),
|
||||
|
||||
// Participants
|
||||
_buildSectionTitle('Participants'),
|
||||
SizedBox(height: 8),
|
||||
Text(
|
||||
'Ajoutez les emails des personnes que vous souhaitez inviter',
|
||||
style: TextStyle(color: Colors.grey[600], fontSize: 14),
|
||||
),
|
||||
SizedBox(height: 16),
|
||||
|
||||
// Champ d'ajout de participant
|
||||
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: Icon(Icons.person_add),
|
||||
),
|
||||
),
|
||||
),
|
||||
SizedBox(width: 8),
|
||||
ElevatedButton(
|
||||
onPressed: _addParticipant,
|
||||
style: ElevatedButton.styleFrom(
|
||||
shape: RoundedRectangleBorder(
|
||||
TextFormField(
|
||||
controller: _titleController,
|
||||
validator: (value) {
|
||||
if (value == null || value.trim().isEmpty) {
|
||||
return 'Titre requis';
|
||||
}
|
||||
return null;
|
||||
},
|
||||
decoration: InputDecoration(
|
||||
labelText: 'Titre du voyage *',
|
||||
hintText: 'ex: Voyage à Paris',
|
||||
border: OutlineInputBorder(
|
||||
borderRadius: BorderRadius.circular(12),
|
||||
),
|
||||
padding: EdgeInsets.all(16),
|
||||
),
|
||||
child: Icon(Icons.add),
|
||||
),
|
||||
],
|
||||
),
|
||||
|
||||
SizedBox(height: 16),
|
||||
|
||||
// Liste des participants ajoutés
|
||||
if (_participants.isNotEmpty) ...[
|
||||
Text(
|
||||
'Participants ajoutés (${_participants.length})',
|
||||
style: TextStyle(fontWeight: FontWeight.w500),
|
||||
),
|
||||
SizedBox(height: 8),
|
||||
Container(
|
||||
width: double.infinity,
|
||||
padding: 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: TextStyle(fontSize: 12)),
|
||||
deleteIcon: Icon(Icons.close, size: 18),
|
||||
onDeleted: () => _removeParticipant(email),
|
||||
backgroundColor: Theme.of(
|
||||
context,
|
||||
).colorScheme.primary.withAlpha(25),
|
||||
),
|
||||
)
|
||||
.toList(),
|
||||
),
|
||||
),
|
||||
],
|
||||
|
||||
SizedBox(height: 32),
|
||||
|
||||
// Bouton de création
|
||||
SizedBox(
|
||||
width: double.infinity,
|
||||
height: 50,
|
||||
child: ElevatedButton(
|
||||
onPressed: _isLoading ? null : _saveTrip,
|
||||
style: ElevatedButton.styleFrom(
|
||||
backgroundColor: Theme.of(context).colorScheme.primary,
|
||||
foregroundColor: Colors.white,
|
||||
shape: RoundedRectangleBorder(
|
||||
borderRadius: BorderRadius.circular(12),
|
||||
prefixIcon: Icon(Icons.travel_explore),
|
||||
),
|
||||
),
|
||||
child: _isLoading
|
||||
? CircularProgressIndicator(color: Colors.white)
|
||||
: Text(
|
||||
'Créer le voyage',
|
||||
style: TextStyle(
|
||||
fontSize: 16,
|
||||
fontWeight: FontWeight.bold,
|
||||
|
||||
SizedBox(height: 16),
|
||||
|
||||
TextFormField(
|
||||
controller: _descriptionController,
|
||||
maxLines: 3,
|
||||
decoration: InputDecoration(
|
||||
labelText: 'Description',
|
||||
hintText: 'Décrivez votre voyage...',
|
||||
border: OutlineInputBorder(
|
||||
borderRadius: BorderRadius.circular(12),
|
||||
),
|
||||
prefixIcon: Icon(Icons.description),
|
||||
),
|
||||
),
|
||||
|
||||
SizedBox(height: 16),
|
||||
|
||||
TextFormField(
|
||||
controller: _locationController,
|
||||
validator: (value) {
|
||||
if (value == null || value.trim().isEmpty) {
|
||||
return 'Destination requise';
|
||||
}
|
||||
return null;
|
||||
},
|
||||
decoration: InputDecoration(
|
||||
labelText: 'Destination *',
|
||||
hintText: 'ex: Paris, France',
|
||||
border: OutlineInputBorder(
|
||||
borderRadius: BorderRadius.circular(12),
|
||||
),
|
||||
prefixIcon: Icon(Icons.location_on),
|
||||
),
|
||||
),
|
||||
|
||||
SizedBox(height: 24),
|
||||
|
||||
_buildSectionTitle('Dates du voyage'),
|
||||
SizedBox(height: 16),
|
||||
|
||||
Row(
|
||||
children: [
|
||||
Expanded(
|
||||
child: _buildDateField(
|
||||
label: 'Date de début *',
|
||||
date: _startDate,
|
||||
onTap: () => _selectStartDate(context),
|
||||
),
|
||||
),
|
||||
SizedBox(width: 16),
|
||||
Expanded(
|
||||
child: _buildDateField(
|
||||
label: 'Date de fin *',
|
||||
date: _endDate,
|
||||
onTap: () => _selectEndDate(context),
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
|
||||
SizedBox(height: 24),
|
||||
|
||||
_buildSectionTitle('Budget'),
|
||||
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: Icon(Icons.euro),
|
||||
suffixText: '€',
|
||||
),
|
||||
),
|
||||
|
||||
SizedBox(height: 24),
|
||||
|
||||
_buildSectionTitle('Participants'),
|
||||
SizedBox(height: 8),
|
||||
Text(
|
||||
'Ajoutez les emails des personnes que vous souhaitez inviter',
|
||||
style: TextStyle(color: Colors.grey[600], fontSize: 14),
|
||||
),
|
||||
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: Icon(Icons.person_add),
|
||||
),
|
||||
),
|
||||
),
|
||||
),
|
||||
),
|
||||
SizedBox(width: 8),
|
||||
ElevatedButton(
|
||||
onPressed: _addParticipant,
|
||||
style: ElevatedButton.styleFrom(
|
||||
shape: RoundedRectangleBorder(
|
||||
borderRadius: BorderRadius.circular(12),
|
||||
),
|
||||
padding: EdgeInsets.all(16),
|
||||
),
|
||||
child: Icon(Icons.add),
|
||||
),
|
||||
],
|
||||
),
|
||||
|
||||
SizedBox(height: 20),
|
||||
],
|
||||
SizedBox(height: 16),
|
||||
|
||||
if (_participants.isNotEmpty) ...[
|
||||
Text(
|
||||
'Participants ajoutés (${_participants.length})',
|
||||
style: TextStyle(fontWeight: FontWeight.w500),
|
||||
),
|
||||
SizedBox(height: 8),
|
||||
Container(
|
||||
width: double.infinity,
|
||||
padding: 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: TextStyle(fontSize: 12)),
|
||||
deleteIcon: Icon(Icons.close, size: 18),
|
||||
onDeleted: () => _removeParticipant(email),
|
||||
backgroundColor: Theme.of(
|
||||
context,
|
||||
).colorScheme.primary.withAlpha(25),
|
||||
),
|
||||
)
|
||||
.toList(),
|
||||
),
|
||||
),
|
||||
],
|
||||
|
||||
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
|
||||
? CircularProgressIndicator(color: Colors.white)
|
||||
: Text(
|
||||
'Créer le voyage',
|
||||
style: TextStyle(
|
||||
fontSize: 16,
|
||||
fontWeight: FontWeight.bold,
|
||||
),
|
||||
),
|
||||
),
|
||||
),
|
||||
|
||||
SizedBox(height: 20),
|
||||
],
|
||||
),
|
||||
),
|
||||
),
|
||||
),
|
||||
),
|
||||
);
|
||||
},
|
||||
);
|
||||
}
|
||||
|
||||
@@ -287,7 +292,6 @@ class _CreateTripContentState extends State<CreateTripContent> {
|
||||
required DateTime? date,
|
||||
required VoidCallback onTap,
|
||||
}) {
|
||||
// Détecter le thème actuel
|
||||
final isDarkMode = Theme.of(context).brightness == Brightness.dark;
|
||||
final textColor = isDarkMode ? Colors.white : Colors.black;
|
||||
final labelColor = isDarkMode ? Colors.white70 : Colors.grey[600];
|
||||
@@ -305,10 +309,7 @@ class _CreateTripContentState extends State<CreateTripContent> {
|
||||
child: Column(
|
||||
crossAxisAlignment: CrossAxisAlignment.start,
|
||||
children: [
|
||||
Text(
|
||||
label,
|
||||
style: TextStyle(fontSize: 12, color: labelColor),
|
||||
),
|
||||
Text(label, style: TextStyle(fontSize: 12, color: labelColor)),
|
||||
SizedBox(height: 8),
|
||||
Row(
|
||||
children: [
|
||||
@@ -352,9 +353,7 @@ class _CreateTripContentState extends State<CreateTripContent> {
|
||||
if (_startDate == null) {
|
||||
if (mounted) {
|
||||
ScaffoldMessenger.of(context).showSnackBar(
|
||||
SnackBar(
|
||||
content: Text('Veuillez d\'abord sélectionner la date de début'),
|
||||
),
|
||||
SnackBar(content: Text('Veuillez d\'abord sélectionner la date de début')),
|
||||
);
|
||||
}
|
||||
return;
|
||||
@@ -377,7 +376,6 @@ class _CreateTripContentState extends State<CreateTripContent> {
|
||||
final email = _participantController.text.trim();
|
||||
if (email.isEmpty) return;
|
||||
|
||||
// Validation email simple
|
||||
final emailRegex = RegExp(r'^[\w-\.]+@([\w-]+\.)+[\w-]{2,4}$');
|
||||
if (!emailRegex.hasMatch(email)) {
|
||||
if (mounted) {
|
||||
@@ -388,7 +386,6 @@ class _CreateTripContentState extends State<CreateTripContent> {
|
||||
return;
|
||||
}
|
||||
|
||||
// Vérifier si l'email existe déjà
|
||||
if (_participants.contains(email)) {
|
||||
if (mounted) {
|
||||
ScaffoldMessenger.of(context).showSnackBar(
|
||||
@@ -410,66 +407,7 @@ class _CreateTripContentState extends State<CreateTripContent> {
|
||||
});
|
||||
}
|
||||
|
||||
Future<bool> _saveGroup(String currentUserId) async {
|
||||
if (!_formKey.currentState!.validate()) {
|
||||
return false;
|
||||
}
|
||||
|
||||
// Convertir les emails en IDs
|
||||
final participantIds = await _changeUserEmailById(_participants);
|
||||
|
||||
// Créer la liste des membres incluant le créateur
|
||||
List<String> allMembers = [currentUserId];
|
||||
|
||||
// Ajouter tous les participants (éviter les doublons)
|
||||
for (String participantId in participantIds) {
|
||||
if (!allMembers.contains(participantId)) {
|
||||
allMembers.add(participantId);
|
||||
}
|
||||
}
|
||||
|
||||
print('Membres du groupe: $allMembers');
|
||||
|
||||
final group = Group(
|
||||
id: '',
|
||||
name: _titleController.text.trim(),
|
||||
members: allMembers, // Contient le créateur + tous les participants
|
||||
);
|
||||
|
||||
final groupService = GroupService();
|
||||
bool success = await groupService.createGroup(group);
|
||||
print('Groupe créé avec succès: $success');
|
||||
return success;
|
||||
}
|
||||
|
||||
Future<List<String>> _changeUserEmailById(List<String> participants) async {
|
||||
final userProvider = Provider.of<UserProvider>(context, listen: false);
|
||||
List<String> ids = [];
|
||||
|
||||
for (String email in participants) {
|
||||
try {
|
||||
final id = await userProvider.getUserIdByEmail(email);
|
||||
if (id != null) {
|
||||
ids.add(id);
|
||||
} else {
|
||||
print('Utilisateur non trouvé pour l\'ID: $email');
|
||||
ScaffoldMessenger.of(context).showSnackBar(
|
||||
SnackBar(
|
||||
content: Text('Utilisateur non trouvé pour l\'email: $email'),
|
||||
backgroundColor: Colors.orange,
|
||||
duration: Duration(seconds: 2),
|
||||
),
|
||||
);
|
||||
}
|
||||
} catch (e) {
|
||||
print('Erreur lors de la récupération de l\'utilisateur $email: $e');
|
||||
}
|
||||
}
|
||||
|
||||
return ids;
|
||||
}
|
||||
|
||||
Future<void> _saveTrip() async {
|
||||
Future<void> _saveTrip(user_state.UserModel currentUser) async {
|
||||
if (!_formKey.currentState!.validate()) {
|
||||
return;
|
||||
}
|
||||
@@ -488,68 +426,48 @@ class _CreateTripContentState extends State<CreateTripContent> {
|
||||
});
|
||||
|
||||
try {
|
||||
final userProvider = Provider.of<UserProvider>(context, listen: false);
|
||||
final currentUser = userProvider.currentUser;
|
||||
|
||||
if (currentUser == null || currentUser.id == null) {
|
||||
throw Exception('Utilisateur non connecté');
|
||||
}
|
||||
|
||||
print('Création du voyage par: ${currentUser.id} (${currentUser.email})');
|
||||
|
||||
// Convertir les emails en IDs utilisateur
|
||||
// Convertir les emails en IDs
|
||||
List<String> participantIds = await _changeUserEmailById(_participants);
|
||||
|
||||
// Ajouter le créateur aux participants s'il n'y est pas déjà
|
||||
if (!participantIds.contains(currentUser.id!)) {
|
||||
participantIds.insert(0, currentUser.id!);
|
||||
// Ajouter le créateur
|
||||
if (!participantIds.contains(currentUser.id)) {
|
||||
participantIds.insert(0, currentUser.id);
|
||||
}
|
||||
|
||||
print('Participants IDs (avec créateur): $participantIds');
|
||||
|
||||
// Créer l'objet Trip avec les IDs des participants
|
||||
// Créer le voyage
|
||||
final trip = Trip(
|
||||
id: '', // Sera généré par Firebase
|
||||
id: '',
|
||||
title: _titleController.text.trim(),
|
||||
description: _descriptionController.text.trim(),
|
||||
location: _locationController.text.trim(),
|
||||
startDate: _startDate!,
|
||||
endDate: _endDate!,
|
||||
budget: double.tryParse(_budgetController.text) ?? 0.0,
|
||||
createdBy: currentUser.id!,
|
||||
participants: participantIds, // Contient le créateur + tous les participants
|
||||
createdBy: currentUser.id,
|
||||
participants: participantIds,
|
||||
createdAt: DateTime.now(),
|
||||
updatedAt: DateTime.now(),
|
||||
);
|
||||
|
||||
print('Données du voyage: ${trip.toMap()}');
|
||||
// Créer le groupe
|
||||
final group = Group(
|
||||
id: '',
|
||||
name: _titleController.text.trim(),
|
||||
members: participantIds,
|
||||
);
|
||||
|
||||
// Sauvegarder le voyage
|
||||
final tripService = TripService();
|
||||
final success = await tripService.addTrip(trip);
|
||||
// Utiliser les BLoCs pour créer
|
||||
context.read<TripBloc>().add(TripCreateRequested(trip: trip));
|
||||
context.read<GroupBloc>().add(GroupCreateRequested(group: group));
|
||||
|
||||
// Créer le groupe associé au voyage avec le créateur inclus
|
||||
final successGroup = await _saveGroup(currentUser.id!);
|
||||
|
||||
if (success && successGroup && mounted) {
|
||||
print('Voyage et groupe créés avec succès !');
|
||||
ScaffoldMessenger.of(context).showSnackBar(
|
||||
SnackBar(
|
||||
content: Text('Voyage créé avec succès !'),
|
||||
backgroundColor: Colors.green,
|
||||
),
|
||||
);
|
||||
|
||||
Navigator.pop(context, true); // Retourner true pour indiquer le succès
|
||||
} else {
|
||||
throw Exception('Erreur lors de la sauvegarde');
|
||||
if (mounted) {
|
||||
Navigator.pop(context, true);
|
||||
}
|
||||
} catch (e) {
|
||||
print('Erreur lors de la création: $e');
|
||||
if (mounted) {
|
||||
ScaffoldMessenger.of(context).showSnackBar(
|
||||
SnackBar(
|
||||
content: Text('Erreur lors de la création: $e'),
|
||||
content: Text('Erreur: $e'),
|
||||
backgroundColor: Colors.red,
|
||||
),
|
||||
);
|
||||
@@ -562,4 +480,30 @@ class _CreateTripContentState extends State<CreateTripContent> {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Future<List<String>> _changeUserEmailById(List<String> participants) async {
|
||||
List<String> ids = [];
|
||||
|
||||
for (String email in participants) {
|
||||
try {
|
||||
final id = await _userService.getUserIdByEmail(email);
|
||||
if (id != null) {
|
||||
ids.add(id);
|
||||
} else {
|
||||
if (mounted) {
|
||||
ScaffoldMessenger.of(context).showSnackBar(
|
||||
SnackBar(
|
||||
content: Text('Utilisateur non trouvé: $email'),
|
||||
backgroundColor: Colors.orange,
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
} catch (e) {
|
||||
print('Erreur: $e');
|
||||
}
|
||||
}
|
||||
|
||||
return ids;
|
||||
}
|
||||
}
|
||||
@@ -1,10 +1,13 @@
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:provider/provider.dart';
|
||||
import 'package:flutter_bloc/flutter_bloc.dart';
|
||||
import 'package:travel_mate/components/home/create_trip_content.dart';
|
||||
import '../../providers/user_provider.dart';
|
||||
import '../../services/trip_service.dart';
|
||||
import '../../data/models/trip.dart';
|
||||
import '../home/show_trip_details_content.dart';
|
||||
import '../../blocs/user/user_bloc.dart';
|
||||
import '../../blocs/user/user_state.dart';
|
||||
import '../../blocs/trip/trip_bloc.dart';
|
||||
import '../../blocs/trip/trip_state.dart';
|
||||
import '../../blocs/trip/trip_event.dart';
|
||||
import '../../data/models/trip.dart';
|
||||
|
||||
class HomeContent extends StatefulWidget {
|
||||
const HomeContent({super.key});
|
||||
@@ -14,61 +17,87 @@ class HomeContent extends StatefulWidget {
|
||||
}
|
||||
|
||||
class _HomeContentState extends State<HomeContent> {
|
||||
final TripService _tripService = TripService();
|
||||
@override
|
||||
void initState() {
|
||||
super.initState();
|
||||
// Charger les trips quand le widget est initialisé
|
||||
_loadTripsIfUserLoaded();
|
||||
}
|
||||
|
||||
void _loadTripsIfUserLoaded() {
|
||||
final userState = context.read<UserBloc>().state;
|
||||
if (userState is UserLoaded) {
|
||||
context.read<TripBloc>().add(TripLoadRequested(userId: userState.user.id));
|
||||
}
|
||||
}
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return Scaffold(
|
||||
body: Consumer<UserProvider>(
|
||||
builder: (context, userProvider, child) {
|
||||
final user = userProvider.currentUser;
|
||||
|
||||
if (user == null || user.id == null) {
|
||||
return Center(
|
||||
child: Text('Utilisateur non connecté'),
|
||||
);
|
||||
}
|
||||
|
||||
return StreamBuilder<List<Trip>>(
|
||||
stream: _tripService.getTripsStreamByUser(user.id!, user.email),
|
||||
builder: (context, snapshot) {
|
||||
print('StreamBuilder - ConnectionState: ${snapshot.connectionState}');
|
||||
print('StreamBuilder - HasError: ${snapshot.hasError}');
|
||||
print('StreamBuilder - Data: ${snapshot.data?.length ?? 0} trips');
|
||||
|
||||
if (snapshot.connectionState == ConnectionState.waiting) {
|
||||
return _buildLoadingState();
|
||||
}
|
||||
|
||||
if (snapshot.hasError) {
|
||||
print('Erreur du stream: ${snapshot.error}');
|
||||
return Center(
|
||||
child: Column(
|
||||
mainAxisAlignment: MainAxisAlignment.center,
|
||||
children: [
|
||||
Icon(Icons.error, size: 64, color: Colors.red),
|
||||
SizedBox(height: 16),
|
||||
Text('Erreur lors du chargement des voyages'),
|
||||
SizedBox(height: 8),
|
||||
Text('${snapshot.error}'),
|
||||
SizedBox(height: 8),
|
||||
ElevatedButton(
|
||||
onPressed: () {
|
||||
setState(() {}); // Forcer le rebuild
|
||||
},
|
||||
child: Text('Réessayer'),
|
||||
),
|
||||
],
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
final trips = snapshot.data ?? [];
|
||||
print('Trips reçus du stream: ${trips.length}');
|
||||
|
||||
return RefreshIndicator(
|
||||
return BlocBuilder<UserBloc, UserState>(
|
||||
builder: (context, userState) {
|
||||
if (userState is UserLoading) {
|
||||
return Scaffold(
|
||||
body: Center(
|
||||
child: CircularProgressIndicator(),
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
if (userState is UserError) {
|
||||
return Scaffold(
|
||||
body: Center(
|
||||
child: Column(
|
||||
mainAxisAlignment: MainAxisAlignment.center,
|
||||
children: [
|
||||
Icon(Icons.error, size: 64, color: Colors.red),
|
||||
SizedBox(height: 16),
|
||||
Text('Erreur: ${userState.message}'),
|
||||
],
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
if (userState is! UserLoaded) {
|
||||
return Scaffold(
|
||||
body: Center(
|
||||
child: Text('Veuillez vous connecter'),
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
final user = userState.user;
|
||||
|
||||
// Charger les trips si ce n'est pas déjà fait
|
||||
if (context.read<TripBloc>().state is TripInitial) {
|
||||
context.read<TripBloc>().add(TripLoadRequested(userId: user.id));
|
||||
}
|
||||
|
||||
return BlocConsumer<TripBloc, TripState>(
|
||||
listener: (context, tripState) {
|
||||
if (tripState is TripOperationSuccess) {
|
||||
ScaffoldMessenger.of(context).showSnackBar(
|
||||
SnackBar(
|
||||
content: Text(tripState.message),
|
||||
backgroundColor: Colors.green,
|
||||
),
|
||||
);
|
||||
// Recharger les trips après une opération réussie
|
||||
context.read<TripBloc>().add(TripLoadRequested(userId: user.id));
|
||||
} else if (tripState is TripError) {
|
||||
ScaffoldMessenger.of(context).showSnackBar(
|
||||
SnackBar(
|
||||
content: Text(tripState.message),
|
||||
backgroundColor: Colors.red,
|
||||
),
|
||||
);
|
||||
}
|
||||
},
|
||||
builder: (context, tripState) {
|
||||
return Scaffold(
|
||||
body: RefreshIndicator(
|
||||
onRefresh: () async {
|
||||
setState(() {}); // Forcer le rebuild du stream
|
||||
context.read<TripBloc>().add(TripLoadRequested(userId: user.id));
|
||||
},
|
||||
child: SingleChildScrollView(
|
||||
physics: AlwaysScrollableScrollPhysics(),
|
||||
@@ -88,43 +117,47 @@ class _HomeContentState extends State<HomeContent> {
|
||||
),
|
||||
SizedBox(height: 20),
|
||||
|
||||
// Contenu principal
|
||||
trips.isEmpty ? _buildEmptyState() : _buildTripsList(trips),
|
||||
// Contenu principal basé sur l'état du TripBloc
|
||||
if (tripState is TripLoading)
|
||||
_buildLoadingState()
|
||||
else if (tripState is TripError)
|
||||
_buildErrorState(tripState.message, user.id)
|
||||
else if (tripState is TripLoaded)
|
||||
tripState.trips.isEmpty
|
||||
? _buildEmptyState()
|
||||
: _buildTripsList(tripState.trips)
|
||||
else
|
||||
_buildEmptyState(),
|
||||
|
||||
// Espacement en bas pour éviter que le FAB cache le contenu
|
||||
const SizedBox(height: 80),
|
||||
],
|
||||
),
|
||||
),
|
||||
);
|
||||
},
|
||||
);
|
||||
},
|
||||
),
|
||||
|
||||
// FloatingActionButton
|
||||
floatingActionButton: FloatingActionButton(
|
||||
onPressed: () async {
|
||||
final result = await Navigator.push(
|
||||
context,
|
||||
MaterialPageRoute(builder: (context) => CreateTripContent()),
|
||||
);
|
||||
|
||||
// Le stream se mettra à jour automatiquement
|
||||
if (result == true) {
|
||||
ScaffoldMessenger.of(context).showSnackBar(
|
||||
SnackBar(
|
||||
content: Text('Voyage créé ! Il apparaîtra dans quelques secondes.'),
|
||||
backgroundColor: Colors.green,
|
||||
),
|
||||
|
||||
// FloatingActionButton
|
||||
floatingActionButton: FloatingActionButton(
|
||||
onPressed: () async {
|
||||
final result = await Navigator.push(
|
||||
context,
|
||||
MaterialPageRoute(builder: (context) => CreateTripContent()),
|
||||
);
|
||||
|
||||
if (result == true) {
|
||||
// Recharger les trips
|
||||
context.read<TripBloc>().add(TripLoadRequested(userId: user.id));
|
||||
}
|
||||
},
|
||||
backgroundColor: Theme.of(context).colorScheme.primary,
|
||||
foregroundColor: Colors.white,
|
||||
child: const Icon(Icons.add),
|
||||
),
|
||||
floatingActionButtonLocation: FloatingActionButtonLocation.endFloat,
|
||||
);
|
||||
}
|
||||
},
|
||||
backgroundColor: Theme.of(context).colorScheme.primary,
|
||||
foregroundColor: Colors.white,
|
||||
child: const Icon(Icons.add),
|
||||
),
|
||||
floatingActionButtonLocation: FloatingActionButtonLocation.endFloat,
|
||||
},
|
||||
);
|
||||
},
|
||||
);
|
||||
}
|
||||
|
||||
@@ -137,6 +170,35 @@ class _HomeContentState extends State<HomeContent> {
|
||||
);
|
||||
}
|
||||
|
||||
Widget _buildErrorState(String error, String userId) {
|
||||
return Center(
|
||||
child: Padding(
|
||||
padding: EdgeInsets.all(32),
|
||||
child: Column(
|
||||
mainAxisAlignment: MainAxisAlignment.center,
|
||||
children: [
|
||||
Icon(Icons.error, size: 64, color: Colors.red),
|
||||
SizedBox(height: 16),
|
||||
Text('Erreur lors du chargement des voyages'),
|
||||
SizedBox(height: 8),
|
||||
Text(
|
||||
error,
|
||||
textAlign: TextAlign.center,
|
||||
style: TextStyle(fontSize: 12, color: Colors.grey),
|
||||
),
|
||||
SizedBox(height: 16),
|
||||
ElevatedButton(
|
||||
onPressed: () {
|
||||
context.read<TripBloc>().add(TripLoadRequested(userId: userId));
|
||||
},
|
||||
child: Text('Réessayer'),
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
Widget _buildEmptyState() {
|
||||
return Center(
|
||||
child: Padding(
|
||||
@@ -182,7 +244,6 @@ class _HomeContentState extends State<HomeContent> {
|
||||
final colors = [Colors.blue, Colors.orange, Colors.green, Colors.purple, Colors.red];
|
||||
final color = colors[trip.title.hashCode.abs() % colors.length];
|
||||
|
||||
// Détecter le thème actuel
|
||||
final isDarkMode = Theme.of(context).brightness == Brightness.dark;
|
||||
final textColor = isDarkMode ? Colors.white : Colors.black;
|
||||
final secondaryTextColor = isDarkMode ? Colors.white70 : Colors.grey[700];
|
||||
@@ -255,9 +316,9 @@ class _HomeContentState extends State<HomeContent> {
|
||||
Expanded(
|
||||
child: Text(
|
||||
trip.location,
|
||||
style: TextStyle(
|
||||
style: const TextStyle(
|
||||
fontSize: 14,
|
||||
color: textColor,
|
||||
color: Colors.white,
|
||||
),
|
||||
overflow: TextOverflow.ellipsis,
|
||||
),
|
||||
@@ -341,7 +402,7 @@ class _HomeContentState extends State<HomeContent> {
|
||||
Icon(Icons.euro, size: 16, color: iconColor),
|
||||
SizedBox(width: 8),
|
||||
Text(
|
||||
'Budget: ${trip.budget?.toStringAsFixed(2)}€',
|
||||
'Budget: ${trip.budget!.toStringAsFixed(2)}€',
|
||||
style: TextStyle(
|
||||
fontSize: 14,
|
||||
color: iconColor,
|
||||
|
||||
@@ -1,6 +1,8 @@
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:provider/provider.dart';
|
||||
import '../../providers/user_provider.dart';
|
||||
import 'package:flutter_bloc/flutter_bloc.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 {
|
||||
@@ -8,14 +10,22 @@ class ProfileContent extends StatelessWidget {
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return Consumer<UserProvider>(
|
||||
builder: (context, userProvider, child) {
|
||||
final user = userProvider.currentUser;
|
||||
return BlocBuilder<UserBloc, user_state.UserState>(
|
||||
builder: (context, state) {
|
||||
if (state is user_state.UserLoading) {
|
||||
return Center(child: CircularProgressIndicator());
|
||||
}
|
||||
|
||||
if (user == null) {
|
||||
if (state is user_state.UserError) {
|
||||
return Center(child: Text('Erreur: ${state.message}'));
|
||||
}
|
||||
|
||||
if (state is! user_state.UserLoaded) {
|
||||
return Center(child: Text('Aucun utilisateur connecté'));
|
||||
}
|
||||
|
||||
final user = state.user;
|
||||
|
||||
return Column(
|
||||
children: [
|
||||
// Section titre
|
||||
@@ -62,7 +72,7 @@ class ProfileContent extends StatelessWidget {
|
||||
|
||||
// Nom complet
|
||||
Text(
|
||||
user.fullName,
|
||||
'${user.prenom} ${user.nom ?? ''}',
|
||||
style: TextStyle(
|
||||
fontSize: 20,
|
||||
fontWeight: FontWeight.bold,
|
||||
@@ -96,7 +106,7 @@ class ProfileContent extends StatelessWidget {
|
||||
title: Text('Changer le mot de passe'),
|
||||
trailing: Icon(Icons.arrow_forward_ios),
|
||||
onTap: () {
|
||||
_showChangePasswordDialog(context);
|
||||
_showChangePasswordDialog(context, user);
|
||||
},
|
||||
),
|
||||
|
||||
@@ -107,7 +117,7 @@ class ProfileContent extends StatelessWidget {
|
||||
title: Text('Supprimer le compte'),
|
||||
trailing: Icon(Icons.arrow_forward_ios),
|
||||
onTap: () {
|
||||
_showDeleteAccountDialog(context);
|
||||
_showDeleteAccountDialog(context, user);
|
||||
},
|
||||
),
|
||||
],
|
||||
@@ -116,13 +126,13 @@ class ProfileContent extends StatelessWidget {
|
||||
);
|
||||
}
|
||||
|
||||
void _showEditProfileDialog(BuildContext context, user) {
|
||||
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 context) {
|
||||
builder: (BuildContext dialogContext) {
|
||||
return AlertDialog(
|
||||
title: Text('Modifier le profil'),
|
||||
content: Column(
|
||||
@@ -141,37 +151,26 @@ class ProfileContent extends StatelessWidget {
|
||||
),
|
||||
actions: [
|
||||
TextButton(
|
||||
onPressed: () => Navigator.of(context).pop(),
|
||||
onPressed: () => Navigator.of(dialogContext).pop(),
|
||||
child: Text('Annuler'),
|
||||
),
|
||||
TextButton(
|
||||
onPressed: () async {
|
||||
if (prenomController.text.trim().isNotEmpty &&
|
||||
nomController.text.trim().isNotEmpty) {
|
||||
final updatedUser = user.copyWith(
|
||||
nom: nomController.text.trim(),
|
||||
prenom: prenomController.text.trim(),
|
||||
onPressed: () {
|
||||
if (prenomController.text.trim().isNotEmpty) {
|
||||
context.read<UserBloc>().add(
|
||||
user_event.UserUpdated({
|
||||
'prenom': prenomController.text.trim(),
|
||||
'nom': nomController.text.trim(),
|
||||
}),
|
||||
);
|
||||
|
||||
final success = await Provider.of<UserProvider>(context,
|
||||
listen: false)
|
||||
.updateUser(updatedUser);
|
||||
|
||||
Navigator.of(context).pop();
|
||||
|
||||
if (success) {
|
||||
ScaffoldMessenger.of(context).showSnackBar(
|
||||
SnackBar(
|
||||
content: Text('Profil mis à jour !'),
|
||||
backgroundColor: Colors.green),
|
||||
);
|
||||
} else {
|
||||
ScaffoldMessenger.of(context).showSnackBar(
|
||||
SnackBar(
|
||||
content: Text('Erreur lors de la mise à jour'),
|
||||
backgroundColor: Colors.red),
|
||||
);
|
||||
}
|
||||
Navigator.of(dialogContext).pop();
|
||||
ScaffoldMessenger.of(context).showSnackBar(
|
||||
SnackBar(
|
||||
content: Text('Profil mis à jour !'),
|
||||
backgroundColor: Colors.green,
|
||||
),
|
||||
);
|
||||
}
|
||||
},
|
||||
child: Text('Sauvegarder'),
|
||||
@@ -182,7 +181,7 @@ class ProfileContent extends StatelessWidget {
|
||||
);
|
||||
}
|
||||
|
||||
void _showChangePasswordDialog(BuildContext context) {
|
||||
void _showChangePasswordDialog(BuildContext context, user_state.UserModel user) {
|
||||
final currentPasswordController = TextEditingController();
|
||||
final newPasswordController = TextEditingController();
|
||||
final confirmPasswordController = TextEditingController();
|
||||
@@ -190,7 +189,7 @@ class ProfileContent extends StatelessWidget {
|
||||
|
||||
showDialog(
|
||||
context: context,
|
||||
builder: (BuildContext context) {
|
||||
builder: (BuildContext dialogContext) {
|
||||
return AlertDialog(
|
||||
title: Text('Changer le mot de passe'),
|
||||
content: Column(
|
||||
@@ -211,15 +210,13 @@ class ProfileContent extends StatelessWidget {
|
||||
TextField(
|
||||
controller: confirmPasswordController,
|
||||
obscureText: true,
|
||||
decoration: InputDecoration(
|
||||
labelText: 'Confirmer le mot de passe',
|
||||
),
|
||||
decoration: InputDecoration(labelText: 'Confirmer le mot de passe'),
|
||||
),
|
||||
],
|
||||
),
|
||||
actions: [
|
||||
TextButton(
|
||||
onPressed: () => Navigator.of(context).pop(),
|
||||
onPressed: () => Navigator.of(dialogContext).pop(),
|
||||
child: Text('Annuler'),
|
||||
),
|
||||
TextButton(
|
||||
@@ -229,8 +226,9 @@ class ProfileContent extends StatelessWidget {
|
||||
confirmPasswordController.text.isEmpty) {
|
||||
ScaffoldMessenger.of(context).showSnackBar(
|
||||
SnackBar(
|
||||
content: Text('Tous les champs sont requis'),
|
||||
backgroundColor: Colors.red),
|
||||
content: Text('Tous les champs sont requis'),
|
||||
backgroundColor: Colors.red,
|
||||
),
|
||||
);
|
||||
return;
|
||||
}
|
||||
@@ -238,42 +236,33 @@ class ProfileContent extends StatelessWidget {
|
||||
if (newPasswordController.text != confirmPasswordController.text) {
|
||||
ScaffoldMessenger.of(context).showSnackBar(
|
||||
SnackBar(
|
||||
content: Text('Les mots de passe ne correspondent pas'),
|
||||
backgroundColor: Colors.red),
|
||||
);
|
||||
return;
|
||||
}
|
||||
|
||||
if (newPasswordController.text.length < 8) {
|
||||
ScaffoldMessenger.of(context).showSnackBar(
|
||||
SnackBar(
|
||||
content:
|
||||
Text('Le mot de passe doit contenir au moins 8 caractères'),
|
||||
backgroundColor: Colors.red),
|
||||
content: Text('Les mots de passe ne correspondent pas'),
|
||||
backgroundColor: Colors.red,
|
||||
),
|
||||
);
|
||||
return;
|
||||
}
|
||||
|
||||
try {
|
||||
final user = Provider.of<UserProvider>(context, listen: false)
|
||||
.currentUser;
|
||||
await authService.resetPasswordFromCurrentPassword(
|
||||
currentPassword: currentPasswordController.text,
|
||||
newPassword: newPasswordController.text,
|
||||
email: user!.email,
|
||||
email: user.email,
|
||||
);
|
||||
|
||||
Navigator.of(context).pop();
|
||||
Navigator.of(dialogContext).pop();
|
||||
ScaffoldMessenger.of(context).showSnackBar(
|
||||
SnackBar(
|
||||
content: Text('Mot de passe changé !'),
|
||||
backgroundColor: Colors.green),
|
||||
content: Text('Mot de passe changé !'),
|
||||
backgroundColor: Colors.green,
|
||||
),
|
||||
);
|
||||
} catch (e) {
|
||||
ScaffoldMessenger.of(context).showSnackBar(
|
||||
SnackBar(
|
||||
content: Text('Erreur: Mot de passe actuel incorrect'),
|
||||
backgroundColor: Colors.red),
|
||||
content: Text('Erreur: Mot de passe actuel incorrect'),
|
||||
backgroundColor: Colors.red,
|
||||
),
|
||||
);
|
||||
}
|
||||
},
|
||||
@@ -285,13 +274,13 @@ class ProfileContent extends StatelessWidget {
|
||||
);
|
||||
}
|
||||
|
||||
void _showDeleteAccountDialog(BuildContext context) {
|
||||
void _showDeleteAccountDialog(BuildContext context, user_state.UserModel user) {
|
||||
final passwordController = TextEditingController();
|
||||
final authService = AuthService();
|
||||
|
||||
showDialog(
|
||||
context: context,
|
||||
builder: (BuildContext context) {
|
||||
builder: (BuildContext dialogContext) {
|
||||
return AlertDialog(
|
||||
title: Text('Supprimer le compte'),
|
||||
content: Column(
|
||||
@@ -313,30 +302,19 @@ class ProfileContent extends StatelessWidget {
|
||||
),
|
||||
actions: [
|
||||
TextButton(
|
||||
onPressed: () => Navigator.of(context).pop(),
|
||||
onPressed: () => Navigator.of(dialogContext).pop(),
|
||||
child: Text('Annuler'),
|
||||
),
|
||||
TextButton(
|
||||
onPressed: () async {
|
||||
if (passwordController.text.isEmpty) {
|
||||
ScaffoldMessenger.of(context).showSnackBar(
|
||||
SnackBar(
|
||||
content: Text('Mot de passe requis'),
|
||||
backgroundColor: Colors.red),
|
||||
);
|
||||
return;
|
||||
}
|
||||
|
||||
try {
|
||||
final user = Provider.of<UserProvider>(context, listen: false)
|
||||
.currentUser;
|
||||
await authService.deleteAccount(
|
||||
password: passwordController.text,
|
||||
email: user!.email,
|
||||
email: user.email,
|
||||
);
|
||||
|
||||
Navigator.of(context).pop();
|
||||
Provider.of<UserProvider>(context, listen: false).logout();
|
||||
Navigator.of(dialogContext).pop();
|
||||
context.read<UserBloc>().add(user_event.UserLoggedOut());
|
||||
Navigator.pushNamedAndRemoveUntil(
|
||||
context,
|
||||
'/login',
|
||||
@@ -345,9 +323,9 @@ class ProfileContent extends StatelessWidget {
|
||||
} catch (e) {
|
||||
ScaffoldMessenger.of(context).showSnackBar(
|
||||
SnackBar(
|
||||
content:
|
||||
Text('Erreur lors de la suppression: Mot de passe incorrect'),
|
||||
backgroundColor: Colors.red),
|
||||
content: Text('Erreur: Mot de passe incorrect'),
|
||||
backgroundColor: Colors.red,
|
||||
),
|
||||
);
|
||||
}
|
||||
},
|
||||
|
||||
@@ -1,6 +1,8 @@
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:provider/provider.dart';
|
||||
import '../../providers/theme_provider.dart';
|
||||
import 'package:flutter_bloc/flutter_bloc.dart';
|
||||
import '../../blocs/theme/theme_bloc.dart';
|
||||
import '../../blocs/theme/theme_state.dart';
|
||||
import '../../blocs/theme/theme_event.dart';
|
||||
|
||||
class SettingsThemeContent extends StatelessWidget {
|
||||
const SettingsThemeContent({super.key});
|
||||
@@ -12,8 +14,8 @@ class SettingsThemeContent extends StatelessWidget {
|
||||
title: const Text('Thème'),
|
||||
backgroundColor: Theme.of(context).colorScheme.inversePrimary,
|
||||
),
|
||||
body: Consumer<ThemeProvider>(
|
||||
builder: (context, themeProvider, child) {
|
||||
body: BlocBuilder<ThemeBloc, ThemeState>(
|
||||
builder: (context, state) {
|
||||
return ListView(
|
||||
padding: const EdgeInsets.all(16),
|
||||
children: [
|
||||
@@ -27,19 +29,21 @@ class SettingsThemeContent extends StatelessWidget {
|
||||
Card(
|
||||
child: ListTile(
|
||||
leading: Icon(
|
||||
themeProvider.themeMode == ThemeMode.system
|
||||
state.themeMode == ThemeMode.system
|
||||
? Icons.radio_button_checked
|
||||
: Icons.radio_button_unchecked,
|
||||
color: themeProvider.themeMode == ThemeMode.system
|
||||
color: state.themeMode == ThemeMode.system
|
||||
? Theme.of(context).colorScheme.primary
|
||||
: null,
|
||||
),
|
||||
title: const Text('Système'),
|
||||
subtitle: const Text('Suit les paramètres de votre appareil'),
|
||||
trailing: const Icon(Icons.brightness_auto),
|
||||
selected: themeProvider.themeMode == ThemeMode.system,
|
||||
selected: state.themeMode == ThemeMode.system,
|
||||
onTap: () {
|
||||
themeProvider.setThemeMode(ThemeMode.system);
|
||||
context.read<ThemeBloc>().add(
|
||||
const ThemeChanged(themeMode: ThemeMode.system),
|
||||
);
|
||||
},
|
||||
),
|
||||
),
|
||||
@@ -50,19 +54,21 @@ class SettingsThemeContent extends StatelessWidget {
|
||||
Card(
|
||||
child: ListTile(
|
||||
leading: Icon(
|
||||
themeProvider.themeMode == ThemeMode.light
|
||||
state.themeMode == ThemeMode.light
|
||||
? Icons.radio_button_checked
|
||||
: Icons.radio_button_unchecked,
|
||||
color: themeProvider.themeMode == ThemeMode.light
|
||||
color: state.themeMode == ThemeMode.light
|
||||
? Theme.of(context).colorScheme.primary
|
||||
: null,
|
||||
),
|
||||
title: const Text('Clair'),
|
||||
subtitle: const Text('Thème clair en permanence'),
|
||||
trailing: const Icon(Icons.light_mode),
|
||||
selected: themeProvider.themeMode == ThemeMode.light,
|
||||
selected: state.themeMode == ThemeMode.light,
|
||||
onTap: () {
|
||||
themeProvider.setThemeMode(ThemeMode.light);
|
||||
context.read<ThemeBloc>().add(
|
||||
const ThemeChanged(themeMode: ThemeMode.light),
|
||||
);
|
||||
},
|
||||
),
|
||||
),
|
||||
@@ -73,19 +79,21 @@ class SettingsThemeContent extends StatelessWidget {
|
||||
Card(
|
||||
child: ListTile(
|
||||
leading: Icon(
|
||||
themeProvider.themeMode == ThemeMode.dark
|
||||
state.themeMode == ThemeMode.dark
|
||||
? Icons.radio_button_checked
|
||||
: Icons.radio_button_unchecked,
|
||||
color: themeProvider.themeMode == ThemeMode.dark
|
||||
color: state.themeMode == ThemeMode.dark
|
||||
? Theme.of(context).colorScheme.primary
|
||||
: null,
|
||||
),
|
||||
title: const Text('Sombre'),
|
||||
subtitle: const Text('Thème sombre en permanence'),
|
||||
trailing: const Icon(Icons.dark_mode),
|
||||
selected: themeProvider.themeMode == ThemeMode.dark,
|
||||
selected: state.themeMode == ThemeMode.dark,
|
||||
onTap: () {
|
||||
themeProvider.setThemeMode(ThemeMode.dark);
|
||||
context.read<ThemeBloc>().add(
|
||||
const ThemeChanged(themeMode: ThemeMode.dark),
|
||||
);
|
||||
},
|
||||
),
|
||||
),
|
||||
@@ -107,14 +115,14 @@ class SettingsThemeContent extends StatelessWidget {
|
||||
Row(
|
||||
children: [
|
||||
Icon(
|
||||
themeProvider.isDarkMode
|
||||
state.isDarkMode
|
||||
? Icons.dark_mode
|
||||
: Icons.light_mode,
|
||||
color: Theme.of(context).colorScheme.primary,
|
||||
),
|
||||
const SizedBox(width: 10),
|
||||
Text(
|
||||
themeProvider.isDarkMode
|
||||
state.isDarkMode
|
||||
? 'Mode sombre actif'
|
||||
: 'Mode clair actif',
|
||||
style: TextStyle(
|
||||
|
||||
Reference in New Issue
Block a user