Refactoring
This commit is contained in:
@@ -1,15 +1,15 @@
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:fluttertoast/fluttertoast.dart';
|
||||
import 'package:google_maps_flutter/google_maps_flutter.dart';
|
||||
import 'package:http/http.dart' as http;
|
||||
import 'dart:convert';
|
||||
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.
|
||||
/// Cette boîte de dialogue permet à l'utilisateur de remplir les détails d'un nouvel événement.
|
||||
class AddEventDialog extends StatefulWidget {
|
||||
final String userId;
|
||||
final String userName;
|
||||
@@ -27,15 +27,36 @@ class AddEventDialog extends StatefulWidget {
|
||||
}
|
||||
|
||||
class _AddEventDialogState extends State<AddEventDialog> {
|
||||
final _formKey = GlobalKey<FormState>(); // Clé globale pour valider le formulaire
|
||||
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'; // Par défaut à Cocody, Abidjan
|
||||
String _location = 'Abidjan';
|
||||
String _category = '';
|
||||
String _link = '';
|
||||
LatLng? _selectedLatLng = const LatLng(5.348722, -3.985038); // Par défaut à Cocody, Abidjan
|
||||
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] ?? [];
|
||||
});
|
||||
}
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
@@ -48,7 +69,7 @@ class _AddEventDialogState extends State<AddEventDialog> {
|
||||
child: Padding(
|
||||
padding: const EdgeInsets.all(16.0),
|
||||
child: Form(
|
||||
key: _formKey, // Formulaire clé pour valider l'entrée des utilisateurs
|
||||
key: _formKey,
|
||||
child: Column(
|
||||
mainAxisSize: MainAxisSize.min,
|
||||
children: [
|
||||
@@ -75,7 +96,6 @@ class _AddEventDialogState extends State<AddEventDialog> {
|
||||
);
|
||||
}
|
||||
|
||||
/// Construction du champ de saisie du titre de l'événement.
|
||||
Widget _buildTitleField() {
|
||||
return TextFormField(
|
||||
decoration: InputDecoration(
|
||||
@@ -104,7 +124,6 @@ class _AddEventDialogState extends State<AddEventDialog> {
|
||||
);
|
||||
}
|
||||
|
||||
/// Construction du champ de saisie de la description de l'événement.
|
||||
Widget _buildDescriptionField() {
|
||||
return TextFormField(
|
||||
decoration: InputDecoration(
|
||||
@@ -134,7 +153,6 @@ class _AddEventDialogState extends State<AddEventDialog> {
|
||||
);
|
||||
}
|
||||
|
||||
/// Widget pour le sélecteur de date pour l'événement.
|
||||
Widget _buildDatePicker() {
|
||||
return GestureDetector(
|
||||
onTap: () async {
|
||||
@@ -175,7 +193,6 @@ class _AddEventDialogState extends State<AddEventDialog> {
|
||||
);
|
||||
}
|
||||
|
||||
/// Construction du champ de localisation pour l'événement.
|
||||
Widget _buildLocationField(BuildContext context) {
|
||||
return GestureDetector(
|
||||
onTap: () async {
|
||||
@@ -217,29 +234,76 @@ class _AddEventDialogState extends State<AddEventDialog> {
|
||||
);
|
||||
}
|
||||
|
||||
/// Construction du champ de saisie de la catégorie de l'événement.
|
||||
/// Construction du champ de catégorie avec sélection du type et de la catégorie.
|
||||
Widget _buildCategoryField() {
|
||||
return TextFormField(
|
||||
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,
|
||||
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');
|
||||
});
|
||||
},
|
||||
),
|
||||
prefixIcon: const Icon(Icons.category, color: Colors.white70),
|
||||
),
|
||||
style: const TextStyle(color: Colors.white),
|
||||
onSaved: (value) {
|
||||
_category = value ?? '';
|
||||
print('Catégorie sauvegardée: $_category');
|
||||
},
|
||||
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';
|
||||
}
|
||||
return null;
|
||||
},
|
||||
),
|
||||
],
|
||||
);
|
||||
}
|
||||
|
||||
/// Construction du champ de sélection d'image pour l'événement.
|
||||
Widget _buildImagePicker() {
|
||||
return GestureDetector(
|
||||
onTap: () {
|
||||
@@ -268,7 +332,6 @@ class _AddEventDialogState extends State<AddEventDialog> {
|
||||
);
|
||||
}
|
||||
|
||||
/// Construction du champ de saisie du lien associé à l'événement.
|
||||
Widget _buildLinkField() {
|
||||
return TextFormField(
|
||||
decoration: InputDecoration(
|
||||
@@ -290,7 +353,6 @@ class _AddEventDialogState extends State<AddEventDialog> {
|
||||
);
|
||||
}
|
||||
|
||||
/// Construction du bouton de soumission pour ajouter l'événement.
|
||||
Widget _buildSubmitButton() {
|
||||
return ElevatedButton(
|
||||
onPressed: () async {
|
||||
@@ -298,7 +360,6 @@ class _AddEventDialogState extends State<AddEventDialog> {
|
||||
_formKey.currentState!.save();
|
||||
print('Formulaire validé');
|
||||
|
||||
// Créer l'événement en utilisant l'ID réel de l'utilisateur pour le créateur et les participants
|
||||
EventModel newEvent = EventModel(
|
||||
id: '',
|
||||
title: _title,
|
||||
@@ -308,7 +369,7 @@ class _AddEventDialogState extends State<AddEventDialog> {
|
||||
category: _category,
|
||||
link: _link,
|
||||
imageUrl: _imagePath ?? '',
|
||||
status: '',
|
||||
status: 'OPEN',
|
||||
creator: UserModel(
|
||||
userId: widget.userId,
|
||||
nom: widget.userName,
|
||||
@@ -327,11 +388,9 @@ class _AddEventDialogState extends State<AddEventDialog> {
|
||||
],
|
||||
);
|
||||
|
||||
// Convertir l'événement en JSON
|
||||
Map<String, dynamic> eventData = newEvent.toJson();
|
||||
print('Données JSON de l\'événement: $eventData');
|
||||
|
||||
// Envoyer la requête POST à l'API
|
||||
final response = await http.post(
|
||||
Uri.parse('${Urls.baseUrl}/events'),
|
||||
headers: {'Content-Type': 'application/json'},
|
||||
@@ -342,7 +401,6 @@ class _AddEventDialogState extends State<AddEventDialog> {
|
||||
print('Réponse brute: ${response.body}');
|
||||
|
||||
if (response.statusCode == 201) {
|
||||
// Création réussie
|
||||
print('Événement créé avec succès');
|
||||
Fluttertoast.showToast(
|
||||
msg: "Événement créé avec succès!",
|
||||
@@ -352,9 +410,8 @@ class _AddEventDialogState extends State<AddEventDialog> {
|
||||
textColor: Colors.white,
|
||||
fontSize: 16.0,
|
||||
);
|
||||
Navigator.of(context).pop(); // Ne passez pas de valeur ici
|
||||
Navigator.of(context).pop(); // Fermer la boîte de dialogue
|
||||
} else {
|
||||
// Gérer l'erreur
|
||||
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",
|
||||
@@ -383,5 +440,4 @@ class _AddEventDialogState extends State<AddEventDialog> {
|
||||
child: const Text('Ajouter l\'événement', style: TextStyle(color: Colors.white)),
|
||||
);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user