2025-03-12 17:49:27 +06:30
|
|
|
// ignore_for_file: use_build_context_synchronously
|
|
|
|
|
|
2021-01-08 17:13:51 +06:30
|
|
|
import 'package:fcs/domain/entities/cargo_type.dart';
|
2020-10-07 02:33:06 +06:30
|
|
|
import 'package:fcs/helpers/theme.dart';
|
|
|
|
|
import 'package:fcs/pages/main/util.dart';
|
2020-10-07 18:49:28 +06:30
|
|
|
import 'package:fcs/pages/widgets/input_text.dart';
|
2024-01-25 17:40:35 +06:30
|
|
|
import 'package:fcs/pages/widgets/local_app_bar.dart';
|
2020-10-07 02:33:06 +06:30
|
|
|
import 'package:fcs/pages/widgets/progress.dart';
|
2020-06-29 16:15:25 +06:30
|
|
|
import 'package:flutter/material.dart';
|
2025-03-12 17:49:27 +06:30
|
|
|
import 'package:flutter_vector_icons/flutter_vector_icons.dart';
|
2020-06-29 16:15:25 +06:30
|
|
|
import 'package:font_awesome_flutter/font_awesome_flutter.dart';
|
2020-10-15 15:49:02 +06:30
|
|
|
import 'package:provider/provider.dart';
|
|
|
|
|
|
|
|
|
|
import 'model/shipment_rate_model.dart';
|
2020-06-29 16:15:25 +06:30
|
|
|
|
|
|
|
|
class CustomEditor extends StatefulWidget {
|
2021-09-10 12:02:08 +06:30
|
|
|
final CargoType? custom;
|
2025-03-12 17:49:27 +06:30
|
|
|
const CustomEditor({super.key, this.custom});
|
2020-06-29 16:15:25 +06:30
|
|
|
|
|
|
|
|
@override
|
|
|
|
|
_CustomEditorState createState() => _CustomEditorState();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
class _CustomEditorState extends State<CustomEditor> {
|
2025-03-12 17:49:27 +06:30
|
|
|
TextEditingController productController = TextEditingController();
|
|
|
|
|
TextEditingController feeController = TextEditingController();
|
|
|
|
|
TextEditingController shipmentRateController = TextEditingController();
|
2020-06-29 16:15:25 +06:30
|
|
|
|
|
|
|
|
bool _isLoading = false;
|
2025-03-12 17:49:27 +06:30
|
|
|
CargoType _custom = CargoType();
|
2020-10-15 15:49:02 +06:30
|
|
|
bool _isNew = false;
|
2024-02-28 17:07:23 +06:30
|
|
|
final _customFormKey = GlobalKey<FormState>();
|
2020-06-29 16:15:25 +06:30
|
|
|
|
|
|
|
|
@override
|
|
|
|
|
void initState() {
|
|
|
|
|
super.initState();
|
|
|
|
|
if (widget.custom != null) {
|
2021-09-10 12:02:08 +06:30
|
|
|
_custom = widget.custom!;
|
2025-03-12 17:49:27 +06:30
|
|
|
productController.text = _custom.name ?? "";
|
|
|
|
|
feeController.text = _custom.customDutyFee.toStringAsFixed(2);
|
|
|
|
|
shipmentRateController.text = _custom.rate.toStringAsFixed(2);
|
2020-10-15 15:49:02 +06:30
|
|
|
} else {
|
|
|
|
|
_isNew = true;
|
2020-06-29 16:15:25 +06:30
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@override
|
|
|
|
|
void dispose() {
|
|
|
|
|
super.dispose();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@override
|
|
|
|
|
Widget build(BuildContext context) {
|
2020-10-07 18:49:28 +06:30
|
|
|
final productBox = InputText(
|
2025-03-12 17:49:27 +06:30
|
|
|
labelTextKey: 'rate.cutom.product_type',
|
|
|
|
|
iconData: FontAwesomeIcons.weightHanging,
|
|
|
|
|
autovalidateMode: AutovalidateMode.onUserInteraction,
|
|
|
|
|
controller: productController,
|
|
|
|
|
validator: (value) {
|
|
|
|
|
if (value == null || value.isEmpty) {
|
|
|
|
|
return "Please insert product type";
|
|
|
|
|
}
|
|
|
|
|
return null;
|
|
|
|
|
},
|
|
|
|
|
);
|
|
|
|
|
|
2020-10-07 18:49:28 +06:30
|
|
|
final feeBox = InputText(
|
2024-02-28 17:07:23 +06:30
|
|
|
labelTextKey: 'rate.custom.fee',
|
2025-03-12 17:49:27 +06:30
|
|
|
iconData: Fontisto.dollar,
|
|
|
|
|
controller: feeController,
|
2024-02-28 17:07:23 +06:30
|
|
|
autovalidateMode: AutovalidateMode.onUserInteraction,
|
|
|
|
|
validator: (value) {
|
|
|
|
|
if (value == null || value.isEmpty) {
|
|
|
|
|
return "Please insert fee";
|
|
|
|
|
}
|
|
|
|
|
return null;
|
|
|
|
|
},
|
|
|
|
|
);
|
2020-12-08 20:24:15 +06:30
|
|
|
|
|
|
|
|
final shipmentRateBox = InputText(
|
2024-02-28 17:07:23 +06:30
|
|
|
labelTextKey: 'rate.custom.shipment_rate',
|
2025-03-12 17:49:27 +06:30
|
|
|
iconData: Fontisto.dollar,
|
|
|
|
|
controller: shipmentRateController,
|
2024-02-28 17:07:23 +06:30
|
|
|
autovalidateMode: AutovalidateMode.onUserInteraction,
|
|
|
|
|
validator: (value) {
|
|
|
|
|
if (value == null || value.isEmpty) {
|
|
|
|
|
return "Please insert shipment rate";
|
|
|
|
|
}
|
|
|
|
|
return null;
|
|
|
|
|
},
|
|
|
|
|
);
|
2020-06-29 16:15:25 +06:30
|
|
|
return LocalProgress(
|
|
|
|
|
inAsyncCall: _isLoading,
|
|
|
|
|
child: Scaffold(
|
2024-01-25 17:40:35 +06:30
|
|
|
appBar: LocalAppBar(
|
|
|
|
|
labelKey: 'rate.custom.form.title',
|
|
|
|
|
backgroundColor: Colors.white,
|
|
|
|
|
arrowColor: primaryColor,
|
|
|
|
|
labelColor: primaryColor,
|
|
|
|
|
onBack: () {
|
|
|
|
|
if (isDataChanged()) {
|
|
|
|
|
showConfirmDialog(context, "back.button_confirm", () {
|
2020-12-04 17:28:21 +06:30
|
|
|
Navigator.of(context).pop();
|
2024-01-25 17:40:35 +06:30
|
|
|
});
|
|
|
|
|
} else {
|
|
|
|
|
Navigator.of(context).pop();
|
|
|
|
|
}
|
|
|
|
|
},
|
2020-10-15 15:49:02 +06:30
|
|
|
actions: [
|
2021-10-09 17:08:28 +06:30
|
|
|
_isNew
|
|
|
|
|
? Container()
|
|
|
|
|
: IconButton(
|
2024-01-25 17:40:35 +06:30
|
|
|
icon: Icon(Icons.delete, color: primaryColor),
|
2021-10-09 17:08:28 +06:30
|
|
|
onPressed: _delete,
|
|
|
|
|
)
|
2020-10-15 15:49:02 +06:30
|
|
|
],
|
2020-06-29 16:15:25 +06:30
|
|
|
),
|
2024-02-28 17:07:23 +06:30
|
|
|
body: Form(
|
|
|
|
|
key: _customFormKey,
|
|
|
|
|
child: Container(
|
|
|
|
|
padding: EdgeInsets.all(18),
|
|
|
|
|
child: Column(
|
|
|
|
|
children: <Widget>[
|
|
|
|
|
Expanded(
|
|
|
|
|
child: ListView(
|
|
|
|
|
children: <Widget>[
|
|
|
|
|
productBox,
|
|
|
|
|
feeBox,
|
|
|
|
|
shipmentRateBox,
|
|
|
|
|
SizedBox(height: 30),
|
|
|
|
|
],
|
|
|
|
|
),
|
2020-06-29 16:15:25 +06:30
|
|
|
),
|
2024-02-28 17:07:23 +06:30
|
|
|
fcsButton(context, getLocalString(context, "btn.save"),
|
|
|
|
|
callack: _save),
|
2025-03-12 17:49:27 +06:30
|
|
|
SizedBox(height: 30)
|
2024-02-28 17:07:23 +06:30
|
|
|
],
|
|
|
|
|
),
|
2020-06-29 16:15:25 +06:30
|
|
|
),
|
|
|
|
|
),
|
|
|
|
|
),
|
|
|
|
|
);
|
|
|
|
|
}
|
2020-10-15 15:49:02 +06:30
|
|
|
|
|
|
|
|
_save() async {
|
2024-02-28 17:07:23 +06:30
|
|
|
if (!_customFormKey.currentState!.validate()) {
|
2021-10-11 17:09:47 +06:30
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2020-10-15 15:49:02 +06:30
|
|
|
setState(() {
|
|
|
|
|
_isLoading = true;
|
|
|
|
|
});
|
|
|
|
|
try {
|
|
|
|
|
var shipmentRateModel =
|
|
|
|
|
Provider.of<ShipmentRateModel>(context, listen: false);
|
2025-03-12 17:49:27 +06:30
|
|
|
CargoType customduty = CargoType(
|
|
|
|
|
name: productController.text,
|
|
|
|
|
customDutyFee: double.parse(feeController.text),
|
|
|
|
|
rate: double.parse(shipmentRateController.text));
|
2020-10-15 15:49:02 +06:30
|
|
|
if (_isNew) {
|
2025-03-12 17:49:27 +06:30
|
|
|
await shipmentRateModel.addCustomDuty(customduty);
|
2020-10-15 15:49:02 +06:30
|
|
|
} else {
|
2025-03-12 17:49:27 +06:30
|
|
|
customduty.id = _custom.id;
|
|
|
|
|
await shipmentRateModel.updateCustomDuty(customduty);
|
2020-10-15 15:49:02 +06:30
|
|
|
}
|
|
|
|
|
Navigator.pop(context);
|
|
|
|
|
} catch (e) {
|
|
|
|
|
showMsgDialog(context, "Error", e.toString());
|
|
|
|
|
} finally {
|
|
|
|
|
setState(() {
|
|
|
|
|
_isLoading = false;
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
_delete() {
|
2020-10-17 01:40:24 +06:30
|
|
|
showConfirmDialog(
|
|
|
|
|
context, "rate.custom.edit.delete.confirm", _deleteCustomDuty);
|
2020-10-15 15:49:02 +06:30
|
|
|
}
|
|
|
|
|
|
|
|
|
|
_deleteCustomDuty() async {
|
|
|
|
|
setState(() {
|
|
|
|
|
_isLoading = true;
|
|
|
|
|
});
|
|
|
|
|
try {
|
|
|
|
|
var shipmentRateModel =
|
|
|
|
|
Provider.of<ShipmentRateModel>(context, listen: false);
|
2025-03-12 17:49:27 +06:30
|
|
|
await shipmentRateModel.deleteCustomDuty(_custom.id!);
|
2020-10-15 15:49:02 +06:30
|
|
|
Navigator.pop(context);
|
|
|
|
|
} catch (e) {
|
|
|
|
|
showMsgDialog(context, "Error", e.toString());
|
|
|
|
|
} finally {
|
|
|
|
|
setState(() {
|
|
|
|
|
_isLoading = false;
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
}
|
2020-12-08 20:24:15 +06:30
|
|
|
|
|
|
|
|
isDataChanged() {
|
|
|
|
|
if (_isNew) {
|
2025-03-12 17:49:27 +06:30
|
|
|
return productController.text != "" ||
|
|
|
|
|
feeController.text != "" ||
|
|
|
|
|
shipmentRateController.text != "";
|
2020-12-08 20:24:15 +06:30
|
|
|
} else {
|
2025-03-12 17:49:27 +06:30
|
|
|
CargoType customduty = CargoType(
|
|
|
|
|
name: productController.text,
|
|
|
|
|
customDutyFee: double.parse(feeController.text),
|
|
|
|
|
rate: double.parse(shipmentRateController.text));
|
|
|
|
|
return _custom.isChangedForEditCustomDuty(customduty);
|
2020-12-08 20:24:15 +06:30
|
|
|
}
|
|
|
|
|
}
|
2020-06-29 16:15:25 +06:30
|
|
|
}
|