import 'package:flutter/material.dart'; import 'package:manager_api_new/api.dart'; import 'package:manager_app/constants.dart'; import 'package:manager_app/Components/rounded_button.dart'; import 'package:manager_app/Components/multi_string_input_container.dart'; import 'package:intl/intl.dart'; void showNewOrUpdateProgrammeBlock( BuildContext context, ProgrammeBlock? block, Function(ProgrammeBlock) onSave, ) { ProgrammeBlock workingBlock = block != null ? ProgrammeBlock.fromJson(block.toJson())! : ProgrammeBlock( title: [], description: [], startTime: DateTime.now(), endTime: DateTime.now().add(Duration(hours: 1)), ); showDialog( context: context, builder: (BuildContext context) { return StatefulBuilder( builder: (context, setState) { final double screenWidth = MediaQuery.of(context).size.width; final double screenHeight = MediaQuery.of(context).size.height; final double dialogWidth = screenWidth * 0.7; final double contentWidth = dialogWidth - 48; final double halfWidth = (contentWidth - 20) / 2; return Dialog( shape: RoundedRectangleBorder(borderRadius: BorderRadius.circular(16)), child: Container( width: dialogWidth, constraints: BoxConstraints(maxHeight: screenHeight * 0.85), padding: const EdgeInsets.all(24), child: Column( mainAxisSize: MainAxisSize.min, crossAxisAlignment: CrossAxisAlignment.start, children: [ Text( block == null ? "Nouveau Bloc de Programme" : "Modifier le Bloc", style: TextStyle( color: kPrimaryColor, fontSize: 20, fontWeight: FontWeight.bold), ), SizedBox(height: 16), Flexible( child: SingleChildScrollView( child: Column( crossAxisAlignment: CrossAxisAlignment.start, children: [ // Titre + Description côte à côte Row( children: [ SizedBox( width: halfWidth, child: MultiStringInputContainer( label: "Titre :", modalLabel: "Titre du bloc", initialValue: workingBlock.title ?? [], onGetResult: (val) => setState(() => workingBlock.title = val), maxLines: 1, isTitle: true, ), ), SizedBox(width: 20), SizedBox( width: halfWidth, child: MultiStringInputContainer( label: "Description :", modalLabel: "Description du bloc", initialValue: workingBlock.description ?? [], onGetResult: (val) => setState( () => workingBlock.description = val), maxLines: 1, isTitle: false, ), ), ], ), Divider(height: 24), // Heures côte à côte Row( children: [ SizedBox( width: halfWidth, child: ListTile( leading: Icon(Icons.schedule, color: kPrimaryColor), title: Text("Heure de début"), subtitle: Text( workingBlock.startTime != null ? DateFormat('HH:mm') .format(workingBlock.startTime!) : "Non définie", style: TextStyle( color: kPrimaryColor, fontWeight: FontWeight.bold), ), onTap: () async { TimeOfDay? time = await showTimePicker( context: context, initialTime: TimeOfDay.fromDateTime( workingBlock.startTime ?? 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(() { DateTime now = DateTime.now(); workingBlock.startTime = DateTime( now.year, now.month, now.day, time.hour, time.minute); }); } }, ), ), SizedBox(width: 20), SizedBox( width: halfWidth, child: ListTile( leading: Icon(Icons.schedule_outlined, color: kPrimaryColor), title: Text("Heure de fin"), subtitle: Text( workingBlock.endTime != null ? DateFormat('HH:mm') .format(workingBlock.endTime!) : "Non définie", style: TextStyle( color: kPrimaryColor, fontWeight: FontWeight.bold), ), onTap: () async { TimeOfDay? time = await showTimePicker( context: context, initialTime: TimeOfDay.fromDateTime( workingBlock.endTime ?? DateTime.now() .add(Duration(hours: 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(() { DateTime now = DateTime.now(); workingBlock.endTime = DateTime( now.year, now.month, now.day, time.hour, time.minute); }); } }, ), ), ], ), ], ), ), ), SizedBox(height: 16), Row( mainAxisAlignment: MainAxisAlignment.end, children: [ SizedBox( height: 46, child: RoundedButton( text: "Annuler", press: () => Navigator.pop(context), color: kSecond, fontSize: 15, horizontal: 24, ), ), SizedBox(width: 12), SizedBox( height: 46, child: RoundedButton( text: "Sauvegarder", press: () { onSave(workingBlock); Navigator.pop(context); }, color: kPrimaryColor, fontSize: 15, horizontal: 24, ), ), ], ), ], ), ), ); }, ); }, ); }