update carton info
This commit is contained in:
@@ -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<PackageCartonEditor> {
|
||||
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<CargoType> _cargoTypes = [];
|
||||
|
||||
@override
|
||||
void initState() {
|
||||
super.initState();
|
||||
|
||||
volumetricRatio = Provider.of<ShipmentRateModel>(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<DeliveryAddressModel>(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<PackageCartonEditor> {
|
||||
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<PackageCartonEditor> {
|
||||
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<PackageCartonEditor> {
|
||||
dimBox,
|
||||
LocalTitle(textKey: "box.delivery_address"),
|
||||
DefaultDeliveryAddress(
|
||||
deliveryAddress: _box.deliveryAddress,
|
||||
deliveryAddress: _deliveryAddress,
|
||||
labelKey: "box.delivery_address",
|
||||
onTap: () async {
|
||||
DeliveryAddress d = await Navigator.push<DeliveryAddress>(
|
||||
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<PackageCartonEditor> {
|
||||
_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<CartonModel>(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;
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user