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/form_popup.dart

322 lines
9.5 KiB
Dart

import 'package:flutter/material.dart';
import 'package:mobile_scanner/mobile_scanner.dart';
import 'package:seshat/ui/add_page/view_model/add_view_model.dart';
class FormPopup extends StatelessWidget {
const FormPopup({
super.key,
required this.viewModel,
required this.exitPopup,
required this.scannerController,
required this.scanEan,
});
final AddViewModel viewModel;
final Function(BuildContext) exitPopup;
final MobileScannerController scannerController;
final Function scanEan;
@override
Widget build(BuildContext context) {
return AlertDialog(
title: Center(child: Text("Type d'entrée")),
content: Column(
mainAxisSize: MainAxisSize.min,
children: [
Card(
clipBehavior: Clip.hardEdge,
child: InkWell(
splashColor: Colors.blue.withAlpha(30),
onTap: () {
Navigator.of(context).pop();
showDialog(
context: context,
barrierDismissible: false,
builder: (context) {
return _ManualEANPopup(
exitPopup: exitPopup,
viewModel: viewModel,
scannerController: scannerController,
scanEan: scanEan,
);
},
);
},
child: const SizedBox(
width: 300,
height: 100,
child: Center(
child: Row(
mainAxisAlignment: MainAxisAlignment.center,
children: [
Icon(Icons.onetwothree),
SizedBox(width: 10),
Text('Entrer manuellement un EAN'),
],
),
),
),
),
),
Card(
clipBehavior: Clip.hardEdge,
child: InkWell(
splashColor: Colors.blue.withAlpha(30),
onTap: () {
Navigator.of(context).pop();
showDialog(
context: context,
barrierDismissible: false,
builder: (context) {
return _FullyManual(
exitPopup: exitPopup,
viewModel: viewModel,
);
},
);
},
child: const SizedBox(
width: 300,
height: 100,
child: Center(
child: Row(
mainAxisAlignment: MainAxisAlignment.center,
children: [
Icon(Icons.history_edu),
SizedBox(width: 10),
Text('Entrer manuellement un livre'),
],
),
),
),
),
),
],
),
actions: [
TextButton(onPressed: () => exitPopup(context), child: Text("Annuler")),
],
);
}
}
/*
* ======================
* ====< MANUAL EAN >====
* ======================
*/
class _ManualEANPopup extends StatefulWidget {
const _ManualEANPopup({
required this.exitPopup,
required this.viewModel,
required this.scannerController,
required this.scanEan,
});
final Function(BuildContext) exitPopup;
final AddViewModel viewModel;
final MobileScannerController scannerController;
final Function scanEan;
@override
State<_ManualEANPopup> createState() => _ManualEANPopupState();
}
class _ManualEANPopupState extends State<_ManualEANPopup> {
final GlobalKey<FormState> _formKey = GlobalKey<FormState>();
String? ean;
@override
Widget build(BuildContext context) {
return AlertDialog(
title: Text("Entrée manuelle par EAN"),
content: Form(
key: _formKey,
child: Column(
mainAxisSize: MainAxisSize.min,
children: [
TextFormField(
decoration: InputDecoration(
labelText: "EAN",
border: OutlineInputBorder(),
),
keyboardType: TextInputType.number,
onSaved: (newValue) {
ean = newValue;
},
validator: (value) {
if (value == null ||
value.length != 13 ||
int.tryParse(value) == null ||
int.parse(value) < 0) {
return "L'entrée n'est pas un code EAN-13 valide";
}
return null;
},
),
],
),
),
actions: [
TextButton(
child: Text("Annuler"),
onPressed: () {
widget.exitPopup(context);
},
),
TextButton(
child: Text("Valider"),
onPressed: () {
if (_formKey.currentState!.validate()) {
_formKey.currentState!.save();
widget.scanEan(
context,
widget.viewModel,
ean!,
widget.scannerController,
leaveLastPopup: (context) {
Navigator.of(context).pop();
},
);
}
},
),
],
);
}
}
class _FullyManual extends StatefulWidget {
const _FullyManual({required this.exitPopup, required this.viewModel});
final Function(BuildContext) exitPopup;
final AddViewModel viewModel;
@override
State<_FullyManual> createState() => _FullyManualState();
}
class _FullyManualState extends State<_FullyManual> {
final GlobalKey<FormState> _formKey = GlobalKey<FormState>();
String? ean = "";
String? title;
String? author;
num? price;
@override
Widget build(BuildContext context) {
return AlertDialog(
title: Text("Entrée manuelle"),
content: Form(
key: _formKey,
child: Column(
mainAxisSize: MainAxisSize.min,
children: [
TextFormField(
decoration: InputDecoration(
labelText: "EAN",
helperText: "Optionnel",
border: OutlineInputBorder(),
),
keyboardType: TextInputType.number,
validator: (value) {
if (value == null ||
(value.length != 13 && value.isNotEmpty) ||
int.tryParse(value) != null) {
return "Indiquez un EAN valide ou rien";
}
return null;
},
onSaved: (newValue) {
ean = newValue;
},
),
SizedBox(height: 10),
TextFormField(
decoration: InputDecoration(
labelText: "Titre",
border: OutlineInputBorder(),
),
validator: (value) {
if (value == null || value.isEmpty) {
return "Indiquez un titre";
}
return null;
},
onSaved: (newValue) {
title = newValue?.trim();
},
),
SizedBox(height: 10),
TextFormField(
decoration: InputDecoration(
labelText: "Auteur·ice",
border: OutlineInputBorder(),
),
validator: (value) {
if (value == null || value.isEmpty) {
return "Indiquez un·e auteur·ice";
}
return null;
},
onSaved: (newValue) {
author = newValue?.trim();
},
),
SizedBox(height: 10),
ListenableBuilder(
listenable: widget.viewModel,
builder: (context, child) {
return (widget.viewModel.askPrice)
? TextFormField(
decoration: InputDecoration(
labelText: "Prix",
border: OutlineInputBorder(),
suffixText: "",
),
keyboardType: TextInputType.numberWithOptions(
decimal: true,
signed: true,
),
validator: (value) {
if (value == null || value.isEmpty) {
return "Indiquez un prix";
} else if (num.tryParse(value) == null) {
return "Le prix doit être un nombre";
} else if (num.parse(value) < 0) {
return "Le prix doit être positif ou nul";
}
return null;
},
onSaved: (newValue) {
price = num.parse(newValue!);
},
)
: SizedBox();
},
),
],
),
),
actions: [
TextButton(
onPressed: () {
widget.exitPopup(context);
},
child: Text("Annuler"),
),
TextButton(
onPressed: () {
if (_formKey.currentState!.validate()) {
_formKey.currentState!.save();
widget.exitPopup(context);
}
},
child: Text("Valider"),
),
],
);
}
}