import 'package:fcs/domain/entities/discount.dart'; import 'package:fcs/helpers/theme.dart'; import 'package:fcs/localization/app_translations.dart'; import 'package:fcs/pages/discount/model/discount_model.dart'; import 'package:fcs/pages/main/util.dart'; import 'package:fcs/pages/rates/model/shipment_rate_model.dart'; import 'package:fcs/pages/widgets/display_text.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:flutter_icons/flutter_icons.dart'; import 'package:font_awesome_flutter/font_awesome_flutter.dart'; import 'package:provider/provider.dart'; class DiscountEditor extends StatefulWidget { final Discount discount; const DiscountEditor({Key key, this.discount}) : super(key: key); @override _DiscountEditorState createState() => _DiscountEditorState(); } class _DiscountEditorState extends State { bool _isLoading = false; Discount _discount = new Discount(); TextEditingController _codeController = new TextEditingController(); TextEditingController _amountController = new TextEditingController(); TextEditingController _statusController = new TextEditingController(); TextEditingController _customerController = new TextEditingController(); bool _isNew = false; @override void initState() { super.initState(); if (widget.discount != null) { _discount = widget.discount; _codeController.text = _discount.code; _amountController.text = _discount.amount.toString(); _statusController.text = _discount.status; _customerController.text = 'Ko Nyi'; } else { _isNew = true; _customerController.text = ''; } } @override Widget build(BuildContext context) { final codeBox = InputText( labelTextKey: 'discount.code', iconData: FontAwesomeIcons.algolia, controller: _codeController); final nameBox = InputText( labelTextKey: 'discount.name', iconData: Feather.user, controller: _customerController); final amountBox = InputText( labelTextKey: 'discount.amount', iconData: FontAwesomeIcons.moneyBill, controller: _amountController); final statusBox = DisplayText( text: _statusController.text, labelTextKey: "discount.status", iconData: Icons.av_timer, ); return LocalProgress( inAsyncCall: _isLoading, child: Scaffold( backgroundColor: Colors.white, appBar: AppBar( centerTitle: true, title: Text( AppTranslations.of(context).text("discount.form"), ), leading: new IconButton( icon: new Icon(CupertinoIcons.back), onPressed: () => Navigator.of(context).pop(), ), backgroundColor: primaryColor, actions: [ IconButton( icon: Icon(Icons.delete), onPressed: _delete, ) ], ), body: Padding( padding: const EdgeInsets.all(8.0), child: Column( children: [ Expanded( child: ListView( children: [ codeBox, nameBox, amountBox, widget.discount == null ? Container() : Container( padding: EdgeInsets.only(top: 5), child: statusBox, ), ], ), ), fcsButton(context, getLocalString(context, "btn.save"), callack: _save), SizedBox( height: 30, ) ], ), )), ); } _save() async { setState(() { _isLoading = true; }); try { DiscountModel discountModel = Provider.of(context, listen: false); Discount _discount = Discount( code: _codeController.text, customer: _customerController.text, amount: double.parse(_amountController.text)); if (_isNew) { await discountModel.addDiscount(_discount); } else { _discount.id = widget.discount.id; await discountModel.updateDiscount(_discount); } Navigator.pop(context); } catch (e) { showMsgDialog(context, "Error", e.toString()); } finally { setState(() { _isLoading = false; }); } } _delete() { showConfirmDialog( context, "cargo_type.edit.delete.confirm", _deleteCargoType); } _deleteCargoType() async { setState(() { _isLoading = true; }); try { var discountModel = Provider.of(context, listen: false); await discountModel.deleteCargoType(widget.discount.id); Navigator.pop(context); } catch (e) { showMsgDialog(context, "Error", e.toString()); } finally { setState(() { _isLoading = false; }); } } }