refactoring and checkpoint
This commit is contained in:
@@ -1,443 +1,110 @@
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:afterwork/data/models/event_model.dart';
|
||||
import 'package:afterwork/data/models/user_model.dart';
|
||||
import 'package:afterwork/core/constants/urls.dart';
|
||||
import 'package:afterwork/data/services/category_service.dart';
|
||||
import '../location/location_picker_screen.dart';
|
||||
import 'dart:convert';
|
||||
import 'package:fluttertoast/fluttertoast.dart';
|
||||
import 'package:google_maps_flutter/google_maps_flutter.dart';
|
||||
import 'package:http/http.dart' as http;
|
||||
|
||||
/// Classe représentant la boîte de dialogue pour ajouter un nouvel événement.
|
||||
/// Dialogue pour ajouter un nouvel événement.
|
||||
/// Ce widget affiche un formulaire permettant à l'utilisateur de saisir les détails d'un événement.
|
||||
/// Les logs permettent de suivre les actions de l'utilisateur dans ce dialogue.
|
||||
class AddEventDialog extends StatefulWidget {
|
||||
final String userId;
|
||||
final String userName;
|
||||
final String userLastName;
|
||||
|
||||
const AddEventDialog({
|
||||
super.key,
|
||||
Key? key,
|
||||
required this.userId,
|
||||
required this.userName,
|
||||
required this.userLastName,
|
||||
});
|
||||
}) : super(key: key);
|
||||
|
||||
@override
|
||||
_AddEventDialogState createState() => _AddEventDialogState();
|
||||
}
|
||||
|
||||
class _AddEventDialogState extends State<AddEventDialog> {
|
||||
final _formKey = GlobalKey<FormState>();
|
||||
|
||||
// Variables pour stocker les données de l'événement
|
||||
String _title = '';
|
||||
String _description = '';
|
||||
DateTime? _selectedDate;
|
||||
String? _imagePath;
|
||||
String _location = 'Abidjan';
|
||||
String _category = '';
|
||||
String _link = '';
|
||||
LatLng? _selectedLatLng = const LatLng(5.348722, -3.985038);
|
||||
Map<String, List<String>> _categories = {};
|
||||
List<String> _currentCategories = [];
|
||||
String? _selectedCategoryType;
|
||||
|
||||
@override
|
||||
void initState() {
|
||||
super.initState();
|
||||
_loadCategories();
|
||||
}
|
||||
|
||||
void _loadCategories() async {
|
||||
final CategoryService categoryService = CategoryService();
|
||||
final categories = await categoryService.loadCategories();
|
||||
setState(() {
|
||||
_categories = categories;
|
||||
_selectedCategoryType = categories.keys.first;
|
||||
_currentCategories = categories[_selectedCategoryType] ?? [];
|
||||
});
|
||||
}
|
||||
final _formKey = GlobalKey<FormState>(); // Clé pour valider le formulaire
|
||||
String _eventName = ''; // Nom de l'événement
|
||||
DateTime _selectedDate = DateTime.now(); // Date de l'événement
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return Dialog(
|
||||
shape: RoundedRectangleBorder(
|
||||
borderRadius: BorderRadius.circular(15.0),
|
||||
),
|
||||
backgroundColor: const Color(0xFF2C2C3E),
|
||||
child: SingleChildScrollView(
|
||||
child: Padding(
|
||||
padding: const EdgeInsets.all(16.0),
|
||||
child: Form(
|
||||
key: _formKey,
|
||||
child: Column(
|
||||
mainAxisSize: MainAxisSize.min,
|
||||
children: [
|
||||
_buildTitleField(),
|
||||
const SizedBox(height: 10),
|
||||
_buildDescriptionField(),
|
||||
const SizedBox(height: 10),
|
||||
_buildDatePicker(),
|
||||
const SizedBox(height: 10),
|
||||
_buildLocationField(context),
|
||||
const SizedBox(height: 10),
|
||||
_buildCategoryField(),
|
||||
const SizedBox(height: 10),
|
||||
_buildImagePicker(),
|
||||
const SizedBox(height: 10),
|
||||
_buildLinkField(),
|
||||
const SizedBox(height: 20),
|
||||
_buildSubmitButton(),
|
||||
],
|
||||
),
|
||||
),
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
print("Affichage du dialogue d'ajout d'événement.");
|
||||
|
||||
Widget _buildTitleField() {
|
||||
return TextFormField(
|
||||
decoration: InputDecoration(
|
||||
labelText: 'Titre',
|
||||
labelStyle: const TextStyle(color: Colors.white70),
|
||||
filled: true,
|
||||
fillColor: Colors.white.withOpacity(0.1),
|
||||
border: const OutlineInputBorder(
|
||||
borderRadius: BorderRadius.all(Radius.circular(10.0)),
|
||||
borderSide: BorderSide.none,
|
||||
),
|
||||
prefixIcon: const Icon(Icons.title, color: Colors.white70),
|
||||
),
|
||||
style: const TextStyle(color: Colors.white),
|
||||
validator: (value) {
|
||||
if (value == null || value.isEmpty) {
|
||||
print('Erreur: Titre est vide');
|
||||
return 'Veuillez entrer un titre';
|
||||
}
|
||||
return null;
|
||||
},
|
||||
onSaved: (value) {
|
||||
_title = value ?? '';
|
||||
print('Titre sauvegardé: $_title');
|
||||
},
|
||||
);
|
||||
}
|
||||
|
||||
Widget _buildDescriptionField() {
|
||||
return TextFormField(
|
||||
decoration: InputDecoration(
|
||||
labelText: 'Description',
|
||||
labelStyle: const TextStyle(color: Colors.white70),
|
||||
filled: true,
|
||||
fillColor: Colors.white.withOpacity(0.1),
|
||||
border: const OutlineInputBorder(
|
||||
borderRadius: BorderRadius.all(Radius.circular(10.0)),
|
||||
borderSide: BorderSide.none,
|
||||
),
|
||||
prefixIcon: const Icon(Icons.description, color: Colors.white70),
|
||||
),
|
||||
style: const TextStyle(color: Colors.white),
|
||||
maxLines: 3,
|
||||
validator: (value) {
|
||||
if (value == null || value.isEmpty) {
|
||||
print('Erreur: Description est vide');
|
||||
return 'Veuillez entrer une description';
|
||||
}
|
||||
return null;
|
||||
},
|
||||
onSaved: (value) {
|
||||
_description = value ?? '';
|
||||
print('Description sauvegardée: $_description');
|
||||
},
|
||||
);
|
||||
}
|
||||
|
||||
Widget _buildDatePicker() {
|
||||
return GestureDetector(
|
||||
onTap: () async {
|
||||
final DateTime? picked = await showDatePicker(
|
||||
context: context,
|
||||
initialDate: DateTime.now(),
|
||||
firstDate: DateTime.now(),
|
||||
lastDate: DateTime(2101),
|
||||
);
|
||||
if (picked != null && picked != _selectedDate) {
|
||||
setState(() {
|
||||
_selectedDate = picked;
|
||||
print('Date sélectionnée: $_selectedDate');
|
||||
});
|
||||
} else {
|
||||
print('Date non sélectionnée ou égale à la précédente');
|
||||
}
|
||||
},
|
||||
child: Container(
|
||||
padding: const EdgeInsets.symmetric(vertical: 12.0, horizontal: 16.0),
|
||||
decoration: BoxDecoration(
|
||||
color: Colors.white.withOpacity(0.1),
|
||||
borderRadius: BorderRadius.circular(10.0),
|
||||
),
|
||||
child: Row(
|
||||
mainAxisAlignment: MainAxisAlignment.spaceBetween,
|
||||
return AlertDialog(
|
||||
title: const Text('Ajouter un événement'),
|
||||
content: Form(
|
||||
key: _formKey,
|
||||
child: Column(
|
||||
mainAxisSize: MainAxisSize.min,
|
||||
children: [
|
||||
Text(
|
||||
_selectedDate == null
|
||||
? 'Sélectionnez une date'
|
||||
: '${_selectedDate!.day}/${_selectedDate!.month}/${_selectedDate!.year}',
|
||||
style: const TextStyle(color: Colors.white70),
|
||||
// Champ pour entrer le nom de l'événement
|
||||
TextFormField(
|
||||
decoration: const InputDecoration(labelText: 'Nom de l\'événement'),
|
||||
onSaved: (value) {
|
||||
_eventName = value ?? '';
|
||||
print("Nom de l'événement saisi : $_eventName");
|
||||
},
|
||||
validator: (value) {
|
||||
if (value == null || value.isEmpty) {
|
||||
print("Erreur : le champ du nom de l'événement est vide.");
|
||||
return 'Veuillez entrer un nom d\'événement';
|
||||
}
|
||||
return null;
|
||||
},
|
||||
),
|
||||
const SizedBox(height: 20),
|
||||
// Sélecteur de date pour l'événement
|
||||
ElevatedButton(
|
||||
onPressed: () async {
|
||||
final selectedDate = await _selectDate(context);
|
||||
if (selectedDate != null) {
|
||||
setState(() {
|
||||
_selectedDate = selectedDate;
|
||||
print("Date de l'événement sélectionnée : $_selectedDate");
|
||||
});
|
||||
}
|
||||
},
|
||||
child: Text('Sélectionner la date : ${_selectedDate.toLocal()}'.split(' ')[0]),
|
||||
),
|
||||
const Icon(Icons.calendar_today, color: Colors.white70),
|
||||
],
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
Widget _buildLocationField(BuildContext context) {
|
||||
return GestureDetector(
|
||||
onTap: () async {
|
||||
final LatLng? pickedLocation = await Navigator.push(
|
||||
context,
|
||||
MaterialPageRoute(
|
||||
builder: (context) => const LocationPickerScreen(),
|
||||
),
|
||||
);
|
||||
if (pickedLocation != null) {
|
||||
setState(() {
|
||||
_selectedLatLng = pickedLocation;
|
||||
_location = '${pickedLocation.latitude}, ${pickedLocation.longitude}';
|
||||
print('Localisation sélectionnée: $_location');
|
||||
});
|
||||
} else {
|
||||
print('Localisation non sélectionnée');
|
||||
}
|
||||
},
|
||||
child: Container(
|
||||
padding: const EdgeInsets.symmetric(vertical: 12.0, horizontal: 16.0),
|
||||
decoration: BoxDecoration(
|
||||
color: Colors.white.withOpacity(0.1),
|
||||
borderRadius: BorderRadius.circular(10.0),
|
||||
),
|
||||
child: Row(
|
||||
mainAxisAlignment: MainAxisAlignment.spaceBetween,
|
||||
children: [
|
||||
Text(
|
||||
_selectedLatLng == null
|
||||
? 'Sélectionnez une localisation'
|
||||
: 'Localisation: $_location',
|
||||
style: const TextStyle(color: Colors.white70),
|
||||
),
|
||||
const Icon(Icons.location_on, color: Colors.white70),
|
||||
],
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
/// Construction du champ de catégorie avec sélection du type et de la catégorie.
|
||||
Widget _buildCategoryField() {
|
||||
return Column(
|
||||
crossAxisAlignment: CrossAxisAlignment.start,
|
||||
children: [
|
||||
DropdownButtonFormField<String>(
|
||||
decoration: InputDecoration(
|
||||
labelText: 'Type de catégorie',
|
||||
labelStyle: const TextStyle(color: Colors.white70),
|
||||
filled: true,
|
||||
fillColor: Colors.white.withOpacity(0.1),
|
||||
border: const OutlineInputBorder(
|
||||
borderRadius: BorderRadius.all(Radius.circular(10.0)),
|
||||
borderSide: BorderSide.none,
|
||||
),
|
||||
),
|
||||
value: _selectedCategoryType,
|
||||
items: _categories.keys.map((String type) {
|
||||
return DropdownMenuItem<String>(
|
||||
value: type,
|
||||
child: Text(type, style: const TextStyle(color: Colors.black)),
|
||||
);
|
||||
}).toList(),
|
||||
onChanged: (String? newValue) {
|
||||
setState(() {
|
||||
_selectedCategoryType = newValue;
|
||||
_currentCategories = _categories[newValue] ?? [];
|
||||
_category = ''; // Réinitialiser la catégorie sélectionnée
|
||||
print('Type de catégorie sélectionné : $_selectedCategoryType');
|
||||
print('Catégories disponibles pour ce type : $_currentCategories');
|
||||
});
|
||||
actions: [
|
||||
// Bouton pour annuler l'ajout de l'événement
|
||||
TextButton(
|
||||
onPressed: () {
|
||||
print("L'utilisateur a annulé l'ajout de l'événement.");
|
||||
Navigator.of(context).pop();
|
||||
},
|
||||
child: const Text('Annuler'),
|
||||
),
|
||||
const SizedBox(height: 10),
|
||||
DropdownButtonFormField<String>(
|
||||
decoration: InputDecoration(
|
||||
labelText: 'Catégorie',
|
||||
labelStyle: const TextStyle(color: Colors.white70),
|
||||
filled: true,
|
||||
fillColor: Colors.white.withOpacity(0.1),
|
||||
border: const OutlineInputBorder(
|
||||
borderRadius: BorderRadius.all(Radius.circular(10.0)),
|
||||
borderSide: BorderSide.none,
|
||||
),
|
||||
),
|
||||
value: _category.isNotEmpty ? _category : null,
|
||||
items: _currentCategories.map((String category) {
|
||||
return DropdownMenuItem<String>(
|
||||
value: category,
|
||||
child: Text(category, style: const TextStyle(color: Colors.black)),
|
||||
);
|
||||
}).toList(),
|
||||
onChanged: (String? newValue) {
|
||||
setState(() {
|
||||
_category = newValue ?? '';
|
||||
print('Catégorie sélectionnée : $_category');
|
||||
});
|
||||
},
|
||||
validator: (value) {
|
||||
if (value == null || value.isEmpty) {
|
||||
print('Erreur: Catégorie non sélectionnée');
|
||||
return 'Veuillez sélectionner une catégorie';
|
||||
// Bouton pour soumettre le formulaire et ajouter l'événement
|
||||
TextButton(
|
||||
onPressed: () {
|
||||
if (_formKey.currentState?.validate() == true) {
|
||||
_formKey.currentState?.save();
|
||||
print("L'utilisateur a ajouté un événement : Nom = $_eventName, Date = $_selectedDate");
|
||||
Navigator.of(context).pop({
|
||||
'eventName': _eventName,
|
||||
'eventDate': _selectedDate,
|
||||
});
|
||||
}
|
||||
return null;
|
||||
},
|
||||
child: const Text('Ajouter'),
|
||||
),
|
||||
],
|
||||
);
|
||||
}
|
||||
|
||||
Widget _buildImagePicker() {
|
||||
return GestureDetector(
|
||||
onTap: () {
|
||||
// Logique pour sélectionner une image
|
||||
print('Image Picker activé');
|
||||
},
|
||||
child: Container(
|
||||
padding: const EdgeInsets.symmetric(vertical: 12.0, horizontal: 16.0),
|
||||
decoration: BoxDecoration(
|
||||
color: Colors.white.withOpacity(0.1),
|
||||
borderRadius: BorderRadius.circular(10.0),
|
||||
),
|
||||
child: Row(
|
||||
mainAxisAlignment: MainAxisAlignment.spaceBetween,
|
||||
children: [
|
||||
Text(
|
||||
_imagePath == null
|
||||
? 'Sélectionnez une image'
|
||||
: 'Image sélectionnée: $_imagePath',
|
||||
style: const TextStyle(color: Colors.white70),
|
||||
),
|
||||
const Icon(Icons.image, color: Colors.white70),
|
||||
],
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
Widget _buildLinkField() {
|
||||
return TextFormField(
|
||||
decoration: InputDecoration(
|
||||
labelText: 'Lien (optionnel)',
|
||||
labelStyle: const TextStyle(color: Colors.white70),
|
||||
filled: true,
|
||||
fillColor: Colors.white.withOpacity(0.1),
|
||||
border: const OutlineInputBorder(
|
||||
borderRadius: BorderRadius.all(Radius.circular(10.0)),
|
||||
borderSide: BorderSide.none,
|
||||
),
|
||||
prefixIcon: const Icon(Icons.link, color: Colors.white70),
|
||||
),
|
||||
style: const TextStyle(color: Colors.white),
|
||||
onSaved: (value) {
|
||||
_link = value ?? '';
|
||||
print('Lien sauvegardé: $_link');
|
||||
},
|
||||
);
|
||||
}
|
||||
|
||||
Widget _buildSubmitButton() {
|
||||
return ElevatedButton(
|
||||
onPressed: () async {
|
||||
if (_formKey.currentState!.validate()) {
|
||||
_formKey.currentState!.save();
|
||||
print('Formulaire validé');
|
||||
|
||||
EventModel newEvent = EventModel(
|
||||
id: '',
|
||||
title: _title,
|
||||
description: _description,
|
||||
date: _selectedDate?.toIso8601String() ?? '',
|
||||
location: _location,
|
||||
category: _category,
|
||||
link: _link,
|
||||
imageUrl: _imagePath ?? '',
|
||||
status: 'OPEN',
|
||||
creator: UserModel(
|
||||
userId: widget.userId,
|
||||
nom: widget.userName,
|
||||
prenoms: widget.userLastName,
|
||||
email: '',
|
||||
motDePasse: '',
|
||||
),
|
||||
participants: [
|
||||
UserModel(
|
||||
userId: widget.userId,
|
||||
nom: widget.userName,
|
||||
prenoms: widget.userLastName,
|
||||
email: '',
|
||||
motDePasse: '',
|
||||
)
|
||||
],
|
||||
);
|
||||
|
||||
Map<String, dynamic> eventData = newEvent.toJson();
|
||||
print('Données JSON de l\'événement: $eventData');
|
||||
|
||||
final response = await http.post(
|
||||
Uri.parse('${Urls.baseUrl}/events'),
|
||||
headers: {'Content-Type': 'application/json'},
|
||||
body: jsonEncode(eventData),
|
||||
);
|
||||
|
||||
print('Statut de la réponse: ${response.statusCode}');
|
||||
print('Réponse brute: ${response.body}');
|
||||
|
||||
if (response.statusCode == 201) {
|
||||
print('Événement créé avec succès');
|
||||
Fluttertoast.showToast(
|
||||
msg: "Événement créé avec succès!",
|
||||
toastLength: Toast.LENGTH_SHORT,
|
||||
gravity: ToastGravity.BOTTOM,
|
||||
backgroundColor: Colors.green,
|
||||
textColor: Colors.white,
|
||||
fontSize: 16.0,
|
||||
);
|
||||
Navigator.of(context).pop(); // Fermer la boîte de dialogue
|
||||
} else {
|
||||
print('Erreur lors de la création de l\'événement: ${response.reasonPhrase}');
|
||||
Fluttertoast.showToast(
|
||||
msg: "Erreur lors de la création de l'événement",
|
||||
toastLength: Toast.LENGTH_SHORT,
|
||||
gravity: ToastGravity.BOTTOM,
|
||||
backgroundColor: Colors.red,
|
||||
textColor: Colors.white,
|
||||
fontSize: 16.0,
|
||||
);
|
||||
ScaffoldMessenger.of(context).showSnackBar(
|
||||
SnackBar(content: Text('Erreur: ${response.reasonPhrase}')),
|
||||
);
|
||||
}
|
||||
} else {
|
||||
print('Le formulaire n\'est pas valide');
|
||||
}
|
||||
},
|
||||
style: ElevatedButton.styleFrom(
|
||||
backgroundColor: const Color(0xFF1DBF73),
|
||||
shape: RoundedRectangleBorder(
|
||||
borderRadius: BorderRadius.circular(8.0),
|
||||
),
|
||||
padding: const EdgeInsets.symmetric(vertical: 12.0),
|
||||
minimumSize: const Size(double.infinity, 40),
|
||||
),
|
||||
child: const Text('Ajouter l\'événement', style: TextStyle(color: Colors.white)),
|
||||
/// Fonction pour afficher le sélecteur de date
|
||||
Future<DateTime?> _selectDate(BuildContext context) async {
|
||||
final DateTime? picked = await showDatePicker(
|
||||
context: context,
|
||||
initialDate: _selectedDate,
|
||||
firstDate: DateTime(2000),
|
||||
lastDate: DateTime(2101),
|
||||
);
|
||||
if (picked != null) {
|
||||
print("Date choisie dans le sélecteur : $picked");
|
||||
}
|
||||
return picked;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user