2020-10-20 06:19:10 +06:30
|
|
|
import 'package:fcs/domain/entities/cargo_type.dart';
|
|
|
|
|
import 'package:fcs/helpers/theme.dart';
|
2020-12-04 17:28:21 +06:30
|
|
|
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: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 {
|
2021-09-10 12:00:08 +06:30
|
|
|
final List<CargoType>? cargoTypes;
|
|
|
|
|
final bool? isNew;
|
|
|
|
|
final OnRemove? onRemove;
|
|
|
|
|
final OnUpdate? onUpdate;
|
2020-10-20 06:19:10 +06:30
|
|
|
|
2020-12-11 17:34:56 +06:30
|
|
|
const CargoTable(
|
2021-09-10 12:00:08 +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-09-10 12:00:08 +06:30
|
|
|
List<CargoType>? cargoTypes;
|
2020-12-08 20:24:15 +06:30
|
|
|
|
2020-12-11 17:34:56 +06:30
|
|
|
@override
|
|
|
|
|
void initState() {
|
2021-01-25 16:09:41 +06:30
|
|
|
cargoTypes = widget.cargoTypes;
|
2021-09-10 12:00:08 +06:30
|
|
|
if (!widget.isNew!) {
|
2021-01-25 16:09:41 +06:30
|
|
|
totalWeight =
|
2021-09-10 16:33:52 +06:30
|
|
|
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) {
|
2020-12-10 20:06:15 +06:30
|
|
|
return SingleChildScrollView(
|
|
|
|
|
scrollDirection: Axis.horizontal,
|
2021-09-13 11:09:32 +06:30
|
|
|
child: DataTable(
|
|
|
|
|
showCheckboxColumn: false,
|
2020-12-10 20:06:15 +06:30
|
|
|
headingRowHeight: 40,
|
2024-01-29 17:18:23 +06:30
|
|
|
// columnSpacing: 40,
|
2024-01-25 17:40:35 +06:30
|
|
|
decoration: BoxDecoration(border: Border.all(color: Colors.white)),
|
|
|
|
|
border: TableBorder(horizontalInside: BorderSide(color: Colors.white)),
|
2020-12-10 20:06:15 +06:30
|
|
|
columns: [
|
2021-09-13 11:09:32 +06:30
|
|
|
DataColumn(
|
2020-12-10 20:06:15 +06:30
|
|
|
label: LocalText(
|
|
|
|
|
context,
|
|
|
|
|
"cargo.type",
|
|
|
|
|
color: Colors.grey,
|
|
|
|
|
),
|
2020-10-20 06:19:10 +06:30
|
|
|
),
|
2021-09-13 11:09:32 +06:30
|
|
|
DataColumn(
|
2020-12-10 20:06:15 +06:30
|
|
|
label: LocalText(
|
|
|
|
|
context,
|
|
|
|
|
"cargo.qty",
|
|
|
|
|
color: Colors.grey,
|
|
|
|
|
),
|
2020-10-20 06:19:10 +06:30
|
|
|
),
|
2021-09-13 11:09:32 +06:30
|
|
|
DataColumn(
|
2020-12-10 20:06:15 +06:30
|
|
|
label: LocalText(
|
|
|
|
|
context,
|
|
|
|
|
"cargo.weight",
|
|
|
|
|
color: Colors.grey,
|
|
|
|
|
),
|
|
|
|
|
),
|
|
|
|
|
],
|
|
|
|
|
rows: getCargoRows(context),
|
|
|
|
|
),
|
2020-10-20 06:19:10 +06:30
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
2021-09-13 11:09:32 +06:30
|
|
|
List<DataRow> getCargoRows(BuildContext context) {
|
2021-01-25 16:09:41 +06:30
|
|
|
if (cargoTypes == null) {
|
2020-12-02 20:55:00 +06:30
|
|
|
return [];
|
|
|
|
|
}
|
2021-09-10 12:00:08 +06:30
|
|
|
var rows = cargoTypes!.map((c) {
|
2021-09-13 11:09:32 +06:30
|
|
|
return DataRow(
|
|
|
|
|
onSelectChanged: (bool? selected) async {},
|
2020-12-02 20:55:00 +06:30
|
|
|
cells: [
|
2021-09-13 11:09:32 +06:30
|
|
|
DataCell(
|
2020-12-11 17:34:56 +06:30
|
|
|
new Text(
|
2021-09-10 16:33:52 +06:30
|
|
|
c.name ?? '',
|
2020-12-11 17:34:56 +06:30
|
|
|
style: textStyle,
|
|
|
|
|
),
|
|
|
|
|
),
|
2021-09-13 11:09:32 +06:30
|
|
|
DataCell(
|
2024-02-05 17:49:12 +06:30
|
|
|
c.isCutomDuty
|
2020-12-11 17:34:56 +06:30
|
|
|
? GestureDetector(
|
2020-12-10 20:06:15 +06:30
|
|
|
onTap: () async {
|
2024-01-23 16:28:08 +06:30
|
|
|
String? _t = await showDialog(
|
2020-12-10 20:06:15 +06:30
|
|
|
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-09-10 12:00:08 +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(
|
2024-01-23 16:28:08 +06:30
|
|
|
c.qty.toString(),
|
2020-12-10 20:06:15 +06:30
|
|
|
style: textStyle,
|
|
|
|
|
textAlign: TextAlign.center,
|
|
|
|
|
),
|
|
|
|
|
),
|
|
|
|
|
),
|
|
|
|
|
)
|
|
|
|
|
: Center(
|
|
|
|
|
child: new Text(
|
|
|
|
|
"-",
|
|
|
|
|
style: textStyle,
|
|
|
|
|
),
|
|
|
|
|
),
|
|
|
|
|
),
|
2021-09-13 11:09:32 +06:30
|
|
|
DataCell(
|
2020-12-02 20:55:00 +06:30
|
|
|
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;
|
|
|
|
|
}
|
|
|
|
|
|
2024-01-23 16:28:08 +06:30
|
|
|
String? _t = await showDialog(
|
2020-12-10 20:06:15 +06:30
|
|
|
context: context,
|
|
|
|
|
builder: (_) => DialogInput(
|
|
|
|
|
label: "cargo.weight",
|
2021-09-10 16:33:52 +06:30
|
|
|
value: c.weight.toStringAsFixed(2)));
|
2020-12-10 20:06:15 +06:30
|
|
|
|
|
|
|
|
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-09-10 12:00:08 +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)),
|
|
|
|
|
),
|
2024-01-23 16:28:08 +06:30
|
|
|
child: Text(c.weight.toStringAsFixed(2), style: textStyle),
|
2020-12-10 20:06:15 +06:30
|
|
|
),
|
|
|
|
|
),
|
2020-12-02 20:55:00 +06:30
|
|
|
widget.onRemove == null
|
|
|
|
|
? SizedBox(
|
|
|
|
|
width: 50,
|
|
|
|
|
)
|
|
|
|
|
: IconButton(
|
|
|
|
|
icon: Icon(
|
|
|
|
|
Icons.remove_circle,
|
|
|
|
|
color: primaryColor,
|
|
|
|
|
),
|
|
|
|
|
onPressed: () {
|
2021-09-10 12:00:08 +06:30
|
|
|
if (widget.onRemove != null) widget.onRemove!(c);
|
2020-12-02 20:55:00 +06:30
|
|
|
})
|
|
|
|
|
],
|
|
|
|
|
),
|
|
|
|
|
),
|
|
|
|
|
],
|
|
|
|
|
);
|
|
|
|
|
}).toList();
|
|
|
|
|
|
2021-09-13 11:09:32 +06:30
|
|
|
var totalRow = DataRow(
|
|
|
|
|
onSelectChanged: (bool? selected) {},
|
2020-12-02 20:55:00 +06:30
|
|
|
cells: [
|
2021-09-13 11:09:32 +06:30
|
|
|
DataCell(Align(
|
2020-12-02 20:55:00 +06:30
|
|
|
alignment: Alignment.centerRight,
|
|
|
|
|
child: LocalText(
|
|
|
|
|
context,
|
|
|
|
|
"shipment.cargo.total",
|
|
|
|
|
color: Colors.black87,
|
|
|
|
|
fontWeight: FontWeight.bold,
|
|
|
|
|
),
|
|
|
|
|
)),
|
2021-09-13 11:09:32 +06:30
|
|
|
DataCell(Text("")),
|
|
|
|
|
DataCell(
|
2020-12-02 20:55:00 +06:30
|
|
|
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 {
|
2021-10-11 17:09:47 +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(
|
2020-12-08 20:24:15 +06:30
|
|
|
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() {
|
2021-09-10 12:00:08 +06:30
|
|
|
var cargoType = autoCalWeight(cargoTypes!, totalWeight);
|
2021-01-25 16:09:41 +06:30
|
|
|
if (cargoType == null) return;
|
2020-12-08 20:24:15 +06:30
|
|
|
|
2021-01-25 16:09:41 +06:30
|
|
|
setState(() {
|
2021-09-10 12:00:08 +06:30
|
|
|
cargoTypes!.remove(cargoType);
|
|
|
|
|
cargoTypes!.add(cargoType);
|
2021-01-25 16:09:41 +06:30
|
|
|
});
|
|
|
|
|
|
|
|
|
|
if (widget.onUpdate != null) {
|
2021-09-10 12:00:08 +06:30
|
|
|
widget.onUpdate!(cargoType);
|
2021-01-25 16:09:41 +06:30
|
|
|
}
|
2020-12-08 20:24:15 +06:30
|
|
|
}
|
2020-10-20 06:19:10 +06:30
|
|
|
}
|
2021-01-25 16:09:41 +06:30
|
|
|
|
2021-09-10 12:00:08 +06:30
|
|
|
CargoType? autoCalWeight(List<CargoType> cargoTypes, double total) {
|
2021-01-25 16:09:41 +06:30
|
|
|
List<CargoType> noWeight = cargoTypes.where((c) => c.weight == 0).toList();
|
|
|
|
|
if (noWeight.length != 1) return null;
|
|
|
|
|
|
2021-09-10 16:33:52 +06:30
|
|
|
double _existing =
|
2021-01-25 16:09:41 +06:30
|
|
|
cargoTypes.fold(0, (previous, current) => previous + current.weight);
|
|
|
|
|
|
|
|
|
|
noWeight[0].weight = total - _existing;
|
|
|
|
|
return noWeight[0];
|
|
|
|
|
}
|