import 'package:fcs/domain/entities/discount_by_weight.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/rates/model/shipment_rate_model.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'; class DiscountByWeightEditor extends StatefulWidget { final DiscountByWeight discountByWeight; DiscountByWeightEditor({this.discountByWeight}); @override _DiscountByWeightEditorState createState() => _DiscountByWeightEditorState(); } class _DiscountByWeightEditorState extends State { TextEditingController _weightController = new TextEditingController(); TextEditingController _discountController = new TextEditingController(); bool _isLoading = false; bool _isNew = false; DiscountByWeight _discountByWeight = new DiscountByWeight(); @override void initState() { super.initState(); if (widget.discountByWeight != null) { _discountByWeight = widget.discountByWeight; _weightController.text = _discountByWeight.weight.toString(); _discountController.text = _discountByWeight.discount.toString(); } } @override void dispose() { super.dispose(); } @override Widget build(BuildContext context) { final weightBox = InputText( labelTextKey: 'rate.discount.weight', iconData: FontAwesomeIcons.weightHanging, controller: _weightController); final discountRateBox = InputText( labelTextKey: 'rate.discount.rate', iconData: Icons.attach_money, controller: _discountController); return LocalProgress( inAsyncCall: _isLoading, child: Scaffold( appBar: AppBar( centerTitle: true, leading: new IconButton( icon: new Icon(CupertinoIcons.back), onPressed: () => Navigator.of(context).pop(), ), backgroundColor: primaryColor, title: Text(AppTranslations.of(context).text("discount.new")), actions: [ IconButton( icon: Icon(Icons.delete), onPressed: _delete, ) ], ), body: 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: 10) ], ), ), ), ); } _save() async { 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 = widget.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(widget.discountByWeight.id); Navigator.pop(context); } catch (e) { showMsgDialog(context, "Error", e.toString()); } finally { setState(() { _isLoading = false; }); } } }