import 'package:flutter/material.dart'; import 'package:manager_api_new/api.dart'; import 'package:manager_app/constants.dart'; import 'package:intl/intl.dart'; import 'showNewOrUpdateProgrammeBlock.dart'; class EventConfig extends StatefulWidget { final SectionEventDTO initialValue; final ValueChanged onChanged; const EventConfig({ Key? key, required this.initialValue, required this.onChanged, }) : super(key: key); @override _EventConfigState createState() => _EventConfigState(); } class _EventConfigState extends State { late SectionEventDTO eventDTO; @override void initState() { super.initState(); eventDTO = widget.initialValue; } @override Widget build(BuildContext context) { return Column( crossAxisAlignment: CrossAxisAlignment.start, children: [ Padding( padding: const EdgeInsets.all(16.0), child: Row( children: [ Expanded( child: ListTile( title: Text("Date de début"), subtitle: Text(eventDTO.startDate != null ? DateFormat('dd/MM/yyyy HH:mm') .format(eventDTO.startDate!) : "Non définie"), trailing: Icon(Icons.calendar_today), onTap: () async { DateTime initialDate = eventDTO.startDate ?? DateTime.now(); if (initialDate.isBefore(DateTime(2000))) { initialDate = DateTime.now(); } DateTime? picked = await showDatePicker( context: context, initialDate: initialDate, firstDate: DateTime(2000), lastDate: DateTime(2100), builder: (context, child) { return Theme( data: Theme.of(context).copyWith( colorScheme: ColorScheme.light( primary: kPrimaryColor, onPrimary: kWhite, onSurface: kSecond, ), ), child: child!, ); }, ); if (picked != null) { TimeOfDay? time = await showTimePicker( context: context, initialTime: TimeOfDay.fromDateTime( eventDTO.startDate ?? DateTime.now()), builder: (context, child) { return Theme( data: Theme.of(context).copyWith( colorScheme: ColorScheme.light( primary: kPrimaryColor, onPrimary: kWhite, onSurface: kSecond, ), ), child: child!, ); }, ); if (time != null) { setState(() { eventDTO.startDate = DateTime(picked.year, picked.month, picked.day, time.hour, time.minute); widget.onChanged(eventDTO); }); } } }, ), ), Expanded( child: ListTile( title: Text("Date de fin"), subtitle: Text(eventDTO.endDate != null ? DateFormat('dd/MM/yyyy HH:mm').format(eventDTO.endDate!) : "Non définie"), trailing: Icon(Icons.calendar_today), onTap: () async { DateTime initialDate = eventDTO.endDate ?? DateTime.now().add(Duration(days: 1)); if (initialDate.isBefore(DateTime(2000))) { initialDate = DateTime.now().add(Duration(days: 1)); } DateTime? picked = await showDatePicker( context: context, initialDate: initialDate, firstDate: DateTime(2000), lastDate: DateTime(2100), builder: (context, child) { return Theme( data: Theme.of(context).copyWith( colorScheme: ColorScheme.light( primary: kPrimaryColor, onPrimary: kWhite, onSurface: kSecond, ), ), child: child!, ); }, ); if (picked != null) { TimeOfDay? time = await showTimePicker( context: context, initialTime: TimeOfDay.fromDateTime(eventDTO.endDate ?? DateTime.now().add(Duration(days: 1))), builder: (context, child) { return Theme( data: Theme.of(context).copyWith( colorScheme: ColorScheme.light( primary: kPrimaryColor, onPrimary: kWhite, onSurface: kSecond, ), ), child: child!, ); }, ); if (time != null) { setState(() { eventDTO.endDate = DateTime(picked.year, picked.month, picked.day, time.hour, time.minute); widget.onChanged(eventDTO); }); } } }, ), ), ], ), ), Divider(), Padding( padding: const EdgeInsets.symmetric(horizontal: 16.0, vertical: 8.0), child: Row( mainAxisAlignment: MainAxisAlignment.spaceBetween, children: [ Text("Programme", style: TextStyle(fontSize: 18, fontWeight: FontWeight.bold)), ElevatedButton.icon( icon: Icon(Icons.add), label: Text("Ajouter un bloc"), onPressed: () { showNewOrUpdateProgrammeBlock( context, null, (newBlock) { setState(() { eventDTO.programme = [ ...(eventDTO.programme ?? []), newBlock ]; widget.onChanged(eventDTO); }); }, ); }, style: ElevatedButton.styleFrom( backgroundColor: kSuccess, foregroundColor: kWhite), ), ], ), ), Expanded( child: (eventDTO.programme == null || eventDTO.programme!.isEmpty) ? Center( child: Text("Aucun bloc de programme défini", style: TextStyle(fontStyle: FontStyle.italic))) : ListView.builder( itemCount: eventDTO.programme!.length, itemBuilder: (context, index) { final block = eventDTO.programme![index]; return Card( margin: EdgeInsets.symmetric(horizontal: 16, vertical: 4), child: ListTile( title: Text( block.title != null && block.title!.isNotEmpty ? block.title! .firstWhere((t) => t.language == 'FR', orElse: () => block.title![0]) .value ?? "Bloc ${index + 1}" : "Bloc ${index + 1}"), subtitle: Text( "${block.startTime != null ? DateFormat('HH:mm').format(block.startTime!) : '??'} - ${block.endTime != null ? DateFormat('HH:mm').format(block.endTime!) : '??'}"), trailing: Row( mainAxisSize: MainAxisSize.min, children: [ IconButton( icon: Icon(Icons.edit, color: kPrimaryColor), onPressed: () { showNewOrUpdateProgrammeBlock( context, block, (updatedBlock) { setState(() { eventDTO.programme![index] = updatedBlock; widget.onChanged(eventDTO); }); }, ); }, ), IconButton( icon: Icon(Icons.delete, color: kError), onPressed: () { setState(() { eventDTO.programme!.removeAt(index); widget.onChanged(eventDTO); }); }, ), ], ), ), ); }, ), ), ], ); } }