update rate

This commit is contained in:
PhyoThandar
2020-10-15 15:49:02 +06:30
parent 47c07a6c88
commit 2a401f2e1f
17 changed files with 937 additions and 295 deletions

View File

@@ -1,15 +1,18 @@
import 'package:fcs/domain/entities/discount.dart';
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 Discount discount;
final DiscountByWeight discount;
DiscountByWeightEditor({this.discount});
@override
@@ -18,10 +21,11 @@ class DiscountByWeightEditor extends StatefulWidget {
class _DiscountByWeightEditorState extends State<DiscountByWeightEditor> {
TextEditingController _weightController = new TextEditingController();
TextEditingController _rateController = new TextEditingController();
TextEditingController _discountController = new TextEditingController();
bool _isLoading = false;
Discount _discount = new Discount();
DiscountByWeight _discount = new DiscountByWeight();
bool _isNew = false;
@override
void initState() {
@@ -29,7 +33,9 @@ class _DiscountByWeightEditorState extends State<DiscountByWeightEditor> {
if (widget.discount != null) {
_discount = widget.discount;
_weightController.text = _discount.weight.toString();
_rateController.text = _discount.discountRate.toString();
_discountController.text = _discount.discount.toString();
} else {
_isNew = true;
}
}
@@ -47,7 +53,7 @@ class _DiscountByWeightEditorState extends State<DiscountByWeightEditor> {
final discountRateBox = InputText(
labelTextKey: 'rate.discount.rate',
iconData: Icons.attach_money,
controller: _rateController);
controller: _discountController);
return LocalProgress(
inAsyncCall: _isLoading,
@@ -60,6 +66,12 @@ class _DiscountByWeightEditorState extends State<DiscountByWeightEditor> {
),
backgroundColor: primaryColor,
title: Text(AppTranslations.of(context).text("discount.new")),
actions: [
IconButton(
icon: Icon(Icons.delete),
onPressed: _delete,
)
],
),
body: Container(
padding: EdgeInsets.all(18),
@@ -74,9 +86,8 @@ class _DiscountByWeightEditorState extends State<DiscountByWeightEditor> {
],
),
),
widget.discount == null
? fcsButton(context, "Create", callack: () {})
: fcsButton(context, "Save", callack: () {}),
fcsButton(context, getLocalString(context, "btn.save"),
callack: _save),
SizedBox(height: 10)
],
),
@@ -84,4 +95,53 @@ class _DiscountByWeightEditorState extends State<DiscountByWeightEditor> {
),
);
}
_save() async {
setState(() {
_isLoading = true;
});
try {
var shipmentRateModel =
Provider.of<ShipmentRateModel>(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.discount.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<ShipmentRateModel>(context, listen: false);
await shipmentRateModel.deleteDiscountByWeight(widget.discount.id);
Navigator.pop(context);
} catch (e) {
showMsgDialog(context, "Error", e.toString());
} finally {
setState(() {
_isLoading = false;
});
}
}
}