import 'package:fcs/domain/entities/cargo_type.dart'; import 'package:fcs/helpers/theme.dart'; import 'package:fcs/localization/app_translations.dart'; import 'package:fcs/pages/main/util.dart'; import 'package:fcs/pages/widgets/input_text.dart'; import 'package:fcs/pages/widgets/progress.dart'; import 'package:flutter/cupertino.dart'; import 'package:flutter/material.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; CustomEditor({this.custom}); @override _CustomEditorState createState() => _CustomEditorState(); } class _CustomEditorState extends State { TextEditingController _productController = new TextEditingController(); TextEditingController _feeController = new TextEditingController(); TextEditingController _shipmentRateController = new TextEditingController(); bool _isLoading = false; CargoType _custom = new CargoType(); bool _isNew = false; @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 == null ? "" : _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, controller: _productController); final feeBox = InputText( labelTextKey: 'rate.custom.fee', iconData: Icons.attach_money, controller: _feeController); final shipmentRateBox = InputText( labelTextKey: 'rate.custom.shipment_rate', iconData: Icons.attach_money, controller: _shipmentRateController); return LocalProgress( inAsyncCall: _isLoading, child: Scaffold( appBar: AppBar( centerTitle: true, leading: new IconButton( icon: new Icon( CupertinoIcons.back, ), onPressed: () { if (isDataChanged()) { showConfirmDialog(context, "back.button_confirm", () { Navigator.of(context).pop(); }); } else { Navigator.of(context).pop(); } }, ), backgroundColor: primaryColor, title: Text(AppTranslations.of(context)!.text("rate.custom.form.title")), actions: [ IconButton( icon: Icon(Icons.delete), onPressed: _delete, ) ], ), body: 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: 10) ], ), ), ), ); } _save() async { 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 = this._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(this._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 this._custom.isChangedForEditCustomDuty(_customduty); } } }