Files
fcs/lib/pages/carton/carton_cargo_table.dart

265 lines
8.1 KiB
Dart
Raw Normal View History

2020-10-20 06:19:10 +06:30
import 'package:fcs/domain/entities/cargo_type.dart';
import 'package:fcs/helpers/theme.dart';
import 'package:fcs/pages/main/util.dart';
2020-12-10 20:06:15 +06:30
import 'package:fcs/pages/widgets/dialog_input.dart';
2020-10-20 06:19:10 +06:30
import 'package:fcs/pages/widgets/local_text.dart';
import 'package:fcs/pages/widgets/my_data_table.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
typedef OnRemove(CargoType cargoType);
2021-01-25 16:09:41 +06:30
typedef OnUpdate(CargoType cargoType);
2020-10-20 06:19:10 +06:30
2020-12-02 20:55:00 +06:30
class CargoTable extends StatefulWidget {
2020-10-20 06:19:10 +06:30
final List<CargoType> cargoTypes;
2020-12-11 17:34:56 +06:30
final bool isNew;
2020-10-20 06:19:10 +06:30
final OnRemove onRemove;
2021-01-25 16:09:41 +06:30
final OnUpdate onUpdate;
2020-10-20 06:19:10 +06:30
2020-12-11 17:34:56 +06:30
const CargoTable(
2021-01-25 16:09:41 +06:30
{Key key, this.cargoTypes, this.isNew, this.onRemove, this.onUpdate})
2020-10-20 06:19:10 +06:30
: super(key: key);
2020-12-02 20:55:00 +06:30
@override
_CargoTableState createState() => _CargoTableState();
}
class _CargoTableState extends State<CargoTable> {
double totalWeight = 0;
2021-01-25 16:09:41 +06:30
List<CargoType> cargoTypes;
2020-12-11 17:34:56 +06:30
@override
void initState() {
2021-01-25 16:09:41 +06:30
cargoTypes = widget.cargoTypes;
2020-12-11 17:34:56 +06:30
if (!widget.isNew) {
2021-01-25 16:09:41 +06:30
totalWeight =
cargoTypes.fold(0, (previous, current) => previous + current.weight);
2020-12-11 17:34:56 +06:30
}
2021-01-25 16:09:41 +06:30
2020-12-11 17:34:56 +06:30
super.initState();
}
2020-10-20 06:19:10 +06:30
@override
Widget build(BuildContext context) {
2021-01-25 16:09:41 +06:30
print("Cargotypes:${cargoTypes.length}");
2020-12-10 20:06:15 +06:30
return SingleChildScrollView(
scrollDirection: Axis.horizontal,
child: MyDataTable(
headingRowHeight: 40,
columnSpacing: 40,
columns: [
MyDataColumn(
label: LocalText(
context,
"cargo.type",
color: Colors.grey,
),
2020-10-20 06:19:10 +06:30
),
2020-12-10 20:06:15 +06:30
MyDataColumn(
label: LocalText(
context,
"cargo.qty",
color: Colors.grey,
),
2020-10-20 06:19:10 +06:30
),
2020-12-10 20:06:15 +06:30
MyDataColumn(
label: LocalText(
context,
"cargo.weight",
color: Colors.grey,
),
),
],
rows: getCargoRows(context),
),
2020-10-20 06:19:10 +06:30
);
}
List<MyDataRow> getCargoRows(BuildContext context) {
2021-01-25 16:09:41 +06:30
if (cargoTypes == null) {
2020-12-02 20:55:00 +06:30
return [];
}
2021-01-25 16:09:41 +06:30
var rows = cargoTypes.map((c) {
2020-12-02 20:55:00 +06:30
return MyDataRow(
2020-12-11 17:34:56 +06:30
onSelectChanged: (bool selected) async {},
2020-12-02 20:55:00 +06:30
cells: [
2020-12-11 17:34:56 +06:30
MyDataCell(
new Text(
c.name == null ? "" : c.name,
style: textStyle,
),
),
2020-12-10 20:06:15 +06:30
MyDataCell(
c.isCutomDuty
2020-12-11 17:34:56 +06:30
? GestureDetector(
2020-12-10 20:06:15 +06:30
onTap: () async {
String _t = await showDialog(
context: context,
builder: (_) => DialogInput(
2021-01-09 19:11:47 +06:30
label: "cargo.qty", value: c.qty.toString()));
2020-12-10 20:06:15 +06:30
if (_t == null) return;
setState(() {
2021-01-25 16:09:41 +06:30
c.qty = int.tryParse(_t) ?? 0;
2020-12-10 20:06:15 +06:30
});
2021-01-25 16:09:41 +06:30
if (widget.onUpdate != null) widget.onUpdate(c);
2020-12-10 20:06:15 +06:30
},
child: Center(
child: Container(
width: 40,
padding: const EdgeInsets.all(7.0),
decoration: BoxDecoration(
border: Border.all(color: primaryColor),
borderRadius: BorderRadius.all(Radius.circular(5.0)),
),
child: new Text(
c.qty == null ? "" : c.qty.toString(),
style: textStyle,
textAlign: TextAlign.center,
),
),
),
)
: Center(
child: new Text(
"-",
style: textStyle,
),
),
),
2020-12-02 20:55:00 +06:30
MyDataCell(
Row(
2020-12-10 20:06:15 +06:30
mainAxisAlignment: MainAxisAlignment.center,
2020-12-02 20:55:00 +06:30
children: [
2020-12-10 20:06:15 +06:30
GestureDetector(
onTap: () async {
if (this.totalWeight <= 0) {
showMsgDialog(
context, "Error", "Please insert total weight");
return;
}
String _t = await showDialog(
context: context,
builder: (_) => DialogInput(
label: "cargo.weight",
value: c.weight.toStringAsFixed(2)));
if (_t == null) return;
setState(() {
2021-01-25 16:09:41 +06:30
c.weight = double.tryParse(_t) ?? 0;
2020-12-10 20:06:15 +06:30
});
2021-01-25 16:09:41 +06:30
if (c.weight != 0) {
_cal();
2020-12-10 20:06:15 +06:30
}
2021-01-25 16:09:41 +06:30
if (widget.onUpdate != null) widget.onUpdate(c);
2020-12-10 20:06:15 +06:30
},
child: Container(
padding: const EdgeInsets.all(7.0),
decoration: BoxDecoration(
border: Border.all(color: primaryColor),
borderRadius: BorderRadius.all(Radius.circular(5.0)),
),
child: Text(
c.weight == null ? "0.00" : c.weight.toStringAsFixed(2),
style: textStyle),
),
),
2020-12-02 20:55:00 +06:30
widget.onRemove == null
? SizedBox(
width: 50,
)
: IconButton(
icon: Icon(
Icons.remove_circle,
color: primaryColor,
),
onPressed: () {
if (widget.onRemove != null) widget.onRemove(c);
})
],
),
),
],
);
}).toList();
var totalRow = MyDataRow(
2020-12-07 17:18:26 +06:30
onSelectChanged: (bool selected) {},
2020-12-02 20:55:00 +06:30
cells: [
MyDataCell(Align(
alignment: Alignment.centerRight,
child: LocalText(
context,
"shipment.cargo.total",
color: Colors.black87,
fontWeight: FontWeight.bold,
),
)),
2020-12-10 20:06:15 +06:30
MyDataCell(Text("")),
2020-12-02 20:55:00 +06:30
MyDataCell(
Padding(
2020-12-10 20:06:15 +06:30
padding: const EdgeInsets.only(right: 48.0),
2020-12-02 20:55:00 +06:30
child: Align(
alignment: Alignment.centerRight,
2020-12-07 17:18:26 +06:30
child: InkWell(
onTap: () async {
2020-12-10 20:06:15 +06:30
String _t = await showDialog(
2021-01-08 17:13:51 +06:30
context: context,
builder: (_) => DialogInput(
label: "shipment.cargo.total",
value: totalWeight.toStringAsFixed(2)),
);
2020-12-10 20:06:15 +06:30
2020-12-07 17:18:26 +06:30
if (_t == null) return;
setState(() {
2021-01-25 16:09:41 +06:30
totalWeight = double.tryParse(_t) ?? 0;
2020-12-07 17:18:26 +06:30
});
2021-01-25 16:09:41 +06:30
_cal();
2020-12-07 17:18:26 +06:30
},
child: Container(
padding: const EdgeInsets.all(7.0),
decoration: BoxDecoration(
border: Border.all(color: primaryColor),
2020-12-07 17:18:26 +06:30
borderRadius: BorderRadius.all(Radius.circular(5.0)),
),
child: Text(totalWeight.toStringAsFixed(2),
style: TextStyle(fontWeight: FontWeight.bold)),
),
)),
2020-12-02 20:55:00 +06:30
),
),
],
);
rows.add(totalRow);
return rows;
}
2021-01-25 16:09:41 +06:30
_cal() {
var cargoType = autoCalWeight(cargoTypes, totalWeight);
if (cargoType == null) return;
2021-01-25 16:09:41 +06:30
setState(() {
cargoTypes.remove(cargoType);
cargoTypes.add(cargoType);
});
if (widget.onUpdate != null) {
widget.onUpdate(cargoType);
}
}
2020-10-20 06:19:10 +06:30
}
2021-01-25 16:09:41 +06:30
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];
}