From 6799e9c85b09068b195f31f077f26bdc7382216b Mon Sep 17 00:00:00 2001 From: Oliver Walters Date: Fri, 10 Apr 2020 00:19:01 +1000 Subject: [PATCH] Cleanup of action dialogs --- lib/widget/stock_detail.dart | 32 +++++++++++++++++++------------- 1 file changed, 19 insertions(+), 13 deletions(-) diff --git a/lib/widget/stock_detail.dart b/lib/widget/stock_detail.dart index aabd6c7c..54cf2927 100644 --- a/lib/widget/stock_detail.dart +++ b/lib/widget/stock_detail.dart @@ -27,7 +27,7 @@ class StockDetailWidget extends StatefulWidget { class _StockItemDisplayState extends State { - // Single TextEditingController which can be shared between dialogs + final TextEditingController _quantityController = TextEditingController(); final TextEditingController _notesController = TextEditingController(); final _addStockKey = GlobalKey(); @@ -49,6 +49,9 @@ class _StockItemDisplayState extends State { Navigator.of(context).pop(); + double quantity = double.parse(_quantityController.text); + _quantityController.clear(); + // Await response to prevent the button from being pressed multiple times var response = await item.addStock(quantity, notes: _notesController.text); _notesController.clear(); @@ -70,7 +73,7 @@ class _StockItemDisplayState extends State { FlatButton( child: Text("Add"), onPressed: () { - _addStockKey.currentState.validate(); + if (_addStockKey.currentState.validate()) _addStock(); }, ) ], @@ -87,6 +90,7 @@ class _StockItemDisplayState extends State { labelText: "Add stock", ), keyboardType: TextInputType.numberWithOptions(signed: false, decimal: true), + controller: _quantityController, validator: (value) { if (value.isEmpty) return "Value cannot be empty"; @@ -94,8 +98,6 @@ class _StockItemDisplayState extends State { if (quantity == null) return "Value cannot be converted to a number"; if (quantity <= 0) return "Value must be positive"; - _addStock(quantity); - return null; }, ), @@ -114,9 +116,12 @@ class _StockItemDisplayState extends State { // TODO - Form for adding stock } - void _removeStock(double quantity) async { + void _removeStock() async { Navigator.of(context).pop(); + double quantity = double.parse(_quantityController.text); + _quantityController.clear(); + var response = await item.removeStock(quantity, notes: _notesController.text); _notesController.clear(); @@ -136,7 +141,7 @@ class _StockItemDisplayState extends State { FlatButton( child: Text("Remove"), onPressed: () { - _removeStockKey.currentState.validate(); + if (_removeStockKey.currentState.validate()) _removeStock(); }, ) ], @@ -152,6 +157,7 @@ class _StockItemDisplayState extends State { decoration: InputDecoration( labelText: "Remove stock", ), + controller: _quantityController, keyboardType: TextInputType.numberWithOptions(signed: false, decimal: true), validator: (value) { if (value.isEmpty) return "Value cannot be empty"; @@ -163,8 +169,6 @@ class _StockItemDisplayState extends State { if (quantity > item.quantity) return "Cannot take more than current quantity"; - _removeStock(quantity); - return null; }, ), @@ -182,10 +186,13 @@ class _StockItemDisplayState extends State { ); } - void _countStock(double quantity) async { + void _countStock() async { Navigator.of(context).pop(); + double quantity = double.parse(_quantityController.text); + _quantityController.clear(); + var response = await item.countStock(quantity, notes: _notesController.text); _notesController.clear(); @@ -207,7 +214,7 @@ class _StockItemDisplayState extends State { FlatButton( child: Text("Count"), onPressed: () { - _countStockKey.currentState.validate(); + if (_countStockKey.currentState.validate()) _countStock(); }, ) ], @@ -223,6 +230,7 @@ class _StockItemDisplayState extends State { labelText: "Count stock", hintText: "${item.quantity}", ), + controller: _quantityController, keyboardType: TextInputType.numberWithOptions(signed: false, decimal: true), validator: (value) { if (value.isEmpty) return "Value cannot be empty"; @@ -231,8 +239,6 @@ class _StockItemDisplayState extends State { if (quantity == null) return "Value cannot be converted to a number"; if (quantity < 0) return "Value cannot be negative"; - _countStock(quantity); - return null; }, ), @@ -277,7 +283,7 @@ class _StockItemDisplayState extends State { ), trailing: IconButton( icon: FaIcon(FontAwesomeIcons.edit), - onPressed: _editStockItem, + onPressed: _editStockItemDialog, ) ) )