357 lines
11 KiB
Dart
357 lines
11 KiB
Dart
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';
|
|
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;
|
|
final List<CargoType> customDuties;
|
|
final OnPrevious? onPrevious;
|
|
final OnContinue? onContinue;
|
|
|
|
const CargoWidget({
|
|
Key? key,
|
|
required this.cargoTypes,
|
|
required this.customDuties,
|
|
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 = [];
|
|
List<CargoType> _customDuties = [];
|
|
TextEditingController _totalCtl = TextEditingController();
|
|
List<TextEditingController> _cargoTypeControllers = [];
|
|
|
|
@override
|
|
void initState() {
|
|
_init();
|
|
super.initState();
|
|
}
|
|
|
|
_init() {
|
|
var model = context.read<ShipmentRateModel>();
|
|
_cargoTypes = model.rate.cargoTypes.map((e) => e.clone()).toList();
|
|
|
|
if (widget.cargoTypes.isNotEmpty) {
|
|
} else {
|
|
_cargoTypes.forEach((e) {
|
|
var editor = new TextEditingController();
|
|
editor.text = '';
|
|
editor.addListener(inputChangeListener);
|
|
_cargoTypeControllers.add(editor);
|
|
});
|
|
}
|
|
|
|
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();
|
|
print("emptyFields:$emptyFields");
|
|
|
|
// if (emptyFields.isNotEmpty && emptyFields.length == 1) {
|
|
// // _cargoTypeControllers[emptyFields.first].text =
|
|
// }
|
|
|
|
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(() {
|
|
_totalCtl.text = total.toString();
|
|
});
|
|
} 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();
|
|
// });
|
|
// }
|
|
// }
|
|
}
|
|
}
|
|
|
|
@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: [
|
|
Expanded(
|
|
child: senderBox,
|
|
flex: 2,
|
|
),
|
|
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: () {
|
|
setState(() {
|
|
_cargoTypeControllers[key].clear();
|
|
});
|
|
},
|
|
child: Icon(MaterialIcons.clear)),
|
|
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();
|
|
// });
|
|
// },
|
|
),
|
|
),
|
|
],
|
|
),
|
|
);
|
|
}).toList());
|
|
|
|
final totalBox = Row(
|
|
mainAxisAlignment: MainAxisAlignment.end,
|
|
children: [
|
|
SizedBox(
|
|
width: MediaQuery.of(context).size.width / 2.3,
|
|
child: Row(
|
|
children: [
|
|
InkResponse(
|
|
radius: 25,
|
|
onTap: () {
|
|
setState(() {
|
|
_totalCtl.clear();
|
|
});
|
|
},
|
|
child: Icon(MaterialIcons.clear)),
|
|
const SizedBox(width: 10),
|
|
Flexible(
|
|
child: inputTextFieldWidget(context,
|
|
lableText: "Total", controller: _totalCtl),
|
|
),
|
|
],
|
|
)),
|
|
],
|
|
);
|
|
|
|
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) =>
|
|
CustomDutyAddition(customDuties: _customDuties)));
|
|
if (customList == null) return;
|
|
|
|
_customDuties = List.from(customList);
|
|
|
|
if (mounted) {
|
|
setState(() {});
|
|
}
|
|
}),
|
|
);
|
|
|
|
final subChargeItemsBox = Wrap(
|
|
alignment: WrapAlignment.spaceBetween,
|
|
runSpacing: 15,
|
|
children: _customDuties.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(() {
|
|
_customDuties.removeAt(key);
|
|
});
|
|
},
|
|
child: Icon(Feather.minus_circle)),
|
|
const SizedBox(width: 10),
|
|
Flexible(
|
|
child: inputTextFieldWidget(
|
|
context,
|
|
lableText: c.name ?? "",
|
|
),
|
|
),
|
|
],
|
|
),
|
|
);
|
|
}).toList());
|
|
|
|
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);
|
|
// }
|
|
},
|
|
);
|
|
|
|
final previousBtn = PreviousButton(onTap: () {
|
|
if (widget.onPrevious != null) {
|
|
widget.onPrevious!(_cargoTypes, _customDuties);
|
|
}
|
|
});
|
|
|
|
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),
|
|
totalBox,
|
|
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,
|
|
Function(String)? onChanged}) {
|
|
return TextFormField(
|
|
controller: controller,
|
|
style: textStyle,
|
|
cursorColor: primaryColor,
|
|
keyboardType: TextInputType.number,
|
|
onChanged: onChanged,
|
|
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)),
|
|
),
|
|
);
|
|
}
|
|
}
|