From b8707b8f878e16eb1f2e9b3624e7ccb1148fbbcd Mon Sep 17 00:00:00 2001 From: Thinzar Win Date: Fri, 11 Dec 2020 17:34:56 +0630 Subject: [PATCH] update carton info --- assets/local/localization_en.json | 1 + assets/local/localization_mu.json | 1 + lib/domain/entities/carton.dart | 8 +- lib/pages/carton/cargo_table.dart | 23 +- lib/pages/carton/cargo_type_addtion.dart | 124 +--------- lib/pages/carton/carton_cargo_table.dart | 73 ++---- lib/pages/carton/carton_editor.dart | 261 ++++++++++++-------- lib/pages/carton/carton_info.dart | 74 +++++- lib/pages/carton/package_carton_editor.dart | 137 +++++----- 9 files changed, 352 insertions(+), 350 deletions(-) diff --git a/assets/local/localization_en.json b/assets/local/localization_en.json index 3845365..24151e4 100644 --- a/assets/local/localization_en.json +++ b/assets/local/localization_en.json @@ -299,6 +299,7 @@ "box.mix_caton_title":"Mix cartons", "box.min_caton.form.title":"Mix Carton", "box.mix_carton_btn":"Create mix carton", + "box.mix_type":"Mix Box Types", "Boxes End ================================================================":"", "Delivery Start ================================================================":"", diff --git a/assets/local/localization_mu.json b/assets/local/localization_mu.json index e8f5154..7d025fd 100644 --- a/assets/local/localization_mu.json +++ b/assets/local/localization_mu.json @@ -299,6 +299,7 @@ "box.mix_caton_title":"Mix cartons", "box.min_caton.form.title":"Mix Carton", "box.mix_carton_btn":"Create mix carton", + "box.mix_type":"Mix Box Types", "Boxes End ================================================================":"", "Delivery Start ================================================================":"", diff --git a/lib/domain/entities/carton.dart b/lib/domain/entities/carton.dart index cccac21..5654a1c 100644 --- a/lib/domain/entities/carton.dart +++ b/lib/domain/entities/carton.dart @@ -36,6 +36,9 @@ class Carton { String fcsShipmentNumber; String mixCartonID; String mixCartonNumber; + String cartonSizeID; + String cartonSizeName; + String mixBoxType; int rate; int weight; @@ -157,7 +160,10 @@ class Carton { this.mixCartonID, this.mixCartonNumber, this.isShipmentCarton = false, - this.deliveryAddress}); + this.deliveryAddress, + this.cartonSizeID, + this.cartonSizeName, + this.mixBoxType}); Map toMap() { List _cargoTypes = cargoTypes.map((c) => c.toMap()).toList(); diff --git a/lib/pages/carton/cargo_table.dart b/lib/pages/carton/cargo_table.dart index fc684a0..1abdf9c 100644 --- a/lib/pages/carton/cargo_table.dart +++ b/lib/pages/carton/cargo_table.dart @@ -24,7 +24,7 @@ class _CargoTableState extends State { scrollDirection: Axis.horizontal, child: MyDataTable( headingRowHeight: 40, - columnSpacing: 140, + columnSpacing: 50, columns: [ MyDataColumn( label: LocalText( @@ -33,6 +33,13 @@ class _CargoTableState extends State { color: Colors.grey, ), ), + MyDataColumn( + label: LocalText( + context, + "cargo.qty", + color: Colors.grey, + ), + ), MyDataColumn( label: LocalText( context, @@ -60,6 +67,19 @@ class _CargoTableState extends State { c.name == null ? "" : c.name, style: textStyle, )), + MyDataCell(c.qty == null || c.qty == 0 + ? Center( + child: Text( + "-", + style: textStyle, + ), + ) + : Center( + child: Text( + c.qty.toString(), + style: textStyle, + ), + )), MyDataCell( Text(c.weight == null ? "0" : c.weight.toStringAsFixed(2), style: textStyle), @@ -80,6 +100,7 @@ class _CargoTableState extends State { fontWeight: FontWeight.bold, ), )), + MyDataCell(Text("")), MyDataCell( Padding( padding: const EdgeInsets.only(right: 48.0), diff --git a/lib/pages/carton/cargo_type_addtion.dart b/lib/pages/carton/cargo_type_addtion.dart index 7c0d966..043c922 100644 --- a/lib/pages/carton/cargo_type_addtion.dart +++ b/lib/pages/carton/cargo_type_addtion.dart @@ -10,7 +10,6 @@ import 'package:fcs/pages/widgets/progress.dart'; import 'package:flutter/cupertino.dart'; import 'package:flutter/material.dart'; import 'package:provider/provider.dart'; -import 'input_text_border.dart'; class CargoTypeAddition extends StatefulWidget { @override @@ -43,38 +42,6 @@ class _CargoTypeAdditionState extends State { @override Widget build(BuildContext context) { - final cargoTableTitleBox = Container( - padding: EdgeInsets.only(left: 0.0, right: 10.0, top: 20), - child: Row( - children: [ - SizedBox( - width: 50, - ), - Expanded( - child: LocalText( - context, - "cargo.type", - color: Colors.grey, - ), - ), - SizedBox( - width: 40, - ), - LocalText( - context, - "cargo.qty", - color: Colors.grey, - ), - Spacer(), - LocalText( - context, - "cargo.weight", - color: Colors.grey, - ), - ], - ), - ); - List getCargoRowList() { return cargos.map((c) { return Container( @@ -115,89 +82,6 @@ class _CargoTypeAdditionState extends State { }).toList(); } - List _getCargoRowList() { - return cargos.map((c) { - return Container( - child: Container( - padding: - EdgeInsets.only(left: 0.0, right: 5.0, top: 3.0, bottom: 3.0), - decoration: BoxDecoration( - border: Border( - bottom: BorderSide(color: Colors.grey[350], width: 1), - ), - ), - child: Row( - children: [ - Checkbox( - value: c.isChecked, - activeColor: primaryColor, - onChanged: (bool check) { - setState(() { - c.isChecked = check; - }); - }), - Expanded(child: new Text(c.name, style: textStyle)), - SizedBox( - width: c.isCutomDuty ? 50 : 40, - child: c.isCutomDuty - ? InkWell( - onTap: () { - setState(() { - cargos.removeWhere((t) => t.name == c.name); - }); - }, - child: Icon( - Icons.remove_circle, - color: Colors.black45, - ), - ) - : Container(), - ), - Container( - height: 30, - width: 50, - child: c.isCutomDuty - ? InputTextBorder( - onAdd: (value) { - setState(() { - if (value == "" || value == null) { - c.isChecked = false; - } else { - c.qty = int.parse(value); - c.isChecked = true; - } - }); - }, - ) - : Center(child: Text("-")), - ), - Spacer(), - Container( - height: 30, - width: 60, - child: Center( - child: Text( - c.weight == null ? "" : c.weight.toStringAsFixed(2))), - // child: InputTextBorder( - // onAdd: (value) { - // setState(() { - // if (value == "" || value == null) { - // c.isChecked = false; - // } else { - // c.weight = double.parse(value); - // c.isChecked = true; - // } - // }); - // }, - // ), - ), - ], - ), - ), - ); - }).toList(); - } - final saveBtn = fcsButton( context, getLocalString(context, 'box.cargo.save.btn'), @@ -246,10 +130,6 @@ class _CargoTypeAdditionState extends State { _addCustom(customDuty); }), ), - // cargoTableTitleBox, - // Divider( - // color: Colors.grey[400], - // ), Column( children: getCargoRowList(), ), @@ -269,8 +149,8 @@ class _CargoTypeAdditionState extends State { if (cargos.any((c) => c.name == customDuty.productType)) return; setState(() { - cargos.add( - CargoType(name: customDuty.productType, isCutomDuty: true, qty: 1)); + cargos.add(CargoType( + name: customDuty.productType, isCutomDuty: true, qty: 1, weight: 0)); }); } } diff --git a/lib/pages/carton/carton_cargo_table.dart b/lib/pages/carton/carton_cargo_table.dart index 65c7e47..ef0f495 100644 --- a/lib/pages/carton/carton_cargo_table.dart +++ b/lib/pages/carton/carton_cargo_table.dart @@ -14,10 +14,12 @@ typedef OnRemove(CargoType cargoType); class CargoTable extends StatefulWidget { final List cargoTypes; + final bool isNew; final OnAdd onAdd; final OnRemove onRemove; - const CargoTable({Key key, this.cargoTypes, this.onAdd, this.onRemove}) + const CargoTable( + {Key key, this.cargoTypes, this.isNew, this.onAdd, this.onRemove}) : super(key: key); @override @@ -31,6 +33,15 @@ class _CargoTableState extends State { List _list = []; List _types = []; + @override + void initState() { + if (!widget.isNew) { + totalWeight = widget.cargoTypes + .fold(0, (previous, current) => previous + current.weight); + } + super.initState(); + } + @override Widget build(BuildContext context) { remainingWeight = @@ -79,61 +90,17 @@ class _CargoTableState extends State { var rows = widget.cargoTypes.map((c) { return MyDataRow( - onSelectChanged: (bool selected) async { - // if (this.totalWeight <= 0) { - // showMsgDialog(context, "Error", "Please insert total weight"); - // return; - // } - - // if (c.isCutomDuty) return; - // CargoType cargo = await Navigator.push( - // context, - // CupertinoPageRoute( - // builder: (context) => CargoTypeEditor( - // cargo: c, - // ))); - // if (widget.onAdd != null) widget.onAdd(cargo); - // if (cargo == null) return; - - // this._cargos.add(cargo); - // if (this.remainingWeight <= 0) return; - // this.remainingWeight -= cargo.weight; - - // this._cargos.forEach((c) { - // _list.add(c.name); - // }); - // widget.cargoTypes.forEach((c) { - // _types.add(c.name); - // }); - - // if (this._cargos.length == widget.cargoTypes.length - 1) { - // _types.forEach((t) { - // if (!_list.contains(t)) { - // widget.cargoTypes.forEach((c) { - // if (c.name == t) { - // c.weight = this.remainingWeight; - // } - // }); - // } - // }); - // } - }, + onSelectChanged: (bool selected) async {}, cells: [ - MyDataCell(Row( - children: [ - new Text( - c.name == null ? "" : c.name, - style: textStyle, - ), - new Text( - c.qty == null ? "" : " x ${c.qty.toString()}", - style: TextStyle(color: Colors.grey), - ), - ], - )), + MyDataCell( + new Text( + c.name == null ? "" : c.name, + style: textStyle, + ), + ), MyDataCell( c.isCutomDuty - ? InkWell( + ? GestureDetector( onTap: () async { String _t = await showDialog( context: context, diff --git a/lib/pages/carton/carton_editor.dart b/lib/pages/carton/carton_editor.dart index 464f66b..7d07175 100644 --- a/lib/pages/carton/carton_editor.dart +++ b/lib/pages/carton/carton_editor.dart @@ -90,9 +90,9 @@ class _CartonEditorState extends State { cargoTypes: [], packages: [], ); - _lengthController.text = ""; - _widthController.text = ""; - _heightController.text = ""; + _lengthController.text = "0"; + _widthController.text = "0"; + _heightController.text = "0"; _isNew = true; _selectedCartonType = carton_from_packages; _selectedMixType = mix_delivery; @@ -256,28 +256,6 @@ class _CartonEditorState extends State { }); }); - final _cartonTitleBox = Container( - child: Row( - mainAxisAlignment: MainAxisAlignment.end, - children: [ - Text("Cartons (${_cartons.length})"), - IconButton( - icon: Icon( - Icons.add_circle, - color: primaryColor, - ), - onPressed: () async { - Carton _carton = await Navigator.push( - context, - CupertinoPageRoute( - builder: (context) => PackageCartonEditor()), - ); - _addCarton(_carton); - }), - ], - ), - ); - final cartonTitleBox = Container( child: LocalTitle( textKey: "boxes.title", @@ -287,26 +265,92 @@ class _CartonEditorState extends State { color: primaryColor, ), onPressed: () async { - Carton _carton = await Navigator.push( - context, - CupertinoPageRoute(builder: (context) => PackageCartonEditor()), - ); - _addCarton(_carton); + bool isFromPackages = _selectedCartonType == carton_from_packages; + if (_user == null && isFromPackages) { + showMsgDialog(context, "Error", "Please select customer"); + return; + } + if (_fcsShipment == null && _isNew) { + showMsgDialog(context, "Error", "Please select FCS shipment"); + return; + } + + double l = double.parse(_lengthController.text, (s) => 0); + double w = double.parse(_widthController.text, (s) => 0); + double h = double.parse(_heightController.text, (s) => 0); + + Carton carton = Carton(); + carton.id = _carton.id; + carton.cartonType = _selectedCartonType; + carton.fcsShipmentID = + _isNew ? _fcsShipment.id : _carton.fcsShipmentID; + carton.userID = _user?.id; + carton.packages = + _carton.packages.where((e) => e.isChecked).toList(); + + carton.cargoTypes = _carton.cargoTypes; + carton.length = l; + carton.width = w; + carton.height = h; + + carton.deliveryAddress = _carton.deliveryAddress; + setState(() { + _isLoading = true; + }); + try { + Navigator.push( + context, + CupertinoPageRoute( + builder: (context) => + PackageCartonEditor(carton: carton, isNew: _isNew)), + ); + } catch (e) { + showMsgDialog(context, "Error", e.toString()); + } finally { + setState(() { + _isLoading = false; + }); + } }), ), ); final mixTypeBox = Container( - padding: EdgeInsets.only(top: 10), - child: LocalRadioButtons( - readOnly: !_isNew, - values: boxModel.mixTypes, - selectedValue: _selectedMixType, - callback: (v) { - setState(() { - _selectedMixType = v; - }); - })); + padding: EdgeInsets.only(top: 20), + child: Column( + crossAxisAlignment: CrossAxisAlignment.start, + children: [ + Container( + padding: EdgeInsets.only(left: 5), + child: LocalText( + context, + "box.mix_type", + color: primaryColor, + fontSize: 15, + fontWeight: FontWeight.bold, + )), + Row( + children: boxModel.mixTypes.map((e) { + return Row( + children: [ + Radio( + value: e, + groupValue: _selectedMixType, + activeColor: primaryColor, + onChanged: (v) { + setState(() { + _selectedMixType = v; + }); + }, + ), + Text(e), + ], + ); + }).toList(), + ), + ], + ), + ); final mixcartonTitleBox = Container( child: LocalTitle( @@ -316,8 +360,8 @@ class _CartonEditorState extends State { Icons.add_circle, color: primaryColor, ), - onPressed: () async { - Carton _carton = await Navigator.push( + onPressed: () { + Navigator.push( context, CupertinoPageRoute(builder: (context) => MixCartonEditor()), ); @@ -401,7 +445,7 @@ class _CartonEditorState extends State { SizedBox( height: 20, ), - _isNew ? createBtn : saveBtn, + createBtn, SizedBox( height: 20, ), @@ -522,78 +566,77 @@ class _CartonEditorState extends State { } _save() async { - bool isFromShipment = _selectedCartonType == carton_from_shipments; - bool isSmallBag = _selectedCartonType == carton_small_bag; - if (_user == null && (isFromShipment || isSmallBag)) { - showMsgDialog(context, "Error", "Please select customer"); - return; - } - if (_fcsShipment == null && _isNew) { - showMsgDialog(context, "Error", "Please select FCS shipment"); - return; - } - if ((_carton.cargoTypes?.length ?? 0) == 0 && - (isFromShipment || isSmallBag)) { - showMsgDialog(context, "Error", "Expect at least one cargo type"); - return; - } - double l = double.parse(_lengthController.text, (s) => 0); - double w = double.parse(_widthController.text, (s) => 0); - double h = double.parse(_heightController.text, (s) => 0); - if ((l <= 0 || w <= 0 || h <= 0) && isFromShipment) { - showMsgDialog(context, "Error", "Invalid dimension"); - return; - } - if (_deliveryAddress == null && (isFromShipment || isSmallBag)) { - showMsgDialog(context, "Error", "Invalid delivery address"); - return; - } - if (isSmallBag && _mixCarton == null && _isNew) { - showMsgDialog(context, "Error", "Invalid mix carton"); - return; - } + // bool isFromShipment = _selectedCartonType == carton_from_shipments; + // bool isSmallBag = _selectedCartonType == carton_small_bag; + // if (_user == null && (isFromShipment || isSmallBag)) { + // showMsgDialog(context, "Error", "Please select customer"); + // return; + // } + // if (_fcsShipment == null && _isNew) { + // showMsgDialog(context, "Error", "Please select FCS shipment"); + // return; + // } + // if ((_carton.cargoTypes?.length ?? 0) == 0 && + // (isFromShipment || isSmallBag)) { + // showMsgDialog(context, "Error", "Expect at least one cargo type"); + // return; + // } + // double l = double.parse(_lengthController.text, (s) => 0); + // double w = double.parse(_widthController.text, (s) => 0); + // double h = double.parse(_heightController.text, (s) => 0); + // if ((l <= 0 || w <= 0 || h <= 0) && isFromShipment) { + // showMsgDialog(context, "Error", "Invalid dimension"); + // return; + // } + // if (_deliveryAddress == null && (isFromShipment || isSmallBag)) { + // showMsgDialog(context, "Error", "Invalid delivery address"); + // return; + // } + // if (isSmallBag && _mixCarton == null && _isNew) { + // showMsgDialog(context, "Error", "Invalid mix carton"); + // return; + // } - Carton carton = Carton(); - carton.id = _carton.id; - carton.cartonType = _selectedCartonType; - carton.fcsShipmentID = _isNew ? _fcsShipment.id : _carton.fcsShipmentID; - carton.userID = _user?.id; - carton.cargoTypes = _carton.cargoTypes; - carton.packages = _carton.packages.where((e) => e.isChecked).toList(); - carton.mixCartonID = _mixCarton?.id; - carton.length = l; - carton.width = w; - carton.height = h; - carton.deliveryAddress = _deliveryAddress; - setState(() { - _isLoading = true; - }); - try { - CartonModel cartonModel = - Provider.of(context, listen: false); - if (_isNew) { - await cartonModel.createCarton(carton); - } else { - await cartonModel.updateCarton(carton); - } - Navigator.pop(context, true); - } catch (e) { - showMsgDialog(context, "Error", e.toString()); - } finally { - setState(() { - _isLoading = false; - }); - } + // Carton carton = Carton(); + // carton.id = _carton.id; + // carton.cartonType = _selectedCartonType; + // carton.fcsShipmentID = _isNew ? _fcsShipment.id : _carton.fcsShipmentID; + // carton.userID = _user?.id; + // carton.cargoTypes = _carton.cargoTypes; + // carton.packages = _carton.packages.where((e) => e.isChecked).toList(); + // carton.mixCartonID = _mixCarton?.id; + // carton.length = l; + // carton.width = w; + // carton.height = h; + // carton.deliveryAddress = _deliveryAddress; + // setState(() { + // _isLoading = true; + // }); + // try { + // CartonModel cartonModel = + // Provider.of(context, listen: false); + // if (_isNew) { + // await cartonModel.createCarton(carton); + // } else { + // await cartonModel.updateCarton(carton); + // } + // Navigator.pop(context, true); + // } catch (e) { + // showMsgDialog(context, "Error", e.toString()); + // } finally { + // setState(() { + // _isLoading = false; + // }); + // } + Navigator.pop(context, true); } isDataChanged() { if (_isNew) { - return _fcsShipment != null || - _user != null || - _cartons.isNotEmpty || - this._mixCartons.isNotEmpty; + return false; + // return _fcsShipment != null || _user != null; } else { - return true; + return false; } } } diff --git a/lib/pages/carton/carton_info.dart b/lib/pages/carton/carton_info.dart index 900c426..3ee835b 100644 --- a/lib/pages/carton/carton_info.dart +++ b/lib/pages/carton/carton_info.dart @@ -1,4 +1,5 @@ import 'package:fcs/domain/constants.dart'; +import 'package:fcs/domain/entities/cargo_type.dart'; import 'package:fcs/domain/entities/carton.dart'; import 'package:fcs/domain/entities/package.dart'; import 'package:fcs/domain/vo/delivery_address.dart'; @@ -23,6 +24,7 @@ import 'package:provider/provider.dart'; import 'cargo_table.dart'; import 'carton_editor.dart'; import 'carton_package_table.dart'; +import 'carton_row.dart'; import 'model/carton_model.dart'; import 'widgets.dart'; @@ -43,8 +45,11 @@ class _CartonInfoState extends State { TextEditingController _widthController = new TextEditingController(); TextEditingController _heightController = new TextEditingController(); TextEditingController _lengthController = new TextEditingController(); + TextEditingController _cartonSizeController = new TextEditingController(); double volumetricRatio = 0; double shipmentWeight = 0; + String selectMixBoxType; + List _cartons = []; bool isMixBox; bool isFromShipments; @@ -73,6 +78,7 @@ class _CartonInfoState extends State { _widthController.text = _box.width.toString(); _heightController.text = _box.height.toString(); _lengthController.text = _box.length.toString(); + _cartonSizeController.text = _box.cartonSizeName ?? ""; _deliveryAddress = _box.deliveryAddress; isMixBox = _box.cartonType == carton_mix_box; isFromShipments = _box.cartonType == carton_from_shipments; @@ -81,6 +87,17 @@ class _CartonInfoState extends State { isEdiable = !isMixBox && (isFromPackages || isSmallBag) && _box.status == carton_packed_status; + selectMixBoxType = _box.mixBoxType ?? "Mix Delivery"; + _cartons = [ + Carton( + cartonNumber: "A100B-1#1", + cargoTypes: [CargoType(name: "General", weight: 12)], + userName: "Seven 7"), + Carton( + cartonNumber: "A100B-1#2", + cargoTypes: [CargoType(name: "General", weight: 12)], + userName: "Seven 7"), + ]; } _loadPackages() async { @@ -176,10 +193,10 @@ class _CartonInfoState extends State { ], ); - final shipmentWeightBox = DisplayText( - text: shipmentWeight != null ? shipmentWeight.toStringAsFixed(2) : "", - labelTextKey: "box.shipment_weight", - iconData: MaterialCommunityIcons.weight, + final cartonSizeBox = DisplayText( + text: _cartonSizeController.text, + labelTextKey: "box.carton_size", + iconData: AntDesign.CodeSandbox, ); final cargoTableBox = CargoTable( cargoTypes: _box.cargoTypes, @@ -190,6 +207,36 @@ class _CartonInfoState extends State { iconData: MaterialCommunityIcons.package, ); + final mixTypeBox = Container( + padding: EdgeInsets.only(top: 20), + child: Column( + crossAxisAlignment: CrossAxisAlignment.start, + children: [ + Container( + padding: EdgeInsets.only(left: 5), + child: LocalText( + context, + "box.mix_type", + color: primaryColor, + fontSize: 15, + fontWeight: FontWeight.bold, + )), + Row( + children: [ + Padding( + padding: const EdgeInsets.all(8.0), + child: Icon( + Icons.check, + color: primaryColor, + ), + ), + Text(selectMixBoxType) + ], + ) + ], + ), + ); + return LocalProgress( inAsyncCall: _isLoading, child: Scaffold( @@ -222,7 +269,7 @@ class _CartonInfoState extends State { ), body: Padding( padding: const EdgeInsets.all(10.0), - child: ListView(children: [ + child: ListView(shrinkWrap: true, children: [ Center(child: getCartonNumberStatus(context, _box)), LocalTitle(textKey: "box.type.title"), cartonTypeBox, @@ -231,6 +278,15 @@ class _CartonInfoState extends State { isSmallBag ? mixCartonNumberBox : Container(), isMixBox ? Container() : fcsIDBox, isMixBox ? Container() : customerNameBox, + isMixBox ? mixTypeBox : Container(), + isMixBox ? LocalTitle(textKey: "boxes.title") : Container(), + isMixBox + ? Column( + children: _getCartons( + context, + this._cartons, + )) + : Container(), isFromPackages || isSmallBag ? CartonPackageTable( packages: _box.packages, @@ -241,8 +297,8 @@ class _CartonInfoState extends State { ...(isFromPackages ? [ LocalTitle(textKey: "box.dimension"), + cartonSizeBox, dimBox, - shipmentWeightBox, ] : []), isMixBox @@ -263,6 +319,12 @@ class _CartonInfoState extends State { ); } + List _getCartons(BuildContext context, List cartons) { + return cartons.map((c) { + return CartonRow(box: c); + }).toList(); + } + _gotoEditor() async { bool updated = await Navigator.push( context, diff --git a/lib/pages/carton/package_carton_editor.dart b/lib/pages/carton/package_carton_editor.dart index ddf92a3..3e2726a 100644 --- a/lib/pages/carton/package_carton_editor.dart +++ b/lib/pages/carton/package_carton_editor.dart @@ -7,6 +7,7 @@ import 'package:fcs/helpers/theme.dart'; import 'package:fcs/pages/carton_size/carton_size_list.dart'; import 'package:fcs/pages/carton_size/model/carton_size_model.dart'; import 'package:fcs/pages/delivery_address/model/delivery_address_model.dart'; +import 'package:fcs/pages/main/util.dart'; import 'package:fcs/pages/rates/model/shipment_rate_model.dart'; import 'package:fcs/pages/widgets/defalut_delivery_address.dart'; import 'package:fcs/pages/widgets/delivery_address_selection.dart'; @@ -23,10 +24,12 @@ import 'package:provider/provider.dart'; import 'cargo_type_addtion.dart'; import 'carton_cargo_table.dart'; +import 'model/carton_model.dart'; class PackageCartonEditor extends StatefulWidget { - final Carton box; - PackageCartonEditor({this.box}); + final Carton carton; + final bool isNew; + PackageCartonEditor({this.carton, this.isNew}); @override _PackageCartonEditorState createState() => _PackageCartonEditorState(); @@ -37,51 +40,28 @@ class _PackageCartonEditorState extends State { TextEditingController _widthCtl = new TextEditingController(); TextEditingController _heightCtl = new TextEditingController(); - Carton _box; + Carton _carton; bool _isLoading = false; - bool _isNew; - double volumetricRatio = 0; - double shipmentWeight = 0; + DeliveryAddress _deliveryAddress = new DeliveryAddress(); + + List _cargoTypes = []; @override void initState() { super.initState(); - - volumetricRatio = Provider.of(context, listen: false) - .rate - .volumetricRatio; - - if (widget.box != null) { - _box = widget.box; - _isNew = false; - _lengthCtl.text = _box.length.toString(); - _widthCtl.text = _box.width.toString(); - _heightCtl.text = _box.height.toString(); + if (widget.isNew) { + _carton = widget.carton; + _lengthCtl.text = "0"; + _widthCtl.text = "0"; + _heightCtl.text = "0"; } else { - var shipmentModel = - Provider.of(context, listen: false); - - _isNew = true; - _box = Carton(cargoTypes: []); - _box.deliveryAddress = shipmentModel.defalutAddress; - - _lengthCtl.text = "12"; - _widthCtl.text = "12"; - _heightCtl.text = "12"; + _carton = widget.carton; + _cargoTypes = List.from(widget.carton.cargoTypes); + _lengthCtl.text = _carton.length.toString(); + _widthCtl.text = _carton.width.toString(); + _heightCtl.text = _carton.height.toString(); + _deliveryAddress = _carton.deliveryAddress; } - _lengthCtl.addListener(_calShipmentWeight); - _widthCtl.addListener(_calShipmentWeight); - _heightCtl.addListener(_calShipmentWeight); - _calShipmentWeight(); - } - - _calShipmentWeight() { - double l = double.parse(_lengthCtl.text, (s) => 0); - double w = double.parse(_widthCtl.text, (s) => 0); - double h = double.parse(_heightCtl.text, (s) => 0); - setState(() { - shipmentWeight = (l * w * h / volumetricRatio).ceilToDouble(); - }); } @override @@ -131,14 +111,15 @@ class _PackageCartonEditorState extends State { CupertinoPageRoute(builder: (context) => CargoTypeAddition())); if (cargos == null) return; setState(() { - _box.cargoTypes.clear(); - _box.cargoTypes.addAll(cargos); + _cargoTypes.clear(); + _cargoTypes.addAll(cargos); }); }), ); final cargoTableBox = CargoTable( - cargoTypes: _box.cargoTypes, + isNew: widget.isNew, + cargoTypes: _cargoTypes, onAdd: (c) => _addCargo(c), onRemove: (c) => _removeCargo(c), ); @@ -159,7 +140,7 @@ class _PackageCartonEditorState extends State { backgroundColor: Colors.white, title: LocalText( context, - _isNew ? "boxes.create.title" : "box.edit.title", + widget.isNew ? "boxes.create.title" : "box.edit.title", fontSize: 20, color: primaryColor, ), @@ -175,19 +156,19 @@ class _PackageCartonEditorState extends State { dimBox, LocalTitle(textKey: "box.delivery_address"), DefaultDeliveryAddress( - deliveryAddress: _box.deliveryAddress, + deliveryAddress: _deliveryAddress, labelKey: "box.delivery_address", onTap: () async { DeliveryAddress d = await Navigator.push( context, CupertinoPageRoute( builder: (context) => DeliveryAddressSelection( - deliveryAddress: _box.deliveryAddress, + deliveryAddress: _deliveryAddress, )), ); if (d == null) return; setState(() { - _box.deliveryAddress = d; + _deliveryAddress = d; }); }), SizedBox( @@ -280,24 +261,64 @@ class _PackageCartonEditorState extends State { _addCargo(CargoType cargo) { if (cargo == null) return; setState(() { - _box.cargoTypes.remove(cargo); - _box.cargoTypes.add(cargo); + _cargoTypes.remove(cargo); + _cargoTypes.add(cargo); }); } _removeCargo(CargoType cargo) { setState(() { - _box.cargoTypes.remove(cargo); + _cargoTypes.remove(cargo); }); } - _creatCarton() { - // double l = double.parse(_lengthCtl.text, (s) => 0); - // double w = double.parse(_widthCtl.text, (s) => 0); - // double h = double.parse(_heightCtl.text, (s) => 0); - // _box.length = l; - // _box.width = w; - // _box.height = h; - // Navigator.pop(context, _box); + _creatCarton() async { + if ((_cargoTypes?.length ?? 0) == 0) { + showMsgDialog(context, "Error", "Expect at least one cargo type"); + return; + } + double l = double.parse(_lengthCtl.text, (s) => 0); + double w = double.parse(_widthCtl.text, (s) => 0); + double h = double.parse(_heightCtl.text, (s) => 0); + if ((l <= 0 || w <= 0 || h <= 0)) { + showMsgDialog(context, "Error", "Invalid dimension"); + return; + } + if (_deliveryAddress == null) { + showMsgDialog(context, "Error", "Invalid delivery address"); + return; + } + + Carton carton = Carton(); + carton.id = _carton.id; + carton.cartonType = _carton.cartonType; + carton.fcsShipmentID = _carton.fcsShipmentID; + carton.userID = _carton.userID; + carton.cargoTypes = _cargoTypes; + carton.packages = _carton.packages.where((e) => e.isChecked).toList(); + // carton.cartonSizeID = selectedCatonSize?.id; + carton.length = l; + carton.width = w; + carton.height = h; + carton.deliveryAddress = _deliveryAddress; + setState(() { + _isLoading = true; + }); + try { + CartonModel cartonModel = + Provider.of(context, listen: false); + if (widget.isNew) { + await cartonModel.createCarton(carton); + } else { + await cartonModel.updateCarton(carton); + } + Navigator.pop(context, true); + } catch (e) { + showMsgDialog(context, "Error", e.toString()); + } finally { + setState(() { + _isLoading = false; + }); + } } }