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

228 lines
6.5 KiB
Dart
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
import 'package:flutter/material.dart';
import 'package:intl/intl.dart';
import 'package:provider/provider.dart';
import 'package:fcs/model/language_model.dart';
import 'package:fcs/model/log_model.dart';
import 'package:fcs/model/main_model.dart';
import 'package:fcs/model/pd_model.dart';
import 'package:fcs/theme/theme.dart';
import 'package:fcs/vo/pd.dart';
import 'package:fcs/widget/local_text.dart';
import 'package:fcs/widget/localization/app_translations.dart';
import 'package:fcs/widget/my_data_table.dart';
import 'package:fcs/widget/progress.dart';
import '../util.dart';
import 'pd_item.dart';
class PDForm extends StatefulWidget {
final PD pd;
const PDForm({Key key, this.pd}) : super(key: key);
@override
_PDFormState createState() => _PDFormState();
}
class _PDFormState extends State<PDForm> {
TextEditingController _date = new TextEditingController();
TextEditingController _userName = new TextEditingController();
PD pd = PD();
bool _isLoading = false;
bool isNew = true;
@override
void initState() {
super.initState();
if (widget.pd != null) {
_userName.text = widget.pd.userName;
} else {
var mainModel = Provider.of<MainModel>(context, listen: false);
_userName.text = mainModel.user.name;
}
_load();
}
_load() async {
if (widget.pd != null) {
this.pd = widget.pd;
_date.text = DateFormat('dd MMM yyyy hh:mm a').format(widget.pd.date);
isNew = false;
Provider.of<PDModel>(context, listen: false).loadPDLines(pd).then((_pd) {
setState(() {
this.pd = _pd;
});
});
}
}
@override
Widget build(BuildContext context) {
var languageModel = Provider.of<LanguageModel>(context);
final _formKey = GlobalKey<FormState>();
final dateBox = Container(
padding: EdgeInsets.only(left: 20, right: 15),
child: TextFormField(
controller: _date,
enabled: false,
cursorColor: primaryColor,
style: textStyle,
decoration: new InputDecoration(
labelText: AppTranslations.of(context).text("pd.date"),
labelStyle: languageModel.isEng ? labelStyle : labelStyleMM,
icon: Icon(
Icons.date_range,
color: primaryColor,
size: 23,
)),
));
final nameBox = Container(
padding: EdgeInsets.only(left: 20, right: 15),
child: TextFormField(
controller: _userName,
autofocus: false,
readOnly: true,
style: textStyle,
decoration: new InputDecoration(
border: InputBorder.none,
focusedBorder: InputBorder.none,
icon: Icon(
Icons.person,
color: primaryColor,
size: 25,
)),
));
return LocalProgress(
inAsyncCall: _isLoading,
child: Scaffold(
appBar: AppBar(
backgroundColor: primaryColor,
title: Text(
AppTranslations.of(context).text('pd'),
style: languageModel.isEng
? TextStyle()
: TextStyle(fontFamily: 'MyanmarUnicode'),
),
actions: <Widget>[
isNew
? IconButton(
icon: Icon(Icons.send),
onPressed: () {
if (!_formKey.currentState.validate()) return;
showConfirmDialog(context, "pd.confirm", () {
_submit();
});
},
)
: Container()
],
),
floatingActionButton: isNew
? FloatingActionButton(
backgroundColor: primaryColor,
child: Icon(Icons.add),
onPressed: () async {
final PDLine pdLine = await Navigator.push<PDLine>(
context,
MaterialPageRoute(builder: (context) => PDItem()),
);
_save(pdLine);
},
)
: null,
body: Form(
key: _formKey,
child: ListView(
children: <Widget>[
isNew ? Container() : dateBox,
nameBox,
SingleChildScrollView(
scrollDirection: Axis.horizontal,
child: MyDataTable(
columns: [
MyDataColumn(
label: LocalText(context, "pd.product")),
MyDataColumn(
label: LocalText(context, "pd.storage"),
),
MyDataColumn(
label: LocalText(context, "pd.quantity")),
],
rows: getProductRow(pd),
),
),
],
),
)),
);
}
List<MyDataRow> getProductRow(PD pd) {
return pd.pdLines.map((p) {
return MyDataRow(
onSelectChanged: (bool selected) async {
if (!isNew) return;
var pdLine = await Navigator.push(
context,
MaterialPageRoute(
builder: (context) => PDItem(
pdLine: p,
)),
);
_save(pdLine);
},
cells: [
MyDataCell(
new Text(
p.productName,
style: textStyle,
),
),
MyDataCell(
new Text(p.storageName, style: textStyle),
),
MyDataCell(
new Text(p.quantity.toString(), style: textStyle),
),
],
);
}).toList();
}
_save(PDLine pdLine) {
if (pdLine == null) return;
if (pdLine.action == "create") {
if (pd.pdLines.contains(pdLine)) {
showMsgDialog(context, "Error", "Duplicate line");
return;
}
pd.pdLines.add(pdLine);
} else if (pdLine.action == "delete") {
pd.pdLines.remove(pdLine);
}
}
_submit() async {
if (pd.pdLines.length == 0) {
showMsgDialog(context, "Error", "No product line");
return;
}
setState(() {
_isLoading = true;
});
try {
PDModel pdModel = Provider.of<PDModel>(context);
await pdModel.createPD(pd);
Navigator.pop(context);
} catch (e) {
showMsgDialog(context, "Error", e.toString());
} finally {
setState(() {
_isLoading = false;
});
}
}
}