refactoring

This commit is contained in:
DahoudG
2024-09-01 04:08:50 +00:00
parent 7e1cb85160
commit a1fce6bf27
31 changed files with 1651 additions and 441 deletions

View File

@@ -1,8 +1,26 @@
import 'package:flutter/material.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/creator_model.dart';
import 'package:afterwork/data/models/event_model.dart';
import 'package:afterwork/data/models/participant_model.dart';
import 'package:provider/provider.dart';
import 'package:afterwork/data/providers/user_provider.dart';
import 'package:afterwork/core/constants/urls.dart';
import '../location/location_picker_screen.dart';
class AddEventDialog extends StatefulWidget {
const AddEventDialog({super.key});
final String userId;
final String userName;
final String userLastName;
const AddEventDialog({
super.key,
required this.userId,
required this.userName,
required this.userLastName,
});
@override
_AddEventDialogState createState() => _AddEventDialogState();
@@ -14,10 +32,10 @@ class _AddEventDialogState extends State<AddEventDialog> {
String _description = '';
DateTime? _selectedDate;
String? _imagePath;
String _location = '';
String _location = 'Abidjan'; // Par défaut à Cocody, Abidjan
String _category = '';
String _link = '';
LatLng? _selectedLatLng;
LatLng? _selectedLatLng = const LatLng(5.348722, -3.985038); // Par défaut à Cocody, Abidjan
@override
Widget build(BuildContext context) {
@@ -26,29 +44,31 @@ class _AddEventDialogState extends State<AddEventDialog> {
borderRadius: BorderRadius.circular(15.0),
),
backgroundColor: const Color(0xFF2C2C3E),
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(),
],
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(),
],
),
),
),
),
@@ -71,12 +91,14 @@ class _AddEventDialogState extends State<AddEventDialog> {
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');
},
);
}
@@ -98,12 +120,14 @@ class _AddEventDialogState extends State<AddEventDialog> {
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');
},
);
}
@@ -120,7 +144,10 @@ class _AddEventDialogState extends State<AddEventDialog> {
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(
@@ -158,7 +185,10 @@ class _AddEventDialogState extends State<AddEventDialog> {
setState(() {
_selectedLatLng = pickedLocation;
_location = '${pickedLocation.latitude}, ${pickedLocation.longitude}';
print('Localisation sélectionnée: $_location');
});
} else {
print('Localisation non sélectionnée');
}
},
child: Container(
@@ -199,6 +229,7 @@ class _AddEventDialogState extends State<AddEventDialog> {
style: const TextStyle(color: Colors.white),
onSaved: (value) {
_category = value ?? '';
print('Catégorie sauvegardée: $_category');
},
);
}
@@ -207,6 +238,7 @@ class _AddEventDialogState extends State<AddEventDialog> {
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),
@@ -218,7 +250,9 @@ class _AddEventDialogState extends State<AddEventDialog> {
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: [
Text(
_imagePath == null ? 'Sélectionnez une image' : 'Image sélectionnée',
_imagePath == null
? 'Sélectionnez une image'
: 'Image sélectionnée: $_imagePath',
style: const TextStyle(color: Colors.white70),
),
const Icon(Icons.image, color: Colors.white70),
@@ -244,17 +278,69 @@ class _AddEventDialogState extends State<AddEventDialog> {
style: const TextStyle(color: Colors.white),
onSaved: (value) {
_link = value ?? '';
print('Lien sauvegardé: $_link');
},
);
}
Widget _buildSubmitButton() {
return ElevatedButton(
onPressed: () {
onPressed: () async {
if (_formKey.currentState!.validate()) {
_formKey.currentState!.save();
// Logique pour soumettre les données
Navigator.of(context).pop();
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(
title: _title,
description: _description,
date: _selectedDate?.toIso8601String() ?? '',
location: _location,
category: _category,
link: _link,
imageUrl: _imagePath ?? '',
creator: CreatorModel(
id: widget.userId,
nom: widget.userName,
prenoms: widget.userLastName,
),
participants: [
ParticipantModel(
id: widget.userId,
nom: widget.userName,
prenoms: widget.userLastName,
)
],
id: '',
);
// 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'},
body: jsonEncode(eventData),
);
print('Statut de la réponse: ${response.statusCode}');
print('Réponse brute: ${response.body}');
if (response.statusCode == 201) {
// Création réussie
print('Événement créé avec succès');
Navigator.of(context).pop(true);
} else {
// Gérer l'erreur
print('Erreur lors de la création de l\'événement: ${response.reasonPhrase}');
ScaffoldMessenger.of(context).showSnackBar(
SnackBar(content: Text('Erreur: ${response.reasonPhrase}')),
);
}
} else {
print('Le formulaire n\'est pas valide');
}
},
style: ElevatedButton.styleFrom(
@@ -265,40 +351,8 @@ class _AddEventDialogState extends State<AddEventDialog> {
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)),
);
}
}
class LocationPickerScreen extends StatelessWidget {
const LocationPickerScreen({Key? key}) : super(key: key);
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: const Text('Sélectionnez une localisation'),
backgroundColor: const Color(0xFF1E1E2C),
),
body: GoogleMap(
initialCameraPosition: const CameraPosition(
target: LatLng(48.8566, 2.3522), // Paris par défaut
zoom: 12.0,
),
markers: Set<Marker>.of(<Marker>[
Marker(
markerId: const MarkerId('selectedLocation'),
position: LatLng(48.8566, 2.3522), // Position par défaut
draggable: true,
onDragEnd: (newPosition) {
Navigator.of(context).pop(newPosition);
},
)
]),
onTap: (position) {
Navigator.of(context).pop(position);
},
),
child: const Text('Ajouter l\'événement',
style: TextStyle(color: Colors.white)),
);
}
}