This repository has been archived on 2025-08-25. You can view files and clone it, but you cannot make any changes to it's state, such as pushing and creating new issues, pull requests or comments.
Seshat/lib/ui/add_page/widgets/confirmation_popup.dart

174 lines
5.8 KiB
Dart

import 'package:flutter/material.dart';
import 'package:seshat/domain/models/bal.dart';
import 'package:seshat/domain/models/book.dart';
import 'package:seshat/ui/add_page/view_model/add_view_model.dart';
import 'package:seshat/utils/result.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<ConfirmationPopup> createState() => _ConfirmationPopupState();
}
class _ConfirmationPopupState extends State<ConfirmationPopup> {
final GlobalKey<FormState> _formKey = GlobalKey<FormState>();
double 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.replaceAll("EUR", "")),
],
),
),
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 = double.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: () async {
var result = await widget.viewModel.sendBook(
widget.book,
widget.viewModel.currentOwner!,
Bal(id: 1),
price,
);
switch (result) {
case Ok():
switch (widget.viewModel.askPrice) {
case true:
if (_formKey.currentState!.validate()) {
_formKey.currentState!.save();
if (context.mounted) {
ScaffoldMessenger.of(context).showSnackBar(
SnackBar(
content: Text(
"\"${widget.book.title}\" ($price€) a bien été enregistré",
),
behavior: SnackBarBehavior.floating,
),
);
widget.exitPopup(context);
}
}
break;
case false:
if (context.mounted) {
ScaffoldMessenger.of(context).showSnackBar(
SnackBar(
content: Text(
"\"${widget.book.title}\" (PL) a bien été enregistré",
),
behavior: SnackBarBehavior.floating,
),
);
widget.exitPopup(context);
}
}
break;
case Error():
if (context.mounted) {
ScaffoldMessenger.of(context).showSnackBar(
SnackBar(
content: Text(
"Une erreur est survenue : ${result.error}",
),
),
);
}
}
},
child: Text("Valider"),
),
],
);
}
}