diff --git a/lib/ui/add_page/widgets/add_page.dart b/lib/ui/add_page/widgets/add_page.dart index 5cc3382..31022c3 100644 --- a/lib/ui/add_page/widgets/add_page.dart +++ b/lib/ui/add_page/widgets/add_page.dart @@ -2,6 +2,7 @@ import 'package:flutter/material.dart'; import 'package:flutter_svg/svg.dart'; import 'package:mobile_scanner/mobile_scanner.dart'; import 'package:seshat/ui/add_page/view_model/add_view_model.dart'; +import 'package:seshat/ui/add_page/widgets/form_popup.dart'; import 'package:seshat/ui/add_page/widgets/owner_popup.dart'; import 'package:seshat/ui/core/ui/navigation_bar.dart'; @@ -22,6 +23,8 @@ class _AddPageState extends State { formats: [BarcodeFormat.ean13], detectionTimeoutMs: 1000, ); + + final theme = Theme.of(context); // return Consumer( // builder: (context, screen, child) { return Scaffold( @@ -85,7 +88,11 @@ class _AddPageState extends State { SvgPicture.asset('assets/scan-overlay.svg'), Expanded(child: SizedBox()), TextButton( - onPressed: () {}, + style: ButtonStyle( + backgroundColor: WidgetStatePropertyAll(theme.cardColor), + ), + onPressed: () => + _formDialogBuilder(context, controller, widget.viewModel), child: Text("Enregistrer manuellement"), ), ], @@ -106,6 +113,25 @@ void onBarcodeScan( return; } +Future _formDialogBuilder( + BuildContext context, + MobileScannerController controller, + AddViewModel viewModel, +) { + controller.stop(); + + void exitPopup(BuildContext localContext) { + Navigator.of(localContext).pop(); + controller.start(); + } + + return showDialog( + context: context, + barrierDismissible: false, + builder: (context) => FormPopup(viewModel: viewModel, exitPopup: exitPopup), + ); +} + Future _ownerDialogBuilder( BuildContext context, MobileScannerController controller, @@ -114,8 +140,8 @@ Future _ownerDialogBuilder( controller.stop(); void onPressAccept(BuildContext localContext) { - controller.start(); Navigator.of(localContext).pop(); + controller.start(); } return showDialog( diff --git a/lib/ui/add_page/widgets/form_popup.dart b/lib/ui/add_page/widgets/form_popup.dart new file mode 100644 index 0000000..519ea0d --- /dev/null +++ b/lib/ui/add_page/widgets/form_popup.dart @@ -0,0 +1,252 @@ +import 'package:flutter/material.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, + }); + + final AddViewModel viewModel; + final Function(BuildContext) exitPopup; + + @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: () { + debugPrint('Card tapped.'); + Navigator.of(context).pop(); + showDialog( + context: context, + barrierDismissible: false, + builder: (context) { + return _ManualEANPopup(exitPopup: exitPopup); + }, + ); + }, + 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: () { + debugPrint('Card tapped.'); + Navigator.of(context).pop(); + showDialog( + context: context, + barrierDismissible: false, + builder: (context) { + return _FullyManual(exitPopup: exitPopup); + }, + ); + }, + 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")), + ], + ); + } +} + +class _ManualEANPopup extends StatefulWidget { + const _ManualEANPopup({required this.exitPopup}); + + final Function(BuildContext) exitPopup; + + @override + State<_ManualEANPopup> createState() => _ManualEANPopupState(); +} + +class _ManualEANPopupState extends State<_ManualEANPopup> { + final GlobalKey _formKey = GlobalKey(); + String? ean; + @override + Widget build(BuildContext context) { + return AlertDialog( + title: Text("Recherche 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) { + return "L'entrée n'est pas un code EAN-13 valide"; + } + return null; + }, + ), + ], + ), + ), + actions: [ + TextButton( + onPressed: () { + widget.exitPopup(context); + }, + child: Text("Annuler"), + ), + TextButton( + onPressed: () { + if (_formKey.currentState!.validate()) { + _formKey.currentState!.save(); + widget.exitPopup(context); + } + }, + child: Text("Valider"), + ), + ], + ); + } +} + +class _FullyManual extends StatefulWidget { + const _FullyManual({required this.exitPopup}); + + final Function(BuildContext) exitPopup; + + @override + State<_FullyManual> createState() => _FullyManualState(); +} + +class _FullyManualState extends State<_FullyManual> { + final GlobalKey _formKey = GlobalKey(); + String? ean = ""; + String? title; + String? author; + + @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; + }, + ), + 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; + }, + ), + ], + ), + ), + actions: [ + TextButton( + onPressed: () { + widget.exitPopup(context); + }, + child: Text("Annuler"), + ), + TextButton( + onPressed: () { + if (_formKey.currentState!.validate()) { + _formKey.currentState!.save(); + widget.exitPopup(context); + } + }, + child: Text("Valider"), + ), + ], + ); + } +} diff --git a/lib/ui/add_page/widgets/form_screen.dart b/lib/ui/add_page/widgets/form_screen.dart deleted file mode 100644 index e69de29..0000000 diff --git a/lib/ui/add_page/widgets/owner_popup.dart b/lib/ui/add_page/widgets/owner_popup.dart index d0829ca..b85f5e8 100644 --- a/lib/ui/add_page/widgets/owner_popup.dart +++ b/lib/ui/add_page/widgets/owner_popup.dart @@ -143,9 +143,9 @@ class _OwnerPopupState extends State { onPressed: () { if (_formKey.currentState!.validate()) { _formKey.currentState!.save(); + widget.viewModel.currentOwner = widget.viewModel + .addOwner(firstName!, lastName!, contact!); setState(() { - widget.viewModel.currentOwner = widget.viewModel - .addOwner(firstName!, lastName!, contact!); showNewOwner = false; }); }