Files
fcs/lib/pages/my_registeration.dart
2020-08-30 21:26:37 +06:30

317 lines
9.1 KiB
Dart

import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:provider/provider.dart';
import 'package:fcs/model/language_model.dart';
import 'package:fcs/model/reg_model.dart';
import 'package:fcs/pages/util.dart';
import 'package:fcs/vo/buyer.dart';
import 'package:fcs/widget/img_file.dart';
import 'package:fcs/widget/local_text.dart';
import 'package:fcs/widget/my_data_table.dart';
import 'package:fcs/widget/progress.dart';
import '../fcs/common/theme.dart';
import '../widget/localization/app_translations.dart';
import 'my_registeration_item.dart';
enum BuyerType { shop, agent }
class MyRegisteration extends StatefulWidget {
final Buyer buyer;
const MyRegisteration({this.buyer});
@override
_MyRegisterationState createState() => _MyRegisterationState();
}
class _MyRegisterationState extends State<MyRegisteration> {
TextEditingController _bizName = new TextEditingController();
TextEditingController _bizAddress = new TextEditingController();
BuyerType buyerType;
Buyer buyer = Buyer();
final formKey = GlobalKey<FormState>();
bool _isLoading = false;
Attachments attachments = Attachments();
bool _isNew = true;
@override
void initState() {
if (widget.buyer != null) {
_isNew = false;
buyerType =
widget.buyer.bizType == "shop" ? BuyerType.shop : BuyerType.agent;
buyer = widget.buyer;
_bizName.text = buyer.bizName;
_bizAddress.text = buyer.bizAddress;
} else {
buyerType = BuyerType.shop;
}
super.initState();
}
@override
void dispose() {
super.dispose();
}
@override
Widget build(BuildContext context) {
var languageModel = Provider.of<LanguageModel>(context);
final companyName = Container(
padding: EdgeInsets.only(top: 0, left: 20, right: 15),
child: TextFormField(
controller: _bizName,
autofocus: false,
cursorColor: primaryColor,
style: textStyle,
decoration: new InputDecoration(
labelText: AppTranslations.of(context).text("reg.biz_name"),
labelStyle: languageModel.isEng ? labelStyle : labelStyleMM,
icon: Icon(
Icons.business,
color: primaryColor,
),
enabledBorder: UnderlineInputBorder(
borderSide: BorderSide(color: primaryColor, width: 1.0)),
focusedBorder: UnderlineInputBorder(
borderSide: BorderSide(color: primaryColor, width: 1.0)),
),
validator: _validateBizName,
));
final companyAddress = Container(
padding: EdgeInsets.only(top: 0, left: 20, right: 15),
child: TextFormField(
minLines: 2,
maxLines: 3,
controller: _bizAddress,
autofocus: false,
cursorColor: primaryColor,
style: textStyle,
decoration: new InputDecoration(
labelText: AppTranslations.of(context).text("reg.biz_address"),
labelStyle: languageModel.isEng ? labelStyle : labelStyleMM,
icon: Image.asset(
"assets/address.png",
height: 25,
color: primaryColor,
),
enabledBorder: UnderlineInputBorder(
borderSide: BorderSide(color: primaryColor, width: 1.0)),
focusedBorder: UnderlineInputBorder(
borderSide: BorderSide(color: primaryColor, width: 1.0)),
),
validator: _validateBizAddress,
));
final nricFrontBox = Container(
padding: EdgeInsets.only(left: 20),
child: Row(children: <Widget>[
LocalText(context, 'reg_info.nric_front'),
ImageFile(
enabled: true,
initialImgUrl:
widget.buyer == null ? "" : widget.buyer.nricFrontUrl,
title: "Image",
onFile: (file) {
attachments.nricFront = file;
})
]));
final nricBackBox = Container(
padding: EdgeInsets.only(left: 20, top: 20),
child: Row(children: <Widget>[
LocalText(context, 'reg_info.nric_back'),
ImageFile(
enabled: true,
initialImgUrl:
widget.buyer == null ? '' : widget.buyer.nricBackUrl,
title: "Image",
onFile: (file) {
attachments.nricBack = file;
}),
]));
final nric = Container(
padding: EdgeInsets.only(top: 20, left: 20, right: 15),
child: Column(
mainAxisAlignment: MainAxisAlignment.start,
children: <Widget>[nricFrontBox, nricBackBox],
));
final buyerTypeWidget = Row(children: <Widget>[
Flexible(
child: ListTile(
onTap: () => setState(() {
buyerType = BuyerType.shop;
}),
title: Text(AppTranslations.of(context).text("reg.type_shop")),
leading: Radio(
activeColor: primaryColor,
value: BuyerType.shop,
groupValue: buyerType,
onChanged: (BuyerType value) {
setState(() {
buyerType = value;
});
},
),
),
),
Flexible(
child: ListTile(
onTap: () => setState(() {
buyerType = BuyerType.agent;
}),
title: Text(AppTranslations.of(context).text("reg.type_agent")),
leading: Radio(
activeColor: primaryColor,
value: BuyerType.agent,
groupValue: buyerType,
onChanged: (BuyerType value) {
setState(() {
buyerType = value;
});
},
),
),
),
]);
return LocalProgress(
inAsyncCall: _isLoading,
child: Scaffold(
appBar: AppBar(
backgroundColor: primaryColor,
title: Text(AppTranslations.of(context).text("reg.title")),
actions: <Widget>[
IconButton(
icon: Icon(Icons.send),
onPressed: () {
showConfirmDialog(context, "reg.confirm", () {
_submit();
});
},
)
],
),
body: Container(
child: Form(
key: formKey,
child: ListView(
children: <Widget>[
companyName,
companyAddress,
buyerTypeWidget,
nric,
],
),
),
),
),
);
}
List<MyDataRow> getProductRow(Buyer buyer) {
return buyer.buyerProducts.map((b) {
return MyDataRow(
onSelectChanged: (bool selected) async {
final BuyerProduct buyerProduct = await Navigator.push<BuyerProduct>(
context,
MaterialPageRoute(
builder: (context) => MyRegisterationItem(
buyerProduct: b,
)),
);
_save(buyerProduct);
},
cells: [
MyDataCell(
new Text(
b.productName,
style: textStyle,
),
),
MyDataCell(
new Text(b.storageCapacityQty.toString(), style: textStyle),
),
MyDataCell(
new Text(b.dailySaleQty.toString(), style: textStyle),
),
],
);
}).toList();
}
String _validateBizName(value) {
if (value.isEmpty) {
return AppTranslations.of(context).text("reg.empty_biz_name");
}
return null;
}
String _validateBizAddress(value) {
if (value.isEmpty) {
return AppTranslations.of(context).text("reg.empty_biz_address");
}
return null;
}
String _validateShops(value) {
if (value.isEmpty) {
return AppTranslations.of(context).text("reg.empty_shops");
}
return null;
}
_save(BuyerProduct buyerProduct) {
if (buyerProduct == null) return;
if (buyerProduct.action == "create") {
if (buyer.buyerProducts.contains(buyerProduct)) {
showMsgDialog(context, "Error", "Duplicate line");
return;
}
buyer.buyerProducts.add(buyerProduct);
} else if (buyerProduct.action == "delete") {
buyer.buyerProducts.remove(buyerProduct);
}
}
_submit() async {
if (!formKey.currentState.validate()) {
return;
}
if (_isNew) {
if (attachments.nricFront == null || attachments.nricBack == null) {
showMsgDialog(
context, "Error", "Required NRIC front and back attachments");
return;
}
}
setState(() {
_isLoading = true;
});
try {
String type = buyerType == BuyerType.agent ? "agent" : "shop";
buyer.bizName = _bizName.text;
buyer.bizAddress = _bizAddress.text;
buyer.bizType = type;
if (_isNew) {
await Provider.of<RegModel>(context).register(buyer, attachments);
} else {
await Provider.of<RegModel>(context).update(buyer, attachments);
}
Navigator.pop(context, true);
} catch (e) {
showMsgDialog(context, "Error", e.toString());
} finally {
setState(() {
_isLoading = false;
});
}
}
}