fix carton
This commit is contained in:
@@ -1,8 +1,6 @@
|
||||
import 'package:fcs/domain/entities/cargo_type.dart';
|
||||
import 'package:fcs/domain/entities/custom_duty.dart';
|
||||
import 'package:fcs/helpers/theme.dart';
|
||||
import 'package:fcs/pages/main/util.dart';
|
||||
import 'package:fcs/pages/rates/custom_list.dart';
|
||||
import 'package:fcs/pages/rates/model/shipment_rate_model.dart';
|
||||
import 'package:fcs/pages/widgets/local_text.dart';
|
||||
import 'package:fcs/pages/widgets/local_title.dart';
|
||||
@@ -20,19 +18,31 @@ class _CargoTypeAdditionState extends State<CargoTypeAddition> {
|
||||
bool _isLoading = false;
|
||||
|
||||
List<CargoType> cargos = [];
|
||||
List<CargoType> specialCargos = [];
|
||||
|
||||
@override
|
||||
void initState() {
|
||||
super.initState();
|
||||
var shipmentRateModel =
|
||||
Provider.of<ShipmentRateModel>(context, listen: false);
|
||||
cargos = List.from(shipmentRateModel.rate.cargoTypes);
|
||||
|
||||
cargos = shipmentRateModel.rate.cargoTypes.map((e) => e.clone()).toList();
|
||||
specialCargos = List.from(shipmentRateModel.rate.customDuties);
|
||||
specialCargos =
|
||||
shipmentRateModel.rate.customDuties.map((e) => e.clone()).toList();
|
||||
|
||||
cargos.forEach((p) {
|
||||
p.isChecked = false;
|
||||
p.isCutomDuty = false;
|
||||
p.weight = 0;
|
||||
p.qty = null;
|
||||
});
|
||||
specialCargos.forEach((p) {
|
||||
p.isChecked = false;
|
||||
p.isCutomDuty = true;
|
||||
p.weight = 0;
|
||||
p.qty = 1;
|
||||
});
|
||||
}
|
||||
|
||||
@override
|
||||
@@ -42,40 +52,31 @@ class _CargoTypeAdditionState extends State<CargoTypeAddition> {
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
List<Widget> getCargoRowList() {
|
||||
return cargos.map((c) {
|
||||
List<Widget> getCargoRowList(List<CargoType> _c) {
|
||||
return _c.map((c) {
|
||||
return Container(
|
||||
child: Container(
|
||||
padding:
|
||||
EdgeInsets.only(left: 10.0, right: 5.0, top: 3.0, bottom: 3.0),
|
||||
child: Row(
|
||||
children: <Widget>[
|
||||
Checkbox(
|
||||
value: c.isChecked,
|
||||
activeColor: primaryColor,
|
||||
onChanged: (bool check) {
|
||||
setState(() {
|
||||
c.isChecked = check;
|
||||
});
|
||||
}),
|
||||
new Text(c.name, style: textStyle),
|
||||
c.isCutomDuty
|
||||
? Padding(
|
||||
padding: const EdgeInsets.only(left: 15),
|
||||
child: IconButton(
|
||||
onPressed: () {
|
||||
setState(() {
|
||||
cargos.removeWhere((t) => t.name == c.name);
|
||||
});
|
||||
},
|
||||
icon: Icon(
|
||||
Icons.remove_circle,
|
||||
color: Colors.black45,
|
||||
),
|
||||
),
|
||||
)
|
||||
: Container(),
|
||||
],
|
||||
child: InkWell(
|
||||
onTap: () {
|
||||
setState(() {
|
||||
c.isChecked = !c.isChecked;
|
||||
});
|
||||
},
|
||||
child: Row(
|
||||
children: <Widget>[
|
||||
Checkbox(
|
||||
value: c.isChecked,
|
||||
activeColor: primaryColor,
|
||||
onChanged: (bool check) {
|
||||
setState(() {
|
||||
c.isChecked = check;
|
||||
});
|
||||
}),
|
||||
new Text(c.name, style: textStyle),
|
||||
],
|
||||
),
|
||||
),
|
||||
),
|
||||
);
|
||||
@@ -88,6 +89,9 @@ class _CargoTypeAdditionState extends State<CargoTypeAddition> {
|
||||
callack: () {
|
||||
List<CargoType> _cargos =
|
||||
this.cargos.where((c) => c.isChecked).toList();
|
||||
List<CargoType> _scargos =
|
||||
this.specialCargos.where((c) => c.isChecked).toList();
|
||||
_cargos.addAll(_scargos);
|
||||
Navigator.pop(context, _cargos);
|
||||
},
|
||||
);
|
||||
@@ -117,21 +121,15 @@ class _CargoTypeAdditionState extends State<CargoTypeAddition> {
|
||||
children: <Widget>[
|
||||
LocalTitle(
|
||||
textKey: "box.select.cargo.title",
|
||||
trailing: IconButton(
|
||||
icon: Icon(
|
||||
Icons.add_circle,
|
||||
color: primaryColor,
|
||||
),
|
||||
onPressed: () async {
|
||||
CargoType customDuty = await Navigator.of(context).push(
|
||||
CupertinoPageRoute(
|
||||
builder: (context) =>
|
||||
CustomList(selected: true)));
|
||||
_addCustom(customDuty);
|
||||
}),
|
||||
),
|
||||
Column(
|
||||
children: getCargoRowList(),
|
||||
children: getCargoRowList(cargos),
|
||||
),
|
||||
LocalTitle(
|
||||
textKey: "box.select.cargo.title",
|
||||
),
|
||||
Column(
|
||||
children: getCargoRowList(specialCargos),
|
||||
),
|
||||
SizedBox(height: 30),
|
||||
saveBtn,
|
||||
@@ -142,21 +140,4 @@ class _CargoTypeAdditionState extends State<CargoTypeAddition> {
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
_addCustom(CargoType customDuty) {
|
||||
if (customDuty == null) return;
|
||||
|
||||
if (cargos.any((c) => c.name == customDuty.name)) return;
|
||||
|
||||
// setState(() {
|
||||
// cargos.add(CargoType(
|
||||
// name: customDuty.productType, isCutomDuty: true, qty: 1, weight: 0));
|
||||
// });
|
||||
|
||||
setState(() {
|
||||
customDuty.qty = 1;
|
||||
customDuty.isChecked = false;
|
||||
cargos.add(customDuty);
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,6 +1,5 @@
|
||||
import 'package:fcs/domain/entities/cargo_type.dart';
|
||||
import 'package:fcs/helpers/theme.dart';
|
||||
import 'package:fcs/pages/main/model/main_model.dart';
|
||||
import 'package:fcs/pages/main/util.dart';
|
||||
import 'package:fcs/pages/rates/model/shipment_rate_model.dart';
|
||||
import 'package:fcs/pages/widgets/input_text.dart';
|
||||
|
||||
@@ -7,19 +7,17 @@ import 'package:fcs/pages/widgets/my_data_table.dart';
|
||||
import 'package:flutter/cupertino.dart';
|
||||
import 'package:flutter/material.dart';
|
||||
|
||||
import 'cargo_type_editor.dart';
|
||||
|
||||
typedef OnAdd(CargoType cargoType);
|
||||
typedef OnRemove(CargoType cargoType);
|
||||
typedef OnUpdate(CargoType cargoType);
|
||||
|
||||
class CargoTable extends StatefulWidget {
|
||||
final List<CargoType> cargoTypes;
|
||||
final bool isNew;
|
||||
final OnAdd onAdd;
|
||||
final OnRemove onRemove;
|
||||
final OnUpdate onUpdate;
|
||||
|
||||
const CargoTable(
|
||||
{Key key, this.cargoTypes, this.isNew, this.onAdd, this.onRemove})
|
||||
{Key key, this.cargoTypes, this.isNew, this.onRemove, this.onUpdate})
|
||||
: super(key: key);
|
||||
|
||||
@override
|
||||
@@ -28,26 +26,23 @@ class CargoTable extends StatefulWidget {
|
||||
|
||||
class _CargoTableState extends State<CargoTable> {
|
||||
double totalWeight = 0;
|
||||
List<CargoType> _cargos = [];
|
||||
double remainingWeight = 0;
|
||||
List<String> _list = [];
|
||||
List<String> _types = [];
|
||||
List<CargoType> cargoTypes;
|
||||
|
||||
@override
|
||||
void initState() {
|
||||
cargoTypes = widget.cargoTypes;
|
||||
if (!widget.isNew) {
|
||||
totalWeight = widget.cargoTypes
|
||||
.fold(0, (previous, current) => previous + current.weight);
|
||||
totalWeight =
|
||||
cargoTypes.fold(0, (previous, current) => previous + current.weight);
|
||||
}
|
||||
|
||||
super.initState();
|
||||
}
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
remainingWeight =
|
||||
widget.cargoTypes.length == 0 ? this.totalWeight : remainingWeight;
|
||||
this._cargos = widget.cargoTypes.length == 0 ? [] : this._cargos;
|
||||
|
||||
print("Cargotypes:${cargoTypes.length}");
|
||||
|
||||
return SingleChildScrollView(
|
||||
scrollDirection: Axis.horizontal,
|
||||
child: MyDataTable(
|
||||
@@ -82,13 +77,10 @@ class _CargoTableState extends State<CargoTable> {
|
||||
}
|
||||
|
||||
List<MyDataRow> getCargoRows(BuildContext context) {
|
||||
if (widget.cargoTypes == null) {
|
||||
if (cargoTypes == null) {
|
||||
return [];
|
||||
}
|
||||
|
||||
CargoType cargo;
|
||||
|
||||
var rows = widget.cargoTypes.map((c) {
|
||||
var rows = cargoTypes.map((c) {
|
||||
return MyDataRow(
|
||||
onSelectChanged: (bool selected) async {},
|
||||
cells: [
|
||||
@@ -109,8 +101,9 @@ class _CargoTableState extends State<CargoTable> {
|
||||
|
||||
if (_t == null) return;
|
||||
setState(() {
|
||||
c.qty = int.parse(_t);
|
||||
c.qty = int.tryParse(_t) ?? 0;
|
||||
});
|
||||
if (widget.onUpdate != null) widget.onUpdate(c);
|
||||
},
|
||||
child: Center(
|
||||
child: Container(
|
||||
@@ -155,37 +148,12 @@ class _CargoTableState extends State<CargoTable> {
|
||||
|
||||
if (_t == null) return;
|
||||
setState(() {
|
||||
c.weight = double.parse(_t);
|
||||
c.weight = double.tryParse(_t) ?? 0;
|
||||
});
|
||||
|
||||
cargo = c;
|
||||
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;
|
||||
setState(() {
|
||||
this._cargos = [];
|
||||
});
|
||||
}
|
||||
});
|
||||
}
|
||||
});
|
||||
this.remainingWeight = this.totalWeight;
|
||||
if (c.weight != 0) {
|
||||
_cal();
|
||||
}
|
||||
if (widget.onUpdate != null) widget.onUpdate(c);
|
||||
},
|
||||
child: Container(
|
||||
padding: const EdgeInsets.all(7.0),
|
||||
@@ -246,9 +214,9 @@ class _CargoTableState extends State<CargoTable> {
|
||||
|
||||
if (_t == null) return;
|
||||
setState(() {
|
||||
totalWeight = double.parse(_t);
|
||||
remainingWeight = totalWeight;
|
||||
totalWeight = double.tryParse(_t) ?? 0;
|
||||
});
|
||||
_cal();
|
||||
},
|
||||
child: Container(
|
||||
padding: const EdgeInsets.all(7.0),
|
||||
@@ -268,77 +236,29 @@ class _CargoTableState extends State<CargoTable> {
|
||||
return rows;
|
||||
}
|
||||
|
||||
List<MyDataRow> _getCargoRows(BuildContext context) {
|
||||
if (widget.cargoTypes == null) {
|
||||
return [];
|
||||
}
|
||||
double total = 0;
|
||||
var rows = widget.cargoTypes.map((c) {
|
||||
total += c.weight;
|
||||
return MyDataRow(
|
||||
onSelectChanged: (bool selected) async {
|
||||
CargoType cargo = await Navigator.push<CargoType>(
|
||||
context,
|
||||
CupertinoPageRoute(
|
||||
builder: (context) => CargoTypeEditor(
|
||||
cargo: c,
|
||||
)));
|
||||
if (widget.onAdd != null) widget.onAdd(cargo);
|
||||
},
|
||||
cells: [
|
||||
MyDataCell(new Text(
|
||||
c.name == null ? "" : c.name,
|
||||
style: textStyle,
|
||||
)),
|
||||
MyDataCell(
|
||||
Row(
|
||||
mainAxisAlignment: MainAxisAlignment.end,
|
||||
children: [
|
||||
Text(c.weight == null ? "0" : c.weight.toStringAsFixed(2),
|
||||
style: textStyle),
|
||||
widget.onRemove == null
|
||||
? SizedBox(
|
||||
width: 50,
|
||||
)
|
||||
: IconButton(
|
||||
icon: Icon(
|
||||
Icons.remove_circle,
|
||||
color: primaryColor,
|
||||
),
|
||||
onPressed: () {
|
||||
if (widget.onRemove != null) widget.onRemove(c);
|
||||
})
|
||||
],
|
||||
),
|
||||
),
|
||||
],
|
||||
);
|
||||
}).toList();
|
||||
_cal() {
|
||||
var cargoType = autoCalWeight(cargoTypes, totalWeight);
|
||||
if (cargoType == null) return;
|
||||
|
||||
var totalRow = MyDataRow(
|
||||
onSelectChanged: (bool selected) {},
|
||||
cells: [
|
||||
MyDataCell(Align(
|
||||
alignment: Alignment.centerRight,
|
||||
child: LocalText(
|
||||
context,
|
||||
"shipment.cargo.total",
|
||||
color: Colors.black87,
|
||||
fontWeight: FontWeight.bold,
|
||||
),
|
||||
)),
|
||||
MyDataCell(
|
||||
Padding(
|
||||
padding: const EdgeInsets.only(right: 48.0),
|
||||
child: Align(
|
||||
alignment: Alignment.centerRight,
|
||||
child: Text(total.toStringAsFixed(2),
|
||||
style: TextStyle(fontWeight: FontWeight.bold))),
|
||||
),
|
||||
),
|
||||
],
|
||||
);
|
||||
rows.add(totalRow);
|
||||
return rows;
|
||||
setState(() {
|
||||
cargoTypes.remove(cargoType);
|
||||
cargoTypes.add(cargoType);
|
||||
});
|
||||
|
||||
if (widget.onUpdate != null) {
|
||||
widget.onUpdate(cargoType);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
CargoType autoCalWeight(List<CargoType> cargoTypes, double total) {
|
||||
if ((cargoTypes?.length ?? 0) == 0 || total == 0) return null;
|
||||
List<CargoType> noWeight = cargoTypes.where((c) => c.weight == 0).toList();
|
||||
if (noWeight.length != 1) return null;
|
||||
|
||||
var _existing =
|
||||
cargoTypes.fold(0, (previous, current) => previous + current.weight);
|
||||
|
||||
noWeight[0].weight = total - _existing;
|
||||
return noWeight[0];
|
||||
}
|
||||
|
||||
@@ -76,6 +76,8 @@ class _CartonEditorState extends State<CartonEditor> {
|
||||
User sender;
|
||||
List<Carton> _cartonsFromCartons = [];
|
||||
|
||||
double totalWeight;
|
||||
|
||||
@override
|
||||
void initState() {
|
||||
super.initState();
|
||||
@@ -410,8 +412,8 @@ class _CartonEditorState extends State<CartonEditor> {
|
||||
CupertinoPageRoute(builder: (context) => CargoTypeAddition()));
|
||||
if (cargos == null) return;
|
||||
setState(() {
|
||||
_cargoTypes.clear();
|
||||
_cargoTypes.addAll(cargos);
|
||||
_cargoTypes.addAll(
|
||||
cargos.where((e) => !_cargoTypes.contains(e)).toList());
|
||||
});
|
||||
}),
|
||||
);
|
||||
@@ -419,8 +421,8 @@ class _CartonEditorState extends State<CartonEditor> {
|
||||
final cargoTableBox = CargoTable(
|
||||
isNew: _isNew,
|
||||
cargoTypes: _cargoTypes,
|
||||
onAdd: (c) => _addCargo(c),
|
||||
onRemove: (c) => _removeCargo(c),
|
||||
onUpdate: (c) => _updateCargo(c),
|
||||
);
|
||||
|
||||
final lengthBox = LengthPicker(
|
||||
@@ -779,20 +781,23 @@ class _CartonEditorState extends State<CartonEditor> {
|
||||
);
|
||||
}
|
||||
|
||||
_addCargo(CargoType cargo) {
|
||||
if (cargo == null) return;
|
||||
setState(() {
|
||||
_cargoTypes.remove(cargo);
|
||||
_cargoTypes.add(cargo);
|
||||
});
|
||||
}
|
||||
|
||||
_removeCargo(CargoType cargo) {
|
||||
setState(() {
|
||||
_cargoTypes.remove(cargo);
|
||||
});
|
||||
}
|
||||
|
||||
_updateCargo(CargoType cargo) {
|
||||
setState(() {
|
||||
var _c =
|
||||
_cargoTypes.firstWhere((e) => e.id == cargo.id, orElse: () => null);
|
||||
if (_c != null) {
|
||||
_c.weight = cargo.weight;
|
||||
_c.qty = cargo.qty;
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
_addCarton() async {
|
||||
bool isFromPackages = _selectedCartonType == carton_from_packages;
|
||||
bool isFromCartons = _selectedCartonType == carton_from_cartons;
|
||||
@@ -931,6 +936,10 @@ class _CartonEditorState extends State<CartonEditor> {
|
||||
showMsgDialog(context, "Error", "Expect at least one cargo type");
|
||||
return;
|
||||
}
|
||||
if (_cargoTypes.where((c) => c.weight <= 0).isNotEmpty) {
|
||||
showMsgDialog(context, "Error", "Invalid cargo weight");
|
||||
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);
|
||||
|
||||
@@ -138,9 +138,10 @@ class _PackageCartonEditorState extends State<PackageCartonEditor> {
|
||||
context,
|
||||
CupertinoPageRoute(builder: (context) => CargoTypeAddition()));
|
||||
if (cargos == null) return;
|
||||
if (cargos == null) return;
|
||||
setState(() {
|
||||
_cargoTypes.clear();
|
||||
_cargoTypes.addAll(cargos);
|
||||
_cargoTypes.addAll(
|
||||
cargos.where((e) => !_cargoTypes.contains(e)).toList());
|
||||
});
|
||||
}),
|
||||
);
|
||||
@@ -148,8 +149,8 @@ class _PackageCartonEditorState extends State<PackageCartonEditor> {
|
||||
final cargoTableBox = CargoTable(
|
||||
isNew: widget.isNew,
|
||||
cargoTypes: _cargoTypes,
|
||||
onAdd: (c) => _addCargo(c),
|
||||
onRemove: (c) => _removeCargo(c),
|
||||
onUpdate: (c) => _updateCargo(c),
|
||||
);
|
||||
|
||||
return LocalProgress(
|
||||
@@ -286,20 +287,23 @@ class _PackageCartonEditorState extends State<PackageCartonEditor> {
|
||||
);
|
||||
}
|
||||
|
||||
_addCargo(CargoType cargo) {
|
||||
if (cargo == null) return;
|
||||
setState(() {
|
||||
_cargoTypes.remove(cargo);
|
||||
_cargoTypes.add(cargo);
|
||||
});
|
||||
}
|
||||
|
||||
_removeCargo(CargoType cargo) {
|
||||
setState(() {
|
||||
_cargoTypes.remove(cargo);
|
||||
});
|
||||
}
|
||||
|
||||
_updateCargo(CargoType cargo) {
|
||||
setState(() {
|
||||
var _c =
|
||||
_cargoTypes.firstWhere((e) => e.id == cargo.id, orElse: () => null);
|
||||
if (_c != null) {
|
||||
_c.weight = cargo.weight;
|
||||
_c.qty = cargo.qty;
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
_creatCarton() async {
|
||||
if ((_cargoTypes?.length ?? 0) == 0) {
|
||||
showMsgDialog(context, "Error", "Expect at least one cargo type");
|
||||
|
||||
Reference in New Issue
Block a user