Files
fcs/lib/pages/do/do_storage_item.dart
2020-05-29 07:45:27 +06:30

210 lines
5.9 KiB
Dart

import 'package:flutter/material.dart';
import 'package:provider/provider.dart';
import 'package:fcs/model/storage_model.dart';
import 'package:fcs/theme/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<DOStorageItem> {
final _formKey = GlobalKey<FormState>();
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: <Widget>[
Image.asset(
"assets/inventory.png",
width: 30,
height: 30,
color: primaryColor,
),
SizedBox(
width: 20,
),
new Flexible(
child: Container(
width: 170.0,
child: DropdownButton<String>(
value: currentStorageID,
isExpanded: true,
hint: Text(
'Select Storage',
style: labelStyle,
),
onChanged: changedStorage,
items: storageModel
.getStorage(doLine.productID)
.map<DropdownMenuItem<String>>((Storage storage) {
return new DropdownMenuItem<String>(
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<MyDataRow> getProductRow(List<Storage> 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<StorageModel>(context);
final productbox = Container(
padding: EdgeInsets.only(top: 10),
child: Row(
children: <Widget>[
LocalText(context, "do.product"),
SizedBox(
width: 20,
),
Text(_product.text, style: textStyle)
],
));
final quantitybox = Container(
padding: EdgeInsets.only(top: 15),
child: Row(
children: <Widget>[
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: <Widget>[
IconButton(
icon: Icon(Icons.save),
onPressed: () {
_save();
},
)
],
),
body: Form(
key: _formKey,
child: Column(
children: <Widget>[
Expanded(
child: ListView(
shrinkWrap: true,
padding: EdgeInsets.only(left: 24.0, right: 24.0, top: 10),
children: <Widget>[
productbox,
quantitybox,
showStorages(context, storgeModel),
showStorgeTable(context, storgeModel)
],
),
),
],
),
)),
);
}
_save() {
this.doLine.storageID = currentStorageID;
var storageName =
Provider.of<StorageModel>(context).getStorageName(currentStorageID);
this.doLine.storageName = storageName;
if (widget.onSave != null)
widget.onSave(this.doLine.storageID, storageName);
Navigator.pop<DOLine>(context, this.doLine);
}
}