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

626 lines
21 KiB
Dart
Raw Normal View History

// ignore_for_file: unused_local_variable
2024-02-05 17:49:12 +06:30
import 'package:fcs/helpers/theme.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_icons_null_safety/flutter_icons_null_safety.dart';
import '../../domain/entities/cargo_type.dart';
import '../../domain/entities/user.dart';
2024-02-06 17:45:36 +06:30
import '../main/util.dart';
2024-02-05 17:49:12 +06:30
import '../widgets/continue_button.dart';
import '../widgets/local_title.dart';
import '../widgets/previous_button.dart';
import 'cargo_type_addition.dart';
import 'cargo_type_addition_dialog.dart';
import 'mix_cargo_type_addition_dialog.dart';
import 'surcharge_item_addition.dart';
2024-02-05 17:49:12 +06:30
typedef OnPrevious = Function(
List<CargoType> cargoTypes, List<CargoType> customDuties);
typedef OnContinue = Function(
List<CargoType> cargoTypes, List<CargoType> customDuties);
class CargoWidget extends StatefulWidget {
final User sender;
final User consignee;
final List<CargoType> cargoTypes;
2024-02-06 17:45:36 +06:30
final List<CargoType> surchargeItems;
2024-02-05 17:49:12 +06:30
final OnPrevious? onPrevious;
final OnContinue? onContinue;
const CargoWidget({
2025-03-07 17:41:09 +06:30
super.key,
2024-02-05 17:49:12 +06:30
required this.cargoTypes,
2024-02-06 17:45:36 +06:30
required this.surchargeItems,
2024-02-05 17:49:12 +06:30
this.onPrevious,
this.onContinue,
required this.sender,
required this.consignee,
2025-03-07 17:41:09 +06:30
});
2024-02-05 17:49:12 +06:30
@override
State<CargoWidget> createState() => _CargoWidgetState();
}
class _CargoWidgetState extends State<CargoWidget> {
List<CargoType> _cargoTypes = [];
2024-02-06 17:45:36 +06:30
List<CargoType> _surchareItems = [];
2025-03-07 17:41:09 +06:30
TextEditingController totalCtl = TextEditingController();
2025-03-24 18:23:17 +06:30
2025-03-07 17:41:09 +06:30
List<TextEditingController> cargoTypeControllers = [];
List<TextEditingController> surchargeControllers = [];
2024-02-05 17:49:12 +06:30
2025-03-24 18:23:17 +06:30
bool get hasValueTotalWeight =>
totalCtl.text.isNotEmpty && totalCtl.text != '0.00';
2025-03-24 18:23:17 +06:30
bool get hasValueCargoes =>
_cargoTypes.isNotEmpty && _cargoTypes.every((e) => e.weight != 0.00);
2025-03-24 18:23:17 +06:30
double get actualTotalWeight =>
_cargoTypes.fold(0, (sum, value) => sum + value.weight);
String? error;
2024-02-05 17:49:12 +06:30
@override
void initState() {
_init();
super.initState();
}
_init() {
2024-02-06 17:45:36 +06:30
// for cargo types
2024-02-05 17:49:12 +06:30
if (widget.cargoTypes.isNotEmpty) {
2025-03-24 18:23:17 +06:30
_cargoTypes = List.from(widget.cargoTypes);
2025-03-07 17:41:09 +06:30
for (var e in _cargoTypes) {
var editor = TextEditingController();
editor.text = twoDecimalFormatted(
double.tryParse(removeTrailingZeros(e.weight)) ?? 0);
2024-02-06 17:45:36 +06:30
editor.addListener(inputChangeListener);
2025-03-07 17:41:09 +06:30
cargoTypeControllers.add(editor);
}
_onPopulate();
} else {
WidgetsBinding.instance.addPostFrameCallback((_) {
_openCargoTypeSelection();
});
2024-02-05 17:49:12 +06:30
}
2024-02-06 17:45:36 +06:30
//for surcharge items
if (widget.surchargeItems.isNotEmpty) {
_surchareItems = List.from(widget.surchargeItems);
2025-03-07 17:41:09 +06:30
for (var e in _surchareItems) {
var editor = TextEditingController();
2024-02-06 17:45:36 +06:30
editor.text = e.qty.toString();
2025-03-24 18:23:17 +06:30
editor.addListener(inputChangeListener);
2025-03-07 17:41:09 +06:30
surchargeControllers.add(editor);
}
2024-02-06 17:45:36 +06:30
}
2024-02-05 17:49:12 +06:30
if (mounted) {
setState(() {});
}
}
_openCargoTypeSelection() async {
List<CargoType>? cargoes = await showDialog(
context: context, builder: (_) => const CargoTypeAdditionDialog());
if (cargoes == null) return;
2025-03-24 18:23:17 +06:30
_cargoTypes = cargoes;
_cargoTypes.sort((a, b) => (a == b ? 0 : (a.isMixCargo ? 1 : -1)));
for (var e in _cargoTypes) {
var editor = TextEditingController();
editor.text = '0.00';
editor.addListener(inputChangeListener);
cargoTypeControllers.add(editor);
2024-02-05 17:49:12 +06:30
}
totalCtl.text = twoDecimalFormatted(
double.tryParse(removeTrailingZeros(actualTotalWeight)) ?? 0);
if (mounted) {
setState(() {});
}
}
2024-02-05 17:49:12 +06:30
2025-03-24 18:23:17 +06:30
inputChangeListener() {
setState(() {});
2024-02-05 17:49:12 +06:30
}
2025-03-24 18:23:17 +06:30
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 _onPopulate() {
2025-03-24 18:23:17 +06:30
if (!hasValueTotalWeight && hasValueCargoes) {
totalCtl.text = twoDecimalFormatted(
double.tryParse(removeTrailingZeros(actualTotalWeight)) ?? 0);
2025-03-24 18:23:17 +06:30
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) {
if (e.value.weight == 0) {
e.value.weight = remainingWeight;
cargoTypeControllers[e.key].text = twoDecimalFormatted(
double.tryParse(removeTrailingZeros(e.value.weight)) ?? 0);
2025-03-24 18:23:17 +06:30
}
});
}
}
}
}
if (mounted) {
setState(() {});
}
}
void _onCheckTotalWeight(String value) {
double totalWeight = double.tryParse(value) ?? 0;
if (totalWeight != actualTotalWeight) {
error = "Invalid total weight";
} else {
error = null;
}
if (mounted) {
setState(() {});
}
}
2024-02-05 17:49:12 +06:30
@override
Widget build(BuildContext context) {
2025-03-07 17:41:09 +06:30
final senderBox = userDisplayBox(context,
lableKey: "box.sender.title",
icon: MaterialCommunityIcons.account_arrow_right,
showLink: false,
name: widget.sender.name ?? "",
fcsID: widget.sender.fcsID ?? "");
final consigneeBox = userDisplayBox(context,
showLink: false,
lableKey: "box.consignee.title",
icon: MaterialCommunityIcons.account_arrow_left,
name: widget.consignee.name,
fcsID: widget.consignee.fcsID);
2024-02-05 17:49:12 +06:30
final userRow = Row(
crossAxisAlignment: CrossAxisAlignment.start,
2024-02-05 17:49:12 +06:30
children: [
2025-03-07 17:41:09 +06:30
Expanded(flex: 2, child: consigneeBox),
Flexible(child: senderBox)
2024-02-05 17:49:12 +06:30
],
);
final cargoTitle = LocalTitle(
textKey: "box.input_cargo_weight",
topPadding: 0,
trailing: IconButton(
icon: Icon(
Icons.add_circle,
color: primaryColor,
),
onPressed: () async {
CargoType? cargoType = await Navigator.push(
context,
CupertinoPageRoute(
builder: (context) =>
2025-03-24 18:23:17 +06:30
CargoTypeAddition(cargoTypes: _cargoTypes)));
if (cargoType == null) return;
2025-03-24 18:23:17 +06:30
// add cargo type
if (cargoType.isMixCargo) {
int lastTrueIndex =
_cargoTypes.lastIndexWhere((e) => e.isMixCargo);
if (lastTrueIndex != -1) {
_cargoTypes.insert(lastTrueIndex + 1, cargoType);
} else {
_cargoTypes.add(cargoType);
}
} else {
int lastFalseIndex =
_cargoTypes.lastIndexWhere((e) => !e.isMixCargo);
if (lastFalseIndex != -1) {
_cargoTypes.insert(lastFalseIndex + 1, cargoType);
} else {
_cargoTypes.insert(0, cargoType);
}
2025-03-24 18:23:17 +06:30
}
2025-03-24 18:23:17 +06:30
cargoTypeControllers.clear();
for (var e in _cargoTypes) {
var editor = TextEditingController();
editor.text = twoDecimalFormatted(
double.tryParse(removeTrailingZeros(e.weight)) ?? 0);
2025-03-24 18:23:17 +06:30
editor.addListener(inputChangeListener);
cargoTypeControllers.add(editor);
2025-03-07 17:41:09 +06:30
}
if (mounted) {
setState(() {});
}
}),
);
final totalWeightBox = Padding(
2025-03-24 18:23:17 +06:30
padding: const EdgeInsets.only(top: 5),
child: Row(
mainAxisAlignment: MainAxisAlignment.end,
children: [
SizedBox(
width: MediaQuery.of(context).size.width / 2.3,
child: Row(
children: [
InkResponse(
radius: 25,
onTap: () {
2025-03-24 18:23:17 +06:30
setState(() {
totalCtl.clear();
2025-03-24 18:23:17 +06:30
});
_onCheckTotalWeight(totalCtl.text);
},
child: Icon(MaterialIcons.clear, color: labelColor)),
const SizedBox(width: 10),
Flexible(
child: inputTextFieldWidget(context,
lableText: "Total",
controller: totalCtl, onFieldSubmitted: (newValue) {
if (hasValueCargoes) {
_onCheckTotalWeight(newValue);
2025-03-24 18:23:17 +06:30
} else {
_onPopulate();
2025-03-24 18:23:17 +06:30
}
},
suffixIcon: InkResponse(
radius: 23,
onTap: () {
setState(() {
totalCtl.text = twoDecimalFormatted(
double.tryParse(removeTrailingZeros(
actualTotalWeight)) ??
0);
error = null;
});
},
child: Icon(Ionicons.md_refresh_circle,
color: labelColor, size: 22))),
),
],
)),
],
),
2024-02-05 17:49:12 +06:30
);
final subchargeItemTitleBox = LocalTitle(
textKey: "box.input_surcharge_item",
trailing: IconButton(
icon: Icon(
Icons.add_circle,
color: primaryColor,
),
onPressed: () async {
CargoType? surchargeItem = await Navigator.push(
2024-02-05 17:49:12 +06:30
context,
CupertinoPageRoute(
builder: (context) =>
SurchargeItemAddition(items: _surchareItems)));
if (surchargeItem == null) return;
2024-02-05 17:49:12 +06:30
_surchareItems.add(surchargeItem);
2024-02-06 17:45:36 +06:30
2025-03-07 17:41:09 +06:30
surchargeControllers.clear();
2024-02-06 17:45:36 +06:30
_surchareItems.asMap().entries.forEach((e) {
2025-03-07 17:41:09 +06:30
var editor = TextEditingController();
editor.text = e.value.qty.toString();
2025-03-07 17:41:09 +06:30
surchargeControllers.add(editor);
2024-02-06 17:45:36 +06:30
});
2024-02-05 17:49:12 +06:30
if (mounted) {
setState(() {});
}
}),
);
final subChargeItemsBox = Padding(
2025-03-24 18:23:17 +06:30
padding: const EdgeInsets.only(top: 5),
child: Wrap(
alignment: WrapAlignment.spaceBetween,
2025-03-24 18:23:17 +06:30
runSpacing: 25,
children: _surchareItems.asMap().entries.map((e) {
var key = e.key;
var c = e.value;
return SizedBox(
width: MediaQuery.of(context).size.width / 2.3,
child: Row(
children: [
InkResponse(
radius: 25,
onTap: () {
setState(() {
_surchareItems.removeAt(key);
});
},
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;
});
},
),
2024-02-05 17:49:12 +06:30
),
],
),
);
}).toList()),
);
2024-02-05 17:49:12 +06:30
final continueBtn = ContinueButton(
onTap: () {
2024-02-06 17:45:36 +06:30
if (widget.onContinue != null) {
if (_surchareItems.isNotEmpty &&
_surchareItems.any((item) => item.qty == 0)) {
showMsgDialog(
context, "Error", "Please insert surcharge item quantity");
return;
}
if (error != null) {
showMsgDialog(
context, "Error", "Please add the right cargo type weight");
return;
}
2025-03-24 18:23:17 +06:30
widget.onContinue!(_cargoTypes, _surchareItems);
2024-02-06 17:45:36 +06:30
}
2024-02-05 17:49:12 +06:30
},
);
final previousBtn = PreviousButton(onTap: () {
if (error != null) {
showMsgDialog(
context, "Error", "Please add the right cargo type weight");
return;
}
2024-02-05 17:49:12 +06:30
if (widget.onPrevious != null) {
2025-03-24 18:23:17 +06:30
widget.onPrevious!(_cargoTypes, _surchareItems);
2024-02-05 17:49:12 +06:30
}
});
Widget cargoesWidget(List<CargoType> items) {
List<Widget> widgets = [];
for (int i = 0; i < items.length; i++) {
var key = i;
var c = items[i];
if (i > 0 && (!items[i - 1].isMixCargo && items[i].isMixCargo)) {
widgets.add(Padding(
padding: const EdgeInsets.symmetric(horizontal: 70),
child: Divider(color: Colors.grey.shade300)));
}
widgets.add(SizedBox(
width: MediaQuery.of(context).size.width / 2.3,
child: Column(
children: [
Row(
children: [
InkResponse(
radius: 25,
onTap: () {
_cargoTypes.removeAt(key);
cargoTypeControllers.removeAt(key);
_onCheckTotalWeight(totalCtl.text);
if (mounted) {
setState(() {});
}
},
child: Icon(Feather.minus_circle, color: labelColor)),
const SizedBox(width: 10),
Flexible(
child: inputTextFieldWidget(context,
lableText: c.name ?? "",
controller: cargoTypeControllers[key],
onFieldSubmitted: (value) {
_cargoTypes[key].weight = double.tryParse(value) ?? 0;
_onPopulate();
},
suffixIcon: InkResponse(
radius: 23,
onTap: () {
double totalWeight =
(double.tryParse(totalCtl.text) ?? 0);
var list = _cargoTypes
.where((e) => e.id != c.id)
.toList();
double sum = (list.fold(
0, (sum, value) => sum + value.weight));
if (sum > totalWeight) {
error = "Exceed total weight";
} else {
error = null;
double resetValue = totalWeight - sum;
setState(() {
c.weight = resetValue;
cargoTypeControllers[key].text =
twoDecimalFormatted(double.tryParse(
removeTrailingZeros(
resetValue)) ??
0);
});
_onPopulate();
}
},
child: Icon(Ionicons.md_refresh_circle,
color: labelColor, size: 22))),
),
c.isMixCargo
? InkResponse(
radius: 23,
onTap: () async {
List<CargoType>? cargoes = await showDialog(
context: context,
builder: (_) => MixCargoTypeAdditionDialog(
cargoTypes: c.mixCargoes));
if (cargoes == null) return;
setState(() {
c.mixCargoes = List.from(cargoes);
});
},
child: Icon(Icons.add_circle,
color: labelColor, size: 22))
: const SizedBox()
],
),
c.mixCargoes.isEmpty
? const SizedBox()
: Padding(
padding: const EdgeInsets.only(top: 5),
child: Column(
children: c.mixCargoes.map((e) {
return Padding(
padding: const EdgeInsets.only(top: 12),
child: Row(
children: [
const SizedBox(width: 25),
InkResponse(
radius: 23,
onTap: () {
setState(() {
c.mixCargoes.remove(e);
});
},
child: Icon(Feather.minus_circle,
color: labelColor, size: 20)),
Padding(
padding: const EdgeInsets.only(left: 10),
child: Text(e.name ?? ""),
),
],
),
);
}).toList()),
)
],
),
));
}
return Padding(
padding: const EdgeInsets.only(top: 5),
child: Wrap(
alignment: WrapAlignment.spaceBetween,
runSpacing: 25,
children: widgets),
);
}
2024-02-05 17:49:12 +06:30
return Column(
children: [
Expanded(
child: Padding(
padding: EdgeInsets.only(left: 10, right: 10),
child: ListView(
children: [
const SizedBox(height: 8),
userRow,
cargoTitle,
cargoesWidget(_cargoTypes),
2025-03-24 18:23:17 +06:30
const SizedBox(height: 15),
_cargoTypes.isNotEmpty
? Divider(
color: Colors.grey.shade300,
)
: const SizedBox(),
2025-03-24 18:23:17 +06:30
const SizedBox(height: 5),
error != null
? Text(
error!,
style: TextStyle(color: dangerColor),
)
: const SizedBox(),
2024-02-06 17:45:36 +06:30
totalWeightBox,
2024-02-05 17:49:12 +06:30
subchargeItemTitleBox,
subChargeItemsBox,
const SizedBox(height: 30),
],
),
),
),
widget.onContinue != null
? Padding(
padding: const EdgeInsets.only(left: 15, right: 15, top: 10),
child: Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: [
previousBtn,
continueBtn,
],
),
)
: const SizedBox(),
const SizedBox(height: 20)
],
);
}
Widget inputTextFieldWidget(BuildContext context,
{required String lableText,
TextEditingController? controller,
2024-02-06 17:45:36 +06:30
Function(String)? onChanged,
2025-03-24 18:23:17 +06:30
Function(String)? onFieldSubmitted,
bool readOnly = false,
Widget? suffixIcon}) {
2024-02-05 17:49:12 +06:30
return TextFormField(
controller: controller,
style: textStyle,
cursorColor: primaryColor,
keyboardType: TextInputType.number,
onChanged: onChanged,
2025-03-24 18:23:17 +06:30
onFieldSubmitted: onFieldSubmitted,
2024-02-06 17:45:36 +06:30
readOnly: readOnly,
2025-03-07 17:41:09 +06:30
decoration: InputDecoration(
suffixIcon: Padding(
padding: const EdgeInsets.only(right: 8),
child: suffixIcon,
),
suffixIconConstraints: BoxConstraints(minWidth: 0, minHeight: 0),
2024-02-05 17:49:12 +06:30
contentPadding: EdgeInsets.all(0),
labelText: lableText,
labelStyle: newLabelStyle(color: Colors.black54, fontSize: 17),
enabledBorder: UnderlineInputBorder(
borderSide: BorderSide(color: primaryColor, width: 1.0)),
focusedBorder: UnderlineInputBorder(
borderSide: BorderSide(color: primaryColor, width: 1.0)),
disabledBorder: UnderlineInputBorder(
borderSide: BorderSide(color: primaryColor, width: 1.0)),
),
);
}
}