Files
fcs/lib/pages/rates/discount_by_weight_editor.dart
PhyoThandar 2a401f2e1f update rate
2020-10-15 15:49:02 +06:30

148 lines
4.3 KiB
Dart

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 DiscountByWeight discount;
DiscountByWeightEditor({this.discount});
@override
_DiscountByWeightEditorState createState() => _DiscountByWeightEditorState();
}
class _DiscountByWeightEditorState extends State<DiscountByWeightEditor> {
TextEditingController _weightController = new TextEditingController();
TextEditingController _discountController = new TextEditingController();
bool _isLoading = false;
DiscountByWeight _discount = new DiscountByWeight();
bool _isNew = false;
@override
void initState() {
super.initState();
if (widget.discount != null) {
_discount = widget.discount;
_weightController.text = _discount.weight.toString();
_discountController.text = _discount.discount.toString();
} 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);
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: <Widget>[
Expanded(
child: ListView(
children: <Widget>[
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<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;
});
}
}
}