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

224 lines
6.6 KiB
Dart

import 'package:flutter/material.dart';
import 'package:font_awesome_flutter/font_awesome_flutter.dart';
import 'package:provider/provider.dart';
import 'package:fcs/model/language_model.dart';
import 'package:fcs/model/product_model.dart';
import 'package:fcs/model/storage_model.dart';
import 'package:fcs/theme/theme.dart';
import 'package:fcs/vo/pd.dart';
import 'package:fcs/vo/product.dart';
import 'package:fcs/vo/storage.dart';
import 'package:fcs/widget/localization/app_translations.dart';
import 'package:fcs/widget/progress.dart';
class PDItem extends StatefulWidget {
final PDLine pdLine;
const PDItem({Key key, this.pdLine}) : super(key: key);
@override
_PDItemState createState() => _PDItemState();
}
class _PDItemState extends State<PDItem> {
final _formKey = GlobalKey<FormState>();
bool _isLoading = false;
String currentStorageID;
String currentProductID;
TextEditingController _quantity = new TextEditingController();
PDLine pdLine = PDLine();
@override
void initState() {
super.initState();
if (widget.pdLine != null) {
this._quantity.text = widget.pdLine.quantity.toString();
this.currentProductID = widget.pdLine.productID;
this.currentStorageID = widget.pdLine.storageID;
this.pdLine = widget.pdLine;
this.pdLine.action = "update";
} else {
this.pdLine.action = "create";
}
}
Widget showInventoryList(BuildContext context, StorageModel storageModel) {
return Row(
mainAxisSize: MainAxisSize.max,
children: <Widget>[
Image.asset(
"assets/inventory.png",
color: primaryColor,
width: 25,
),
SizedBox(
width: 20,
),
new Flexible(
child: Container(
width: 170.0,
child: DropdownButton<String>(
value: currentStorageID,
isExpanded: true,
hint: Text(
'Select Storage',
style: labelStyle,
),
onChanged: changedDropDownItem,
items: storageModel.storages
.map<DropdownMenuItem<String>>((Storage storage) {
return new DropdownMenuItem<String>(
value: storage.id,
child: new Text(storage.name, style: textStyle),
);
}).toList(),
),
),
),
],
);
}
void changedDropDownItem(selected) {
setState(() {
currentStorageID = selected;
});
}
Widget showProducts(BuildContext context, ProductModel productModel) {
return Row(
mainAxisSize: MainAxisSize.max,
children: <Widget>[
Icon(
FontAwesomeIcons.tag,
color: primaryColor,
size: 20,
),
SizedBox(
width: 20,
),
new Flexible(
child: Container(
width: 170.0,
child: DropdownButton<String>(
value: currentProductID,
isExpanded: true,
hint: Text(
'Select Product',
style: labelStyle,
),
onChanged: changedProduct,
items: productModel.products
.map<DropdownMenuItem<String>>((Product product) {
return new DropdownMenuItem<String>(
value: product.id,
child: new Text(product.name, style: textStyle),
);
}).toList(),
),
),
),
],
);
}
void changedProduct(selected) {
setState(() {
currentProductID = selected;
});
}
@override
Widget build(BuildContext context) {
var storageModel = Provider.of<StorageModel>(context);
var productModel = Provider.of<ProductModel>(context);
var languageModel = Provider.of<LanguageModel>(context);
final quantityBox = Container(
padding: EdgeInsets.only(top: 10),
child: TextFormField(
controller: _quantity,
keyboardType: TextInputType.number,
autofocus: false,
cursorColor: primaryColor,
decoration: new InputDecoration(
labelText: AppTranslations.of(context).text("pd.quantity"),
labelStyle: languageModel.isEng ? labelStyle : labelStyleMM,
icon: Icon(
FontAwesomeIcons.sortNumericUpAlt,
color: primaryColor,
),
enabledBorder: UnderlineInputBorder(
borderSide: BorderSide(color: primaryColor, width: 1.0)),
focusedBorder: UnderlineInputBorder(
borderSide: BorderSide(color: primaryColor, width: 1.0)),
),
validator: (value) {
if (value.isEmpty) {
return AppTranslations.of(context).text("pd.form.quan");
}
return null;
},
),
);
return LocalProgress(
inAsyncCall: _isLoading,
child: Scaffold(
appBar: AppBar(
backgroundColor: primaryColor,
title: Text(
AppTranslations.of(context).text('pd.product.title'),
style: languageModel.isEng
? TextStyle()
: TextStyle(fontFamily: 'MyanmarUnicode'),
),
actions: <Widget>[
IconButton(
icon: Icon(Icons.delete),
onPressed: () {
_delete();
},
),
IconButton(
icon: Icon(Icons.save),
onPressed: () {
if (!_formKey.currentState.validate()) return;
_save();
},
)
],
),
body: Form(
key: _formKey,
child: ListView(
shrinkWrap: true,
padding: EdgeInsets.only(left: 24.0, right: 20.0),
children: <Widget>[
quantityBox,
showInventoryList(context, storageModel),
showProducts(context, productModel)
],
),
)),
);
}
_save() {
if (currentProductID == null || currentStorageID == null) return;
this.pdLine.storageID = currentStorageID;
var storageName =
Provider.of<StorageModel>(context).getStorageName(currentStorageID);
this.pdLine.storageName = storageName;
this.pdLine.productID = currentProductID;
var productName =
Provider.of<ProductModel>(context).getProductName(currentProductID);
this.pdLine.productName = productName;
this.pdLine.quantity = int.parse(_quantity.text);
Navigator.pop<PDLine>(context, this.pdLine);
}
_delete() {
this.pdLine.action = "delete";
Navigator.pop<PDLine>(context, this.pdLine);
}
}