import 'package:flutter/material.dart'; import 'package:intl/date_symbol_data_local.dart'; import 'package:intl/intl.dart'; import 'package:seshat/ui/home_page/view_model/home_view_model.dart'; class CreateConfirmationPopup extends StatefulWidget { const CreateConfirmationPopup({super.key, required this.viewModel}); final HomeViewModel viewModel; @override State createState() => _CreateConfirmationPopupState(); } class _CreateConfirmationPopupState extends State { final GlobalKey _formKey = GlobalKey(); String? name; DateTime? start; Future _selectStart() async { final DateTime? pickedDate = await showDatePicker( context: context, initialDate: start ?? DateTime.now(), firstDate: DateTime.now(), lastDate: DateTime(DateTime.now().year + 2), locale: Locale("fr"), ); setState(() { start = pickedDate; }); } DateTime? end; Future _selectEnd() async { final DateTime? pickedDate = await showDatePicker( context: context, initialDate: end ?? DateTime.now(), firstDate: DateTime.now(), lastDate: DateTime(DateTime.now().year + 2), locale: Locale("fr"), ); setState(() { end = pickedDate; }); } @override Widget build(BuildContext context) { initializeDateFormatting(); var format = DateFormat("dd MMM yyyy", "fr"); return AlertDialog( title: Text("Créer une BAL"), content: Column( mainAxisSize: MainAxisSize.min, children: [ Form( key: _formKey, child: Column( mainAxisSize: MainAxisSize.min, children: [ TextFormField( decoration: InputDecoration( labelText: "Nom de la BAL", border: OutlineInputBorder(), ), validator: (value) { if (value == null || value.isEmpty) { return "Veuillez entrer un nom"; } return null; }, onSaved: (newValue) { name = newValue; }, ), Row( children: [ Text("Date de début : "), TextButton( onPressed: () { _selectStart(); }, child: Text(format.format(start ?? DateTime.now())), ), ], ), Row( children: [ Text("Date de fin : "), TextButton( onPressed: () { _selectEnd(); }, child: Text(format.format(end ?? DateTime.now())), ), ], ), Text("Note: Les dates sont à titre purement indicatif."), ], ), ), ], ), actions: [ TextButton( onPressed: () { Navigator.of(context).pop(); }, child: Text("Annuler"), ), TextButton( onPressed: () async { if (_formKey.currentState!.validate() && start != null && end != null) { _formKey.currentState!.save(); await widget.viewModel.createBal(name!, start!, end!); if (context.mounted) { Navigator.of(context).pop(); } } else { ScaffoldMessenger.of(context).showSnackBar( SnackBar( content: Text( "Veuillez indiquer une date de début et de fin.", ), ), ); } }, child: Text("Valider"), ), ], ); } }