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