import 'package:flutter/material.dart'; import 'package:seshat/domain/models/book.dart'; import 'package:seshat/ui/add_page/view_model/add_view_model.dart'; class ConfirmationPopup extends StatefulWidget { const ConfirmationPopup({ super.key, required this.exitPopup, required this.setPrice, required this.viewModel, required this.book, }); final Function(BuildContext) exitPopup; final Function(num) setPrice; final AddViewModel viewModel; final Book book; @override State createState() => _ConfirmationPopupState(); } class _ConfirmationPopupState extends State { final GlobalKey _formKey = GlobalKey(); num price = 0; @override Widget build(BuildContext context) { final theme = Theme.of(context); return AlertDialog( title: Text("Prix"), content: Form( key: _formKey, child: SingleChildScrollView( child: Column( mainAxisSize: MainAxisSize.min, crossAxisAlignment: CrossAxisAlignment.stretch, children: [ RichText( text: TextSpan( style: theme.textTheme.bodyMedium, children: [ TextSpan( text: "Titre : ", style: TextStyle(fontWeight: FontWeight.bold), ), TextSpan(text: widget.book.title), ], ), ), RichText( text: TextSpan( style: theme.textTheme.bodyMedium, children: [ TextSpan( text: "Auteur·ice : ", style: TextStyle(fontWeight: FontWeight.bold), ), TextSpan(text: widget.book.author), ], ), ), RichText( text: TextSpan( style: theme.textTheme.bodyMedium, children: [ TextSpan( text: "Prix à neuf : ", style: TextStyle(fontWeight: FontWeight.bold), ), TextSpan(text: widget.book.priceNew), ], ), ), SizedBox(height: 10), (widget.viewModel.askPrice) ? TextFormField( decoration: InputDecoration( labelText: "Prix", border: OutlineInputBorder(), suffixText: "€", ), keyboardType: TextInputType.number, validator: (value) { if (value == null || value.isEmpty) { return "Indiquez un prix"; } else if (num.tryParse(value) == null) { return "Le prix doit être un nombre"; } return null; }, onSaved: (newValue) { price = num.parse(newValue!); }, ) : SizedBox(), ], ), ), ), actions: [ TextButton( onPressed: () { ScaffoldMessenger.of(context).showSnackBar( SnackBar( content: Text("Le livre n'a pas été enregistré"), behavior: SnackBarBehavior.floating, ), ); widget.exitPopup(context); }, child: Text("Annuler"), ), TextButton( onPressed: () { switch (widget.viewModel.askPrice) { case true: if (_formKey.currentState!.validate()) { _formKey.currentState!.save(); ScaffoldMessenger.of(context).showSnackBar( SnackBar( content: Text( "\"${widget.book.title}\" ($price) a bien été enregistré", ), behavior: SnackBarBehavior.floating, ), ); widget.exitPopup(context); } break; case false: ScaffoldMessenger.of(context).showSnackBar( SnackBar( content: Text( "\"${widget.book.title}\" (PL) a bien été enregistré", ), behavior: SnackBarBehavior.floating, ), ); widget.exitPopup(context); } }, child: Text("Valider"), ), ], ); } }