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

393 lines
12 KiB
Dart
Raw Normal View History

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 'package:provider/provider.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 '../rates/model/shipment_rate_model.dart';
import '../widgets/continue_button.dart';
import '../widgets/display_text.dart';
import '../widgets/local_title.dart';
import '../widgets/previous_button.dart';
import 'custom_duty_addition.dart';
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({
Key? key,
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,
}) : super(key: key);
@override
State<CargoWidget> createState() => _CargoWidgetState();
}
class _CargoWidgetState extends State<CargoWidget> {
List<CargoType> _cargoTypes = [];
2024-02-06 17:45:36 +06:30
List<CargoType> _surchareItems = [];
2024-02-05 17:49:12 +06:30
TextEditingController _totalCtl = TextEditingController();
List<TextEditingController> _cargoTypeControllers = [];
2024-02-06 17:45:36 +06:30
List<TextEditingController> _surchargeControllers = [];
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
var model = context.read<ShipmentRateModel>();
_cargoTypes = model.rate.cargoTypes.map((e) => e.clone()).toList();
if (widget.cargoTypes.isNotEmpty) {
2024-02-06 17:45:36 +06:30
_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);
2024-02-05 17:49:12 +06:30
} else {
_cargoTypes.forEach((e) {
var editor = new TextEditingController();
editor.text = '';
editor.addListener(inputChangeListener);
_cargoTypeControllers.add(editor);
});
}
2024-02-06 17:45:36 +06:30
//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);
});
}
2024-02-05 17:49:12 +06:30
if (mounted) {
setState(() {});
}
}
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() {
List<int> emptyFields = getEmptyFields();
if (emptyFields.isEmpty) {
_cargoTypes.asMap().entries.forEach((e) {
_cargoTypes[e.key].weight =
double.tryParse(_cargoTypeControllers[e.key].text) ?? 0;
});
double total = _cargoTypes.fold(0, (sum, value) => sum + value.weight);
setState(() {
2024-02-06 17:45:36 +06:30
_totalCtl.text = removeTrailingZeros(total);
2024-02-05 17:49:12 +06:30
});
}
}
@override
Widget build(BuildContext context) {
final senderBox = DisplayText(
text: widget.sender.name,
labelTextKey: "box.sender.title",
iconData: MaterialCommunityIcons.account_arrow_right,
subText: Text(widget.sender.fcsID!,
style: TextStyle(fontSize: 13, color: labelColor)),
);
final consigneeBox = DisplayText(
text: widget.consignee.name,
labelTextKey: "box.consignee.title",
iconData: MaterialCommunityIcons.account_arrow_left,
subText: Text(widget.consignee.fcsID!,
style: TextStyle(fontSize: 13, color: labelColor)),
);
final userRow = Row(
children: [
2024-02-06 17:45:36 +06:30
Expanded(child: senderBox, flex: 2),
2024-02-05 17:49:12 +06:30
Flexible(child: consigneeBox)
],
);
final cargosBox = Wrap(
alignment: WrapAlignment.spaceBetween,
runSpacing: 15,
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(
children: [
InkResponse(
radius: 25,
onTap: () {
2024-02-06 17:45:36 +06:30
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(() {});
}
2024-02-05 17:49:12 +06:30
},
2024-02-06 17:45:36 +06:30
child: Icon(MaterialIcons.clear, color: labelColor)),
2024-02-05 17:49:12 +06:30
const SizedBox(width: 10),
Flexible(
child: inputTextFieldWidget(context,
lableText: c.name ?? "",
2024-02-06 17:45:36 +06:30
controller: _cargoTypeControllers[key]),
2024-02-05 17:49:12 +06:30
),
],
),
);
}).toList());
2024-02-06 17:45:36 +06:30
final totalWeightBox = Row(
2024-02-05 17:49:12 +06:30
mainAxisAlignment: MainAxisAlignment.end,
children: [
SizedBox(
width: MediaQuery.of(context).size.width / 2.3,
child: Row(
children: [
InkResponse(
radius: 25,
onTap: () {
setState(() {
_totalCtl.clear();
});
},
2024-02-06 17:45:36 +06:30
child: Icon(MaterialIcons.clear, color: labelColor)),
2024-02-05 17:49:12 +06:30
const SizedBox(width: 10),
Flexible(
child: inputTextFieldWidget(context,
2024-02-06 17:45:36 +06:30
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);
});
}
}
}),
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 {
List<CargoType>? customList = await Navigator.push<List<CargoType>>(
context,
CupertinoPageRoute(
builder: (context) =>
2024-02-06 17:45:36 +06:30
CustomDutyAddition(customDuties: _surchareItems)));
2024-02-05 17:49:12 +06:30
if (customList == null) return;
2024-02-06 17:45:36 +06:30
_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);
});
2024-02-05 17:49:12 +06:30
if (mounted) {
setState(() {});
}
}),
);
final subChargeItemsBox = Wrap(
alignment: WrapAlignment.spaceBetween,
runSpacing: 15,
2024-02-06 17:45:36 +06:30
children: _surchareItems.asMap().entries.map((e) {
2024-02-05 17:49:12 +06:30
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(() {
2024-02-06 17:45:36 +06:30
_surchareItems.removeAt(key);
2024-02-05 17:49:12 +06:30
});
},
2024-02-06 17:45:36 +06:30
child: Icon(Feather.minus_circle, color: labelColor)),
2024-02-05 17:49:12 +06:30
const SizedBox(width: 10),
Flexible(
child: inputTextFieldWidget(
context,
lableText: c.name ?? "",
2024-02-06 17:45:36 +06:30
controller: _surchargeControllers[key],
onChanged: (newValue) {
setState(() {
_surchareItems[key].qty = int.tryParse(newValue) ?? 0;
});
},
2024-02-05 17:49:12 +06:30
),
),
],
),
);
}).toList());
final continueBtn = ContinueButton(
onTap: () {
2024-02-06 17:45:36 +06:30
if (widget.onContinue != null) {
widget.onContinue!(_cargoTypes, _surchareItems);
}
2024-02-05 17:49:12 +06:30
},
);
final previousBtn = PreviousButton(onTap: () {
if (widget.onPrevious != null) {
2024-02-06 17:45:36 +06:30
widget.onPrevious!(_cargoTypes, _surchareItems);
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,
LocalTitle(textKey: "box.input_cargo_weight", topPadding: 10),
cargosBox,
const SizedBox(height: 15),
Divider(),
const SizedBox(height: 5),
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,
bool readOnly = false}) {
2024-02-05 17:49:12 +06:30
return TextFormField(
controller: controller,
style: textStyle,
cursorColor: primaryColor,
keyboardType: TextInputType.number,
onChanged: onChanged,
2024-02-06 17:45:36 +06:30
readOnly: readOnly,
2024-02-05 17:49:12 +06:30
decoration: new InputDecoration(
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)),
),
);
}
}