From af104717abe86e0141ad620ca5dc409e3a1186e0 Mon Sep 17 00:00:00 2001 From: Oliver Walters Date: Sat, 18 Apr 2020 22:25:59 +1000 Subject: [PATCH] More refactoring Helper function to display a scrollable form in a dialog --- lib/widget/dialogs.dart | 23 ++++ lib/widget/stock_detail.dart | 247 ++++++++++++++++++++--------------- 2 files changed, 162 insertions(+), 108 deletions(-) diff --git a/lib/widget/dialogs.dart b/lib/widget/dialogs.dart index ceb94158..d6e0cf0e 100644 --- a/lib/widget/dialogs.dart +++ b/lib/widget/dialogs.dart @@ -38,4 +38,27 @@ void showProgressDialog(BuildContext context, String title, String description) void hideProgressDialog(BuildContext context) { Navigator.pop(context); +} + +void showFormDialog(BuildContext context, String title, {GlobalKey key, List fields, List actions}) { + showDialog( + context: context, + builder: (BuildContext context) { + return AlertDialog( + title: Text(title), + actions: actions, + content: Form( + key: key, + child: SingleChildScrollView( + child: Column( + mainAxisSize: MainAxisSize.min, + mainAxisAlignment: MainAxisAlignment.start, + crossAxisAlignment: CrossAxisAlignment.start, + children: fields + ) + ) + ) + ); + } + ); } \ No newline at end of file diff --git a/lib/widget/stock_detail.dart b/lib/widget/stock_detail.dart index 36f56a04..6574d742 100644 --- a/lib/widget/stock_detail.dart +++ b/lib/widget/stock_detail.dart @@ -2,6 +2,7 @@ import 'package:InvenTree/inventree/stock.dart'; import 'package:InvenTree/inventree/part.dart'; +import 'package:InvenTree/widget/dialogs.dart'; import 'package:InvenTree/widget/fields.dart'; import 'package:InvenTree/widget/location_display.dart'; import 'package:InvenTree/widget/part_detail.dart'; @@ -102,43 +103,30 @@ class _StockItemDisplayState extends RefreshableState { _quantityController.clear(); - showDialog(context: context, - builder: (BuildContext context) { - return AlertDialog( - title: Text("Add Stock"), - actions: [ - FlatButton( - child: Text("Add"), - onPressed: () { - if (_addStockKey.currentState.validate()) _addStock(); - }, - ) - ], - content: Form( - key: _addStockKey, - child: Column( - mainAxisAlignment: MainAxisAlignment.start, - crossAxisAlignment: CrossAxisAlignment.start, - mainAxisSize: MainAxisSize.min, - children: [ - Text("Current stock: ${item.quantity}"), - QuantityField( - label: "Add Stock", - controller: _quantityController, - ), - TextFormField( - decoration: InputDecoration( - labelText: "Notes", - ), - controller: _notesController, - ) - ], - ) + showFormDialog(context, "Add Stock", + key: _addStockKey, + actions: [ + FlatButton( + child: Text("Add"), + onPressed: () { + if (_addStockKey.currentState.validate()) _addStock(); + }, + ) + ], + fields: [ + Text("Current stock: ${item.quantity}"), + QuantityField( + label: "Add Stock", + controller: _quantityController, + ), + TextFormField( + decoration: InputDecoration( + labelText: "Notes", ), - ); - } + controller: _notesController, + ) + ], ); - // TODO - Form for adding stock } void _removeStock() async { @@ -159,42 +147,30 @@ class _StockItemDisplayState extends RefreshableState { _quantityController.clear(); - showDialog(context: context, - builder: (BuildContext context) { - return AlertDialog( - title: Text("Remove Stock"), - actions: [ - FlatButton( - child: Text("Remove"), - onPressed: () { - if (_removeStockKey.currentState.validate()) _removeStock(); - }, - ) - ], - content: Form( - key: _removeStockKey, - child: Column( - mainAxisSize: MainAxisSize.min, - mainAxisAlignment: MainAxisAlignment.start, - crossAxisAlignment: CrossAxisAlignment.start, - children: [ - Text("Current stock: ${item.quantity}"), - QuantityField( - label: "Remove stock", - controller: _quantityController, - max: item.quantity, - ), - TextFormField( - decoration: InputDecoration( - labelText: "Notes", - ), - controller: _notesController, - ), - ], - ) + showFormDialog(context, "Remove Stock", + key: _removeStockKey, + actions: [ + FlatButton( + child: Text("Remove"), + onPressed: () { + if (_removeStockKey.currentState.validate()) _removeStock(); + }, + ) + ], + fields: [ + Text("Current stock: ${item.quantity}"), + QuantityField( + label: "Remove stock", + controller: _quantityController, + max: item.quantity, ), - ); - } + TextFormField( + decoration: InputDecoration( + labelText: "Notes", + ), + controller: _notesController, + ), + ], ); } @@ -214,42 +190,30 @@ class _StockItemDisplayState extends RefreshableState { } void _countStockDialog() async { - showDialog( - context: context, - builder: (BuildContext context) { - return AlertDialog( - title: Text("Count Stock"), - actions: [ - FlatButton( - child: Text("Count"), - onPressed: () { - if (_countStockKey.currentState.validate()) _countStock(); - }, - ) - ], - content: Form( - key: _countStockKey, - child: Column( - mainAxisAlignment: MainAxisAlignment.start, - crossAxisAlignment: CrossAxisAlignment.start, - mainAxisSize: MainAxisSize.min, - children: [ - QuantityField( - label: "Count Stock", - hint: "${item.quantity}", - controller: _quantityController, - ), - TextFormField( - decoration: InputDecoration( - labelText: "Notes", - ), - controller: _notesController, - ) - ], - ) - ) - ); - } + + showFormDialog(context, "Count Stock", + key: _countStockKey, + actions: [ + FlatButton( + child: Text("Count"), + onPressed: () { + if (_countStockKey.currentState.validate()) _countStock(); + }, + ) + ], + fields: [ + QuantityField( + label: "Count Stock", + hint: "${item.quantity}", + controller: _quantityController, + ), + TextFormField( + decoration: InputDecoration( + labelText: "Notes", + ), + controller: _notesController, + ) + ] ); } @@ -258,8 +222,75 @@ class _StockItemDisplayState extends RefreshableState { // TODO } - void _transferStockDialog() { - // TODO - Form for transferring stock + void _transferStockDialog() async { + + var locations = await InvenTreeStockLocation().list(context); + final _selectedController = TextEditingController(); + + InvenTreeStockLocation selectedLocation; + + _quantityController.text = "${item.quantity}"; + + showFormDialog(context, "Transfer Stock", + key: _moveStockKey, + actions: [ + FlatButton( + child: Text("Transfer"), + onPressed: () { + if (_moveStockKey.currentState.validate()) { + // TODO - Transfer! + } + }, + ) + ], + fields: [ + QuantityField( + label: "Quantity", + controller: _quantityController, + max: item.quantity, + ), + TypeAheadFormField( + textFieldConfiguration: TextFieldConfiguration( + controller: _selectedController, + autofocus: true, + decoration: InputDecoration( + hintText: "Search for location", + border: OutlineInputBorder() + ) + ), + suggestionsCallback: (pattern) async { + var suggestions = List(); + + for (var loc in locations) { + if (loc.matchAgainstString(pattern)) { + suggestions.add(loc as InvenTreeStockLocation); + } + } + + return suggestions; + }, + validator: (value) { + if (selectedLocation == null) { + return "Select a location"; + } + + return null; + }, + onSuggestionSelected: (suggestion) { + selectedLocation = suggestion as InvenTreeStockLocation; + _selectedController.text = selectedLocation.pathstring; + }, + itemBuilder: (context, suggestion) { + var location = suggestion as InvenTreeStockLocation; + + return ListTile( + title: Text("${location.pathstring}"), + subtitle: Text("${location.description}"), + ); + } + ), + ], + ); } /*