Ajout des options

This commit is contained in:
2025-05-27 22:55:34 +02:00
parent 9489183b68
commit 50a38816d3
5 changed files with 787 additions and 140 deletions

View File

@ -19,6 +19,7 @@ import 'package:flutter/foundation.dart';
import 'package:flutter_dropzone/flutter_dropzone.dart';
import 'package:em2rp/views/widgets/inputs/dropzone_upload_widget.dart';
import 'package:em2rp/views/widgets/user_management/user_multi_select_widget.dart';
import 'package:em2rp/views/widgets/inputs/option_selector_widget.dart';
class EventAddPage extends StatefulWidget {
const EventAddPage({super.key});
@ -31,7 +32,7 @@ class _EventAddPageState extends State<EventAddPage> {
final _formKey = GlobalKey<FormState>();
final TextEditingController _nameController = TextEditingController();
final TextEditingController _descriptionController = TextEditingController();
final TextEditingController _priceController = TextEditingController();
final TextEditingController _basePriceController = TextEditingController();
final TextEditingController _installationController = TextEditingController();
final TextEditingController _disassemblyController = TextEditingController();
final TextEditingController _addressController = TextEditingController();
@ -49,6 +50,7 @@ class _EventAddPageState extends State<EventAddPage> {
List<Map<String, String>> _uploadedFiles = [];
DropzoneViewController? _dropzoneController;
bool _isDropzoneHighlighted = false;
List<Map<String, dynamic>> _selectedOptions = [];
@override
void initState() {
@ -74,11 +76,37 @@ class _EventAddPageState extends State<EventAddPage> {
});
}
void _onEventTypeChanged(String? newType) {
if (newType == _selectedEventType) return;
final oldType = _selectedEventType;
setState(() {
_selectedEventType = newType;
if (newType != null) {
// Efface les options non compatibles
final before = _selectedOptions.length;
_selectedOptions.removeWhere((opt) {
final types = opt['compatibleTypes'] as List<String>?;
if (types == null) return true;
return !types.contains(newType);
});
if (_selectedOptions.length < before && context.mounted) {
ScaffoldMessenger.of(context).showSnackBar(
SnackBar(
content: Text(
'Certaines options ont été retirées car elles ne sont pas compatibles avec le type "$newType".')),
);
}
} else {
_selectedOptions.clear();
}
});
}
@override
void dispose() {
_nameController.dispose();
_descriptionController.dispose();
_priceController.dispose();
_basePriceController.dispose();
_installationController.dispose();
_disassemblyController.dispose();
_addressController.dispose();
@ -181,7 +209,7 @@ class _EventAddPageState extends State<EventAddPage> {
description: _descriptionController.text.trim(),
startDateTime: _startDateTime!,
endDateTime: _endDateTime!,
price: double.tryParse(_priceController.text) ?? 0.0,
basePrice: double.tryParse(_basePriceController.text) ?? 0.0,
installationTime: int.tryParse(_installationController.text) ?? 0,
disassemblyTime: int.tryParse(_disassemblyController.text) ?? 0,
eventTypeId: _selectedEventType!,
@ -193,6 +221,12 @@ class _EventAddPageState extends State<EventAddPage> {
latitude: 0.0,
longitude: 0.0,
documents: _uploadedFiles,
options: _selectedOptions
.map((opt) => {
'name': opt['name'],
'price': opt['price'],
})
.toList(),
);
final docRef = await FirebaseFirestore.instance
.collection('events')
@ -313,8 +347,7 @@ class _EventAddPageState extends State<EventAddPage> {
child: Text(type),
))
.toList(),
onChanged: (val) =>
setState(() => _selectedEventType = val),
onChanged: _onEventTypeChanged,
decoration: const InputDecoration(
labelText: 'Type d\'événement',
border: OutlineInputBorder(),
@ -446,9 +479,9 @@ class _EventAddPageState extends State<EventAddPage> {
),
const SizedBox(height: 16),
TextFormField(
controller: _priceController,
controller: _basePriceController,
decoration: const InputDecoration(
labelText: 'Prix (€)',
labelText: 'Prix de base (€)',
border: OutlineInputBorder(),
prefixIcon: Icon(Icons.euro),
hintText: '1050.50',
@ -461,19 +494,30 @@ class _EventAddPageState extends State<EventAddPage> {
],
validator: (value) {
if (value == null || value.isEmpty) {
return 'Le prix est requis';
return 'Le prix de base est requis';
}
final price =
double.tryParse(value.replaceAll(',', '.'));
if (price == null) {
return 'Veuillez entrer un nombre valide';
}
if (price < 0) {
return 'Le prix ne peut pas être négatif';
}
return null;
},
),
const SizedBox(height: 16),
OptionSelectorWidget(
eventType: _selectedEventType,
selectedOptions: _selectedOptions,
onChanged: (opts) =>
setState(() => _selectedOptions = opts),
onRemove: (name) {
setState(() {
_selectedOptions
.removeWhere((o) => o['name'] == name);
});
},
eventTypeRequired: _selectedEventType == null,
),
_buildSectionTitle('Détails'),
AnimatedContainer(
duration: const Duration(milliseconds: 200),