update crgo type input for carton

This commit is contained in:
tzw
2025-03-24 18:23:17 +06:30
parent 9371c6b1a3
commit 3d4bc43de4
4 changed files with 321 additions and 290 deletions

View File

@@ -168,7 +168,7 @@ class Carton {
var _types = cargoTypes.where((t) => t.weight != 0).toList();
var _cargoTypes = _types.map((c) => c.toMapForCarton()).toList();
var _packagesIds = packages.map((c) => c.id).toList();
// var _packagesIds = packages.map((c) => c.id).toList();
var _surchareItems =
surchareItems.map((c) => c.toMapForSurcharge()).toList();
@@ -184,7 +184,7 @@ class Carton {
'length': length,
'width': width,
'height': height,
'package_ids': _packagesIds,
// 'package_ids': _packagesIds,
'cargo_types': _cargoTypes,
'surcharge_items': _surchareItems,
};

View File

@@ -47,14 +47,18 @@ class _CargoWidgetState extends State<CargoWidget> {
List<CargoType> _cargoTypes = [];
List<CargoType> _surchareItems = [];
TextEditingController totalCtl = TextEditingController();
List<TextEditingController> cargoTypeControllers = [];
List<TextEditingController> surchargeControllers = [];
List<CargoType> _mixCargoTypes = [];
List<TextEditingController> mixCargoTypeControllers = [];
bool get isKnownTotalWeight =>
bool get hasValueTotalWeight =>
totalCtl.text.isNotEmpty && totalCtl.text != '0';
bool get hasValueCargoes =>
_cargoTypes.isNotEmpty && _cargoTypes.every((e) => e.weight != 0);
double get actualTotalWeight =>
_cargoTypes.fold(0, (sum, value) => sum + value.weight);
String? error;
@override
void initState() {
@@ -63,25 +67,16 @@ class _CargoWidgetState extends State<CargoWidget> {
}
_init() {
totalCtl.addListener(totalInputChangeListener);
// for cargo types
if (widget.cargoTypes.isNotEmpty) {
List<CargoType> allCargoes = List.from(widget.cargoTypes);
_cargoTypes = allCargoes.where((e) => !e.isMixCargo).toList();
_cargoTypes = List.from(widget.cargoTypes);
for (var e in _cargoTypes) {
var editor = TextEditingController();
editor.text = removeTrailingZeros(e.weight);
editor.addListener(inputChangeListener);
cargoTypeControllers.add(editor);
}
_mixCargoTypes = allCargoes.where((e) => e.isMixCargo).toList();
for (var e in _mixCargoTypes) {
var editor = TextEditingController();
editor.text = removeTrailingZeros(e.weight);
editor.addListener(inputChangeListener);
mixCargoTypeControllers.add(editor);
}
_calculateTotalWeght();
onUpdated();
} else {
WidgetsBinding.instance.addPostFrameCallback((_) {
_openCargoTypeSelection();
@@ -94,7 +89,7 @@ class _CargoWidgetState extends State<CargoWidget> {
for (var e in _surchareItems) {
var editor = TextEditingController();
editor.text = e.qty.toString();
editor.addListener(inputChangeListenerForSurchage);
editor.addListener(inputChangeListener);
surchargeControllers.add(editor);
}
}
@@ -104,21 +99,12 @@ class _CargoWidgetState extends State<CargoWidget> {
}
}
totalInputChangeListener() {
print("Listen isKnownTotalWeight:$isKnownTotalWeight");
setState(() {});
}
inputChangeListenerForSurchage() {
setState(() {});
}
_openCargoTypeSelection() async {
List<CargoType>? cargoes = await showDialog(
context: context, builder: (_) => const CargoTypeAdditionDialog());
if (cargoes == null) return;
_cargoTypes = cargoes.where((e) => !e.isMixCargo).toList();
_mixCargoTypes = cargoes.where((e) => e.isMixCargo).toList();
_cargoTypes = cargoes;
_cargoTypes.sort((a, b) => (a == b ? 0 : (a.isMixCargo ? 1 : -1)));
for (var e in _cargoTypes) {
var editor = TextEditingController();
@@ -127,63 +113,60 @@ class _CargoWidgetState extends State<CargoWidget> {
cargoTypeControllers.add(editor);
}
for (var e in _mixCargoTypes) {
var editor = TextEditingController();
editor.text = '0';
editor.addListener(inputChangeListener);
mixCargoTypeControllers.add(editor);
}
_calculateTotalWeght();
if (mounted) {
setState(() {});
}
}
_calculateTotalWeght() {
print("_calculateTotalWeght isKnownTotalWeight:$isKnownTotalWeight");
double notMixTotal =
_cargoTypes.fold(0, (sum, value) => sum + value.weight);
double mixTotal =
_mixCargoTypes.fold(0, (sum, value) => sum + value.weight);
double total = notMixTotal + mixTotal;
if (isKnownTotalWeight) {
} else {
totalCtl.text = removeTrailingZeros(total);
}
}
// bool isFieldEmpty(int index) {
// return cargoTypeControllers[index].text.isEmpty;
// }
// List<int> getEmptyFields() {
// List<int> emptyFields = [];
// for (int i = 0; i < cargoTypeControllers.length; i++) {
// if (isFieldEmpty(i)) {
// emptyFields.add(i);
// }
// }
// return emptyFields;
// }
inputChangeListener() {
setState(() {});
}
List<int> getEmptyFields() {
List<int> emptyFields = [];
for (int i = 0; i < cargoTypeControllers.length; i++) {
if (cargoTypeControllers[i].text.trim().isEmpty ||
cargoTypeControllers[i].text.trim() == "0") {
emptyFields.add(i);
}
}
return emptyFields;
}
void onUpdated() {
if (!hasValueTotalWeight && hasValueCargoes) {
totalCtl.text = removeTrailingZeros(actualTotalWeight);
error = null;
} else {
// auto populate remaining value
double totalWeight = (double.tryParse(totalCtl.text) ?? 0);
if (actualTotalWeight > totalWeight) {
error = "Exceed total weight";
} else {
error = null;
double remainingWeight =
(totalWeight - actualTotalWeight).clamp(0, totalWeight);
List<int> emptyFieldIndexes = getEmptyFields();
if (emptyFieldIndexes.isNotEmpty) {
if (emptyFieldIndexes.length == 1) {
_cargoTypes.asMap().entries.forEach((e) {
_cargoTypes[e.key].weight =
double.tryParse(cargoTypeControllers[e.key].text) ?? 0;
});
_mixCargoTypes.asMap().entries.forEach((e) {
_mixCargoTypes[e.key].weight =
double.tryParse(mixCargoTypeControllers[e.key].text) ?? 0;
});
double notMixTotal =
_cargoTypes.fold(0, (sum, value) => sum + value.weight);
double mixTotal =
_mixCargoTypes.fold(0, (sum, value) => sum + value.weight);
double total = notMixTotal + mixTotal;
setState(() {
totalCtl.text = removeTrailingZeros(total);
if (e.value.weight == 0) {
e.value.weight = remainingWeight;
cargoTypeControllers[e.key].text =
removeTrailingZeros(e.value.weight);
}
});
}
}
}
}
if (mounted) {
setState(() {});
}
}
@override
Widget build(BuildContext context) {
@@ -218,35 +201,29 @@ class _CargoWidgetState extends State<CargoWidget> {
color: primaryColor,
),
onPressed: () async {
List<CargoType> allCargoTypes = _cargoTypes + _mixCargoTypes;
CargoType? cargoType = await Navigator.push(
context,
CupertinoPageRoute(
builder: (context) =>
CargoTypeAddition(cargoTypes: allCargoTypes)));
CargoTypeAddition(cargoTypes: _cargoTypes)));
if (cargoType == null) return;
if (cargoType.isMixCargo) {
_mixCargoTypes.add(cargoType);
mixCargoTypeControllers.clear();
for (var e in _mixCargoTypes) {
var editor = TextEditingController();
editor.text = removeTrailingZeros(e.weight);
editor.addListener(inputChangeListener);
mixCargoTypeControllers.add(editor);
}
} else {
if (!cargoType.isMixCargo) {
_cargoTypes.add(cargoType);
cargoTypeControllers.clear();
}
_cargoTypes.sort((a, b) => (a == b ? 0 : (a.isMixCargo ? 1 : -1)));
if (cargoType.isMixCargo) {
_cargoTypes.add(cargoType);
}
cargoTypeControllers.clear();
for (var e in _cargoTypes) {
var editor = TextEditingController();
editor.text = removeTrailingZeros(e.weight);
editor.addListener(inputChangeListener);
cargoTypeControllers.add(editor);
}
}
_calculateTotalWeght();
if (mounted) {
setState(() {});
}
@@ -254,34 +231,34 @@ class _CargoWidgetState extends State<CargoWidget> {
);
final cargosBox = Padding(
padding: const EdgeInsets.only(top: 10),
padding: const EdgeInsets.only(top: 5),
child: Wrap(
alignment: WrapAlignment.spaceBetween,
runSpacing: 15,
runSpacing: 25,
children: _cargoTypes.asMap().entries.map((e) {
var key = e.key;
var c = e.value;
return SizedBox(
width: MediaQuery.of(context).size.width / 2.3,
child: Row(
child: Column(
children: [
Row(
children: [
InkResponse(
radius: 23,
radius: 25,
onTap: () {
_cargoTypes.removeAt(key);
double totalWeight =
double.tryParse(totalCtl.text) ?? 0;
double removeWeight =
(double.tryParse(cargoTypeControllers[key].text) ??
0);
if (totalWeight >= removeWeight) {
double result = totalWeight - removeWeight;
if (actualTotalWeight > totalWeight) {
error = "Exceed total weight";
} else {
double result = totalWeight - c.weight;
totalCtl.text = removeTrailingZeros(result);
}
cargoTypeControllers[key].clear();
_cargoTypes.removeAt(key);
cargoTypeControllers.removeAt(key);
if (mounted) {
setState(() {});
@@ -293,66 +270,36 @@ class _CargoWidgetState extends State<CargoWidget> {
child: inputTextFieldWidget(context,
lableText: c.name ?? "",
controller: cargoTypeControllers[key],
onFieldSubmitted: (value) {
_cargoTypes[e.key].weight =
double.tryParse(value) ?? 0;
onUpdated();
},
suffixIcon: InkResponse(
radius: 23,
onTap: () {},
child: Icon(Ionicons.md_refresh_circle,
color: labelColor))),
),
],
),
);
}).toList()),
);
final mixCargosBox = Wrap(
alignment: WrapAlignment.spaceBetween,
runSpacing: 15,
children: _mixCargoTypes.asMap().entries.map((e) {
var key = e.key;
var c = e.value;
return SizedBox(
width: MediaQuery.of(context).size.width / 2.3,
child: Column(
children: [
Row(
children: [
InkResponse(
radius: 23,
onTap: () {
_mixCargoTypes.removeAt(key);
double totalWeight =
double.tryParse(totalCtl.text) ?? 0;
double removeWeight = (double.tryParse(
mixCargoTypeControllers[key].text) ??
0);
(double.tryParse(totalCtl.text) ?? 0);
if (totalWeight >= removeWeight) {
double result = totalWeight - removeWeight;
totalCtl.text = removeTrailingZeros(result);
}
var list = _cargoTypes
.where((e) => e.id != c.id)
.toList();
double resetValue = totalWeight -
(list.fold(0,
(sum, value) => sum + value.weight));
setState(() {
e.value.weight = resetValue;
cargoTypeControllers[e.key].text =
removeTrailingZeros(resetValue);
});
mixCargoTypeControllers[key].clear();
if (mounted) {
setState(() {});
}
onUpdated();
},
child: Icon(Feather.minus_circle,
color: labelColor, size: 20)),
const SizedBox(width: 10),
Flexible(
child: inputTextFieldWidget(context,
lableText: c.name ?? "",
controller: mixCargoTypeControllers[key],
suffixIcon: InkResponse(
radius: 23,
onTap: () {},
child: Icon(Ionicons.md_refresh_circle,
color: labelColor, size: 22))),
),
InkResponse(
c.isMixCargo
? InkResponse(
radius: 23,
onTap: () async {
List<CargoType>? cargoes = await showDialog(
@@ -364,8 +311,9 @@ class _CargoWidgetState extends State<CargoWidget> {
c.mixCargoes = List.from(cargoes);
});
},
child:
Icon(Icons.add_circle, color: labelColor, size: 22))
child: Icon(Icons.add_circle,
color: labelColor, size: 22))
: const SizedBox()
],
),
c.mixCargoes.isEmpty
@@ -400,7 +348,8 @@ class _CargoWidgetState extends State<CargoWidget> {
],
),
);
}).toList());
}).toList()),
);
final totalWeightBox = Row(
mainAxisAlignment: MainAxisAlignment.end,
@@ -421,33 +370,33 @@ class _CargoWidgetState extends State<CargoWidget> {
Flexible(
child: inputTextFieldWidget(context,
lableText: "Total",
controller: totalCtl,
// onChanged: (neValue) {
// List<int> emptyFields = getEmptyFields();
// if (emptyFields.length == 1) {
// double totalWeight = double.tryParse(neValue) ?? 0;
// _cargoTypes.asMap().entries.forEach((e) {
// _cargoTypes[e.key].weight =
// double.tryParse(cargoTypeControllers[e.key].text) ??
// 0;
// });
// double result = _cargoTypes.fold(
// 0, (sum, value) => sum + value.weight);
// if (totalWeight >= result) {
// double remaining = totalWeight - result;
// setState(() {
// cargoTypeControllers[emptyFields.first].text =
// removeTrailingZeros(remaining);
// });
// }
// }
// },
controller: totalCtl, onFieldSubmitted: (neValue) {
if (hasValueCargoes) {
double totalWeight = double.tryParse(neValue) ?? 0;
if (totalWeight < actualTotalWeight) {
setState(() {
error = "Invalid total weight";
});
} else {
setState(() {
error = null;
});
}
} else {
onUpdated();
}
},
suffixIcon: InkResponse(
radius: 23,
onTap: () {
setState(() {
totalCtl.text =
removeTrailingZeros(actualTotalWeight);
error = null;
});
},
child: Icon(Ionicons.md_refresh_circle,
color: labelColor))),
color: labelColor, size: 22))),
),
],
)),
@@ -485,10 +434,10 @@ class _CargoWidgetState extends State<CargoWidget> {
);
final subChargeItemsBox = Padding(
padding: const EdgeInsets.only(top: 10),
padding: const EdgeInsets.only(top: 5),
child: Wrap(
alignment: WrapAlignment.spaceBetween,
runSpacing: 15,
runSpacing: 25,
children: _surchareItems.asMap().entries.map((e) {
var key = e.key;
var c = e.value;
@@ -532,18 +481,14 @@ class _CargoWidgetState extends State<CargoWidget> {
context, "Error", "Please insert surcharge item quantity");
return;
}
var allCargoes = _cargoTypes + _mixCargoTypes;
widget.onContinue!(allCargoes, _surchareItems);
widget.onContinue!(_cargoTypes, _surchareItems);
}
},
);
final previousBtn = PreviousButton(onTap: () {
if (widget.onPrevious != null) {
var allCargoes = _cargoTypes + _mixCargoTypes;
widget.onPrevious!(allCargoes, _surchareItems);
widget.onPrevious!(_cargoTypes, _surchareItems);
}
});
@@ -558,18 +503,15 @@ class _CargoWidgetState extends State<CargoWidget> {
userRow,
cargoTitle,
cargosBox,
_mixCargoTypes.isNotEmpty && _cargoTypes.isNotEmpty
? Padding(
padding: const EdgeInsets.symmetric(
horizontal: 100, vertical: 25),
child: Divider(color: Colors.grey.shade300),
const SizedBox(height: 15),
Divider(),
const SizedBox(height: 5),
error != null
? Text(
error!,
style: TextStyle(color: dangerColor),
)
: const SizedBox(),
mixCargosBox,
Padding(
padding: const EdgeInsets.symmetric(vertical: 25),
child: Divider(color: Colors.grey.shade300),
),
totalWeightBox,
subchargeItemTitleBox,
subChargeItemsBox,
@@ -599,6 +541,7 @@ class _CargoWidgetState extends State<CargoWidget> {
{required String lableText,
TextEditingController? controller,
Function(String)? onChanged,
Function(String)? onFieldSubmitted,
bool readOnly = false,
Widget? suffixIcon}) {
return TextFormField(
@@ -607,6 +550,7 @@ class _CargoWidgetState extends State<CargoWidget> {
cursorColor: primaryColor,
keyboardType: TextInputType.number,
onChanged: onChanged,
onFieldSubmitted: onFieldSubmitted,
readOnly: readOnly,
decoration: InputDecoration(
suffixIcon: suffixIcon,

View File

@@ -294,7 +294,7 @@ class _CartonInfoState extends State<CartonInfo> {
return Padding(
padding: const EdgeInsets.symmetric(vertical: 2),
child: Container(
color: e.key.isEven ? Colors.grey.shade300 : oddColor,
color: e.key.isEven ? Colors.grey.shade200 : oddColor,
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
@@ -325,7 +325,9 @@ class _CartonInfoState extends State<CartonInfo> {
vertical: 2),
child: Text(
"- ${c.name}",
style: TextStyle(fontSize: 14),
style: TextStyle(
fontSize: 14,
color: labelColor),
),
);
}).toList()),
@@ -357,7 +359,7 @@ class _CartonInfoState extends State<CartonInfo> {
return Padding(
padding: const EdgeInsets.symmetric(vertical: 2),
child: Container(
color: e.key.isEven ? Colors.grey.shade300 : oddColor,
color: e.key.isEven ? Colors.grey.shade200 : oddColor,
child: Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: [

View File

@@ -264,37 +264,120 @@ class CartonSubmit extends StatelessWidget {
children: cargoTypes.map((e) {
return Padding(
padding: const EdgeInsets.symmetric(vertical: 3),
child: Row(
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: [
Text(
e.name ?? "",
style:
TextStyle(color: Colors.black, fontSize: 15),
style: TextStyle(
color: Colors.black, fontSize: 15),
),
Text("${removeTrailingZeros(e.weight)} lb",
style: TextStyle(
color: Colors.black, fontSize: 15))
],
),
e.isMixCargo
? Padding(
padding: const EdgeInsets.only(left: 20),
child: Column(
crossAxisAlignment:
CrossAxisAlignment.start,
children: e.mixCargoes.map((c) {
return Padding(
padding: const EdgeInsets.symmetric(
vertical: 2),
child: Text(
"- ${c.name}",
style: TextStyle(
fontSize: 14,
color: labelColor),
),
);
}).toList()),
const SizedBox(height: 10),
)
: const SizedBox()
],
),
);
}).toList()),
// const SizedBox(height: 10),
// Column(
// crossAxisAlignment: CrossAxisAlignment.start,
// children: surchareItems.map((e) {
// return Padding(
// padding: const EdgeInsets.symmetric(vertical: 3),
// child: Row(
// mainAxisAlignment: MainAxisAlignment.spaceBetween,
// children: [
// Text(
// e.name ?? "",
// style: TextStyle(color: labelColor, fontSize: 15),
// ),
// Text("${numberFormatter.format(e.qty)} pc",
// style:
// TextStyle(color: labelColor, fontSize: 15))
// ],
// ),
// );
// }).toList()),
],
),
),
),
]),
);
final surChargeItemsBox = Padding(
padding: const EdgeInsets.only(top: 10),
child: Column(crossAxisAlignment: CrossAxisAlignment.stretch, children: [
Padding(
padding: const EdgeInsets.only(left: 5, bottom: 5, right: 8),
child: Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: [
LocalText(context, 'box.input_surcharge_item',
color: primaryColor,
fontSize: 16,
fontWeight: FontWeight.normal),
],
),
),
Container(
decoration: BoxDecoration(
border: Border.all(color: primaryColor),
borderRadius: BorderRadius.circular(5),
),
child: Padding(
padding: const EdgeInsets.all(8.0),
child: Column(
children: [
Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: surchareItems.map((e) {
return Padding(
padding: const EdgeInsets.symmetric(vertical: 3),
child: Row(
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: [
Text(
e.name ?? "",
style: TextStyle(color: labelColor, fontSize: 15),
style: TextStyle(
color: Colors.black, fontSize: 15),
),
Text(
"${removeTrailingZeros((e.qty).toDouble())} pc",
textAlign: TextAlign.end,
style: TextStyle(
color: Colors.black, fontSize: 15))
],
),
Text("${numberFormatter.format(e.qty)} pc",
style:
TextStyle(color: labelColor, fontSize: 15))
],
),
);
@@ -368,6 +451,8 @@ class CartonSubmit extends StatelessWidget {
packages.isNotEmpty ? packagesBox : const SizedBox(),
const SizedBox(height: 10),
cargosBox,
const SizedBox(height: 10),
surChargeItemsBox,
const SizedBox(height: 20),
],
),