// ignore_for_file: use_build_context_synchronously import 'package:fcs/domain/entities/discount_by_weight.dart'; import 'package:fcs/helpers/theme.dart'; import 'package:fcs/pages/main/util.dart'; import 'package:fcs/pages/rates/model/shipment_rate_model.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'; class DiscountByWeightEditor extends StatefulWidget { final DiscountByWeight? discountByWeight; const DiscountByWeightEditor({super.key, this.discountByWeight}); @override _DiscountByWeightEditorState createState() => _DiscountByWeightEditorState(); } class _DiscountByWeightEditorState extends State { TextEditingController weightController = TextEditingController(); TextEditingController discountController = TextEditingController(); bool _isLoading = false; bool _isNew = false; DiscountByWeight _discountByWeight = DiscountByWeight(); final _discountFormKey = GlobalKey(); @override void initState() { super.initState(); if (widget.discountByWeight != null) { _discountByWeight = widget.discountByWeight!; weightController.text = _discountByWeight.weight.toStringAsFixed(2); discountController.text = _discountByWeight.discount.toString(); _isNew = false; } else { _isNew = true; } } @override void dispose() { super.dispose(); } @override Widget build(BuildContext context) { final weightBox = InputText( labelTextKey: 'rate.discount.weight', iconData: FontAwesomeIcons.weightHanging, controller: weightController, autovalidateMode: AutovalidateMode.onUserInteraction, validator: (value) { if (value == null || value.isEmpty) { return "Please inset weight"; } return null; }, ); final discountRateBox = InputText( labelTextKey: 'rate.discount.rate', iconData: Fontisto.dollar, controller: discountController, autovalidateMode: AutovalidateMode.onUserInteraction, validator: (value) { if (value == null || value.isEmpty) { return "Please insert discount rate"; } return null; }, ); return LocalProgress( inAsyncCall: _isLoading, child: Scaffold( appBar: LocalAppBar( labelKey: 'discount.form', backgroundColor: Colors.white, labelColor: primaryColor, arrowColor: 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: _discountFormKey, child: Container( padding: EdgeInsets.all(18), child: Column( children: [ Expanded( child: ListView( children: [ weightBox, discountRateBox, SizedBox(height: 30), ], ), ), fcsButton(context, getLocalString(context, "btn.save"), callack: _save), SizedBox(height: 30) ], ), ), ), ), ); } _save() async { if (!_discountFormKey.currentState!.validate()) { return; } setState(() { _isLoading = true; }); try { var shipmentRateModel = Provider.of(context, listen: false); DiscountByWeight discount = DiscountByWeight( weight: double.parse(weightController.text), discount: double.parse(discountController.text)); if (_isNew) { await shipmentRateModel.addDiscountByWeight(discount); } else { discount.id = _discountByWeight.id; await shipmentRateModel.updateDiscountByWeight(discount); } Navigator.pop(context); } catch (e) { showMsgDialog(context, "Error", e.toString()); } finally { setState(() { _isLoading = false; }); } } _delete() { showConfirmDialog(context, "rate.discount_by_weight.edit.delete.confirm", _deleteDiscountByWeight); } _deleteDiscountByWeight() async { setState(() { _isLoading = true; }); try { var shipmentRateModel = Provider.of(context, listen: false); await shipmentRateModel.deleteDiscountByWeight(_discountByWeight.id!); Navigator.pop(context); } catch (e) { showMsgDialog(context, "Error", e.toString()); } finally { setState(() { _isLoading = false; }); } } isDataChanged() { if (_isNew) { return weightController.text != "" || discountController.text != ""; } else { DiscountByWeight discount = DiscountByWeight( weight: double.parse(weightController.text), discount: double.parse(discountController.text)); return _discountByWeight.isChangedForEdit(discount); } } }