update cartion editor for package

This commit is contained in:
tzw
2024-02-06 17:45:36 +06:30
parent 3767ba76dc
commit 3a91b49a1a
10 changed files with 563 additions and 109 deletions

View File

@@ -6,6 +6,7 @@ import 'package:provider/provider.dart';
import '../../domain/entities/cargo_type.dart';
import '../../domain/entities/user.dart';
import '../main/util.dart';
import '../rates/model/shipment_rate_model.dart';
import '../widgets/continue_button.dart';
import '../widgets/display_text.dart';
@@ -22,14 +23,14 @@ class CargoWidget extends StatefulWidget {
final User sender;
final User consignee;
final List<CargoType> cargoTypes;
final List<CargoType> customDuties;
final List<CargoType> surchargeItems;
final OnPrevious? onPrevious;
final OnContinue? onContinue;
const CargoWidget({
Key? key,
required this.cargoTypes,
required this.customDuties,
required this.surchargeItems,
this.onPrevious,
this.onContinue,
required this.sender,
@@ -42,9 +43,10 @@ class CargoWidget extends StatefulWidget {
class _CargoWidgetState extends State<CargoWidget> {
List<CargoType> _cargoTypes = [];
List<CargoType> _customDuties = [];
List<CargoType> _surchareItems = [];
TextEditingController _totalCtl = TextEditingController();
List<TextEditingController> _cargoTypeControllers = [];
List<TextEditingController> _surchargeControllers = [];
@override
void initState() {
@@ -53,10 +55,28 @@ class _CargoWidgetState extends State<CargoWidget> {
}
_init() {
// for cargo types
var model = context.read<ShipmentRateModel>();
_cargoTypes = model.rate.cargoTypes.map((e) => e.clone()).toList();
if (widget.cargoTypes.isNotEmpty) {
_cargoTypes.forEach((mp) {
mp.weight = 0;
widget.cargoTypes.forEach((vp) {
if (mp.id == vp.id) {
mp.weight = vp.weight;
}
});
});
_cargoTypes.forEach((e) {
var editor = new TextEditingController();
editor.text = removeTrailingZeros(e.weight);
editor.addListener(inputChangeListener);
_cargoTypeControllers.add(editor);
});
double total = _cargoTypes.fold(0, (sum, value) => sum + value.weight);
_totalCtl.text = removeTrailingZeros(total);
} else {
_cargoTypes.forEach((e) {
var editor = new TextEditingController();
@@ -66,6 +86,18 @@ class _CargoWidgetState extends State<CargoWidget> {
});
}
//for surcharge items
if (widget.surchargeItems.isNotEmpty) {
_surchareItems = List.from(widget.surchargeItems);
_surchareItems.forEach((e) {
var editor = new TextEditingController();
editor.text = e.qty.toString();
editor.addListener(inputChangeListener);
_surchargeControllers.add(editor);
});
}
if (mounted) {
setState(() {});
}
@@ -87,11 +119,6 @@ class _CargoWidgetState extends State<CargoWidget> {
inputChangeListener() {
List<int> emptyFields = getEmptyFields();
print("emptyFields:$emptyFields");
// if (emptyFields.isNotEmpty && emptyFields.length == 1) {
// // _cargoTypeControllers[emptyFields.first].text =
// }
if (emptyFields.isEmpty) {
_cargoTypes.asMap().entries.forEach((e) {
@@ -100,29 +127,8 @@ class _CargoWidgetState extends State<CargoWidget> {
});
double total = _cargoTypes.fold(0, (sum, value) => sum + value.weight);
setState(() {
_totalCtl.text = total.toString();
_totalCtl.text = removeTrailingZeros(total);
});
} else {
// if (emptyFields.length == 1) {
// print("_totalCtl.text:${_totalCtl.text}");
// if (_totalCtl.text.isNotEmpty) {
// double t = double.tryParse(_totalCtl.text) ?? 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);
// double remaining = t - result;
// setState(() {
// _cargoTypeControllers[emptyFields.first].text =
// remaining.toString();
// });
// }
// }
}
}
@@ -146,10 +152,7 @@ class _CargoWidgetState extends State<CargoWidget> {
final userRow = Row(
children: [
Expanded(
child: senderBox,
flex: 2,
),
Expanded(child: senderBox, flex: 2),
Flexible(child: consigneeBox)
],
);
@@ -167,34 +170,34 @@ class _CargoWidgetState extends State<CargoWidget> {
InkResponse(
radius: 25,
onTap: () {
setState(() {
_cargoTypeControllers[key].clear();
});
double totalWeight = double.tryParse(_totalCtl.text) ?? 0;
double removeWeight =
(double.tryParse(_cargoTypeControllers[key].text) ??
0);
if (totalWeight >= removeWeight) {
double result = totalWeight - removeWeight;
_totalCtl.text = removeTrailingZeros(result);
}
_cargoTypeControllers[key].clear();
if (mounted) {
setState(() {});
}
},
child: Icon(MaterialIcons.clear)),
child: Icon(MaterialIcons.clear, color: labelColor)),
const SizedBox(width: 10),
Flexible(
child: inputTextFieldWidget(context,
lableText: c.name ?? "",
controller: _cargoTypeControllers[key]
// onChanged: (newValue) {
// setState(() {
// _cargoTypes[key].weight = double.tryParse(newValue) ?? 0;
// });
// double total =
// _cargoTypes.fold(0, (sum, value) => sum + value.weight);
// setState(() {
// _totalCtl.text = total.toString();
// });
// },
),
controller: _cargoTypeControllers[key]),
),
],
),
);
}).toList());
final totalBox = Row(
final totalWeightBox = Row(
mainAxisAlignment: MainAxisAlignment.end,
children: [
SizedBox(
@@ -208,11 +211,34 @@ class _CargoWidgetState extends State<CargoWidget> {
_totalCtl.clear();
});
},
child: Icon(MaterialIcons.clear)),
child: Icon(MaterialIcons.clear, color: labelColor)),
const SizedBox(width: 10),
Flexible(
child: inputTextFieldWidget(context,
lableText: "Total", controller: _totalCtl),
lableText: "Total",
controller: _totalCtl,
readOnly: getEmptyFields().isEmpty, 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);
});
}
}
}),
),
],
)),
@@ -231,10 +257,17 @@ class _CargoWidgetState extends State<CargoWidget> {
context,
CupertinoPageRoute(
builder: (context) =>
CustomDutyAddition(customDuties: _customDuties)));
CustomDutyAddition(customDuties: _surchareItems)));
if (customList == null) return;
_customDuties = List.from(customList);
_surchareItems = List.from(customList);
_surchargeControllers.clear();
_surchareItems.asMap().entries.forEach((e) {
var editor = new TextEditingController();
editor.text = e.value.qty == 0 ? "" : e.value.qty.toString();
_surchargeControllers.add(editor);
});
if (mounted) {
setState(() {});
@@ -245,7 +278,7 @@ class _CargoWidgetState extends State<CargoWidget> {
final subChargeItemsBox = Wrap(
alignment: WrapAlignment.spaceBetween,
runSpacing: 15,
children: _customDuties.asMap().entries.map((e) {
children: _surchareItems.asMap().entries.map((e) {
var key = e.key;
var c = e.value;
return SizedBox(
@@ -256,15 +289,21 @@ class _CargoWidgetState extends State<CargoWidget> {
radius: 25,
onTap: () {
setState(() {
_customDuties.removeAt(key);
_surchareItems.removeAt(key);
});
},
child: Icon(Feather.minus_circle)),
child: Icon(Feather.minus_circle, color: labelColor)),
const SizedBox(width: 10),
Flexible(
child: inputTextFieldWidget(
context,
lableText: c.name ?? "",
controller: _surchargeControllers[key],
onChanged: (newValue) {
setState(() {
_surchareItems[key].qty = int.tryParse(newValue) ?? 0;
});
},
),
),
],
@@ -274,20 +313,15 @@ class _CargoWidgetState extends State<CargoWidget> {
final continueBtn = ContinueButton(
onTap: () {
// if (selectedPackageList.isEmpty || searchResults.isEmpty) {
// showMsgDialog(context, 'Error', "Please select the packages");
// return false;
// }
// if (widget.onContinue != null) {
// widget.onContinue!(selectedPackageList);
// }
if (widget.onContinue != null) {
widget.onContinue!(_cargoTypes, _surchareItems);
}
},
);
final previousBtn = PreviousButton(onTap: () {
if (widget.onPrevious != null) {
widget.onPrevious!(_cargoTypes, _customDuties);
widget.onPrevious!(_cargoTypes, _surchareItems);
}
});
@@ -305,7 +339,7 @@ class _CargoWidgetState extends State<CargoWidget> {
const SizedBox(height: 15),
Divider(),
const SizedBox(height: 5),
totalBox,
totalWeightBox,
subchargeItemTitleBox,
subChargeItemsBox,
const SizedBox(height: 30),
@@ -333,13 +367,15 @@ class _CargoWidgetState extends State<CargoWidget> {
Widget inputTextFieldWidget(BuildContext context,
{required String lableText,
TextEditingController? controller,
Function(String)? onChanged}) {
Function(String)? onChanged,
bool readOnly = false}) {
return TextFormField(
controller: controller,
style: textStyle,
cursorColor: primaryColor,
keyboardType: TextInputType.number,
onChanged: onChanged,
readOnly: readOnly,
decoration: new InputDecoration(
contentPadding: EdgeInsets.all(0),
labelText: lableText,