feat: add price popup
This commit is contained in:
parent
60265b9735
commit
70146055df
2 changed files with 104 additions and 5 deletions
|
|
@ -4,6 +4,7 @@ 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/view_model/add_view_model.dart';
|
||||||
import 'package:seshat/ui/add_page/widgets/form_popup.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/add_page/widgets/owner_popup.dart';
|
||||||
|
import 'package:seshat/ui/add_page/widgets/price_popup.dart';
|
||||||
import 'package:seshat/ui/core/ui/navigation_bar.dart';
|
import 'package:seshat/ui/core/ui/navigation_bar.dart';
|
||||||
|
|
||||||
class AddPage extends StatefulWidget {
|
class AddPage extends StatefulWidget {
|
||||||
|
|
@ -16,6 +17,7 @@ class AddPage extends StatefulWidget {
|
||||||
}
|
}
|
||||||
|
|
||||||
class _AddPageState extends State<AddPage> {
|
class _AddPageState extends State<AddPage> {
|
||||||
|
num? price;
|
||||||
@override
|
@override
|
||||||
Widget build(BuildContext context) {
|
Widget build(BuildContext context) {
|
||||||
final MobileScannerController controller = MobileScannerController(
|
final MobileScannerController controller = MobileScannerController(
|
||||||
|
|
@ -33,8 +35,28 @@ class _AddPageState extends State<AddPage> {
|
||||||
ColoredBox(color: Colors.black),
|
ColoredBox(color: Colors.black),
|
||||||
MobileScanner(
|
MobileScanner(
|
||||||
controller: controller,
|
controller: controller,
|
||||||
onDetect: (barcodes) {
|
onDetect: (barcodes) async {
|
||||||
onBarcodeScan(barcodes, controller);
|
void setPrice(num newPrice) {
|
||||||
|
setState(() {
|
||||||
|
price = newPrice;
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
if (widget.viewModel.askPrice) {
|
||||||
|
await _priceDialogBuilder(context, setPrice, controller);
|
||||||
|
} else {
|
||||||
|
setPrice(0);
|
||||||
|
}
|
||||||
|
|
||||||
|
ScaffoldMessenger.of(context).showSnackBar(
|
||||||
|
SnackBar(
|
||||||
|
content: Text(
|
||||||
|
"Envoyé : ${barcodes.barcodes.first.rawValue} pour $price€",
|
||||||
|
),
|
||||||
|
behavior: SnackBarBehavior.floating,
|
||||||
|
),
|
||||||
|
);
|
||||||
|
debugPrint(price.toString());
|
||||||
},
|
},
|
||||||
),
|
),
|
||||||
SafeArea(
|
SafeArea(
|
||||||
|
|
@ -106,11 +128,23 @@ class _AddPageState extends State<AddPage> {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void onBarcodeScan(
|
Future<void> _priceDialogBuilder(
|
||||||
BarcodeCapture barcodes,
|
BuildContext context,
|
||||||
|
Function(num) setPrice,
|
||||||
MobileScannerController controller,
|
MobileScannerController controller,
|
||||||
) {
|
) {
|
||||||
return;
|
controller.stop();
|
||||||
|
|
||||||
|
void exitPopup(BuildContext localContext) {
|
||||||
|
Navigator.of(localContext).pop();
|
||||||
|
controller.start();
|
||||||
|
}
|
||||||
|
|
||||||
|
return showDialog(
|
||||||
|
context: context,
|
||||||
|
barrierDismissible: false,
|
||||||
|
builder: (context) => PricePopup(exitPopup: exitPopup, setPrice: setPrice),
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
Future<void> _formDialogBuilder(
|
Future<void> _formDialogBuilder(
|
||||||
|
|
|
||||||
65
lib/ui/add_page/widgets/price_popup.dart
Normal file
65
lib/ui/add_page/widgets/price_popup.dart
Normal file
|
|
@ -0,0 +1,65 @@
|
||||||
|
import 'package:flutter/material.dart';
|
||||||
|
|
||||||
|
class PricePopup extends StatefulWidget {
|
||||||
|
const PricePopup({
|
||||||
|
super.key,
|
||||||
|
required this.exitPopup,
|
||||||
|
required this.setPrice,
|
||||||
|
});
|
||||||
|
|
||||||
|
final Function(BuildContext) exitPopup;
|
||||||
|
final Function(num) setPrice;
|
||||||
|
|
||||||
|
@override
|
||||||
|
State<PricePopup> createState() => _PricePopupState();
|
||||||
|
}
|
||||||
|
|
||||||
|
class _PricePopupState extends State<PricePopup> {
|
||||||
|
final GlobalKey<FormState> _formKey = GlobalKey<FormState>();
|
||||||
|
num? price;
|
||||||
|
@override
|
||||||
|
Widget build(BuildContext context) {
|
||||||
|
return AlertDialog(
|
||||||
|
title: Text("Prix"),
|
||||||
|
content: Form(
|
||||||
|
key: _formKey,
|
||||||
|
child: Column(
|
||||||
|
mainAxisSize: MainAxisSize.min,
|
||||||
|
children: [
|
||||||
|
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!);
|
||||||
|
},
|
||||||
|
),
|
||||||
|
],
|
||||||
|
),
|
||||||
|
),
|
||||||
|
actions: [
|
||||||
|
TextButton(
|
||||||
|
onPressed: () {
|
||||||
|
if (_formKey.currentState!.validate()) {
|
||||||
|
_formKey.currentState!.save();
|
||||||
|
widget.setPrice(price!);
|
||||||
|
widget.exitPopup(context);
|
||||||
|
}
|
||||||
|
},
|
||||||
|
child: Text("Valider"),
|
||||||
|
),
|
||||||
|
],
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in a new issue