import 'package:flutter/material.dart'; import 'package:provider/provider.dart'; import 'package:fcs/model/storage_model.dart'; import 'package:fcs/fcs/common/helpers/theme.dart'; import 'package:fcs/util.dart'; import 'package:fcs/vo/do.dart'; import 'package:fcs/vo/storage.dart'; import 'package:fcs/widget/local_text.dart'; import 'package:fcs/widget/my_data_table.dart'; import 'package:fcs/widget/number_cell.dart'; import 'package:fcs/widget/progress.dart'; typedef OnSave = void Function(String storageID, String storageName); class DOStorageItem extends StatefulWidget { final DOLine doLine; final OnSave onSave; const DOStorageItem({Key key, this.doLine, this.onSave}) : super(key: key); @override _DOStorageItemState createState() => _DOStorageItemState(); } class _DOStorageItemState extends State { final _formKey = GlobalKey(); bool _isLoading = false; String currentStorageID; TextEditingController _product = new TextEditingController(); TextEditingController _quantity = new TextEditingController(); DOLine doLine = DOLine(); @override void initState() { super.initState(); this.doLine = widget.doLine; if (doLine.storageID != null && doLine.storageID.isNotEmpty) { this.currentStorageID = doLine.storageID; } _product.text = this.doLine.productName; _quantity.text = this.doLine.qty.toString(); } Widget showStorages(BuildContext context, StorageModel storageModel) { return Container( padding: EdgeInsets.only(top: 10), child: Row( mainAxisSize: MainAxisSize.max, children: [ Image.asset( "assets/inventory.png", width: 30, height: 30, color: primaryColor, ), SizedBox( width: 20, ), new Flexible( child: Container( width: 170.0, child: DropdownButton( value: currentStorageID, isExpanded: true, hint: Text( 'Select Storage', style: labelStyle, ), onChanged: changedStorage, items: storageModel .getStorage(doLine.productID) .map>((Storage storage) { return new DropdownMenuItem( value: storage.id, child: new Text(storage.name, style: textStyle), ); }).toList(), ), ), ), ], ), ); } void changedStorage(selected) { setState(() { currentStorageID = selected; }); } Widget showStorgeTable(BuildContext context, StorageModel storageModel) { return Container( child: SingleChildScrollView( scrollDirection: Axis.horizontal, child: MyDataTable( headingRowHeight: 40, columnSpacing: 100, columns: [ MyDataColumn( label: LocalText(context, "storge"), ), MyDataColumn( label: LocalText(context, "storage.product.qty"), ), ], rows: getProductRow(storageModel.getStorage(doLine.productID)), ), ), ); } List getProductRow(List storages) { return storages.map((s) { return MyDataRow( onSelectChanged: (bool selected) { setState(() { currentStorageID = s.id; }); }, cells: [ MyDataCell( new Text(s.name, style: textStyle), ), MyDataCell( NumberCell(s.products .firstWhere((p) => p.id == doLine.productID) .quantity), ), ], ); }).toList(); } @override Widget build(BuildContext context) { var storgeModel = Provider.of(context); final productbox = Container( padding: EdgeInsets.only(top: 10), child: Row( children: [ LocalText(context, "do.product"), SizedBox( width: 20, ), Text(_product.text, style: textStyle) ], )); final quantitybox = Container( padding: EdgeInsets.only(top: 15), child: Row( children: [ LocalText(context, "do.quantity"), SizedBox( width: 20, ), Text(formatNumber(this.doLine.qty), style: textStyle) ], ), ); return LocalProgress( inAsyncCall: _isLoading, child: Scaffold( appBar: AppBar( backgroundColor: primaryColor, title: Text("DO"), actions: [ IconButton( icon: Icon(Icons.save), onPressed: () { _save(); }, ) ], ), body: Form( key: _formKey, child: Column( children: [ Expanded( child: ListView( shrinkWrap: true, padding: EdgeInsets.only(left: 24.0, right: 24.0, top: 10), children: [ productbox, quantitybox, showStorages(context, storgeModel), showStorgeTable(context, storgeModel) ], ), ), ], ), )), ); } _save() { this.doLine.storageID = currentStorageID; var storageName = Provider.of(context).getStorageName(currentStorageID); this.doLine.storageName = storageName; if (widget.onSave != null) widget.onSave(this.doLine.storageID, storageName); Navigator.pop(context, this.doLine); } }