fix carton

This commit is contained in:
2021-01-25 16:09:41 +06:30
parent 020b748272
commit 14183410d2
9 changed files with 175 additions and 216 deletions

View File

@@ -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];
}