Files
fcs/lib/pages/rates/discount_by_weight_editor.dart

149 lines
4.3 KiB
Dart
Raw Normal View History

2020-10-15 15:49:02 +06:30
import 'package:fcs/domain/entities/discount_by_weight.dart';
2020-10-07 02:33:06 +06:30
import 'package:fcs/helpers/theme.dart';
import 'package:fcs/localization/app_translations.dart';
import 'package:fcs/pages/main/util.dart';
2020-10-15 15:49:02 +06:30
import 'package:fcs/pages/rates/model/shipment_rate_model.dart';
2020-10-07 18:49:28 +06:30
import 'package:fcs/pages/widgets/input_text.dart';
2020-10-07 02:33:06 +06:30
import 'package:fcs/pages/widgets/progress.dart';
2020-10-14 13:54:42 +06:30
import 'package:flutter/cupertino.dart';
2020-06-29 16:15:25 +06:30
import 'package:flutter/material.dart';
import 'package:font_awesome_flutter/font_awesome_flutter.dart';
2020-10-15 15:49:02 +06:30
import 'package:provider/provider.dart';
2020-06-29 16:15:25 +06:30
class DiscountByWeightEditor extends StatefulWidget {
2020-10-15 10:53:41 +06:30
final DiscountByWeight discountByWeight;
DiscountByWeightEditor({this.discountByWeight});
2020-06-29 16:15:25 +06:30
@override
_DiscountByWeightEditorState createState() => _DiscountByWeightEditorState();
}
class _DiscountByWeightEditorState extends State<DiscountByWeightEditor> {
TextEditingController _weightController = new TextEditingController();
2020-10-15 15:49:02 +06:30
TextEditingController _discountController = new TextEditingController();
2020-06-29 16:15:25 +06:30
bool _isLoading = false;
2020-10-20 18:45:18 +06:30
bool _isNew;
2020-10-15 10:53:41 +06:30
DiscountByWeight _discountByWeight = new DiscountByWeight();
2020-06-29 16:15:25 +06:30
@override
void initState() {
super.initState();
2020-10-15 10:53:41 +06:30
if (widget.discountByWeight != null) {
2020-10-20 18:45:18 +06:30
_discountByWeight = widget.discountByWeight;
_weightController.text = _discountByWeight.weight.toStringAsFixed(2);
2020-10-15 15:51:43 +06:30
_discountController.text = _discountByWeight.discount.toString();
2020-10-20 18:45:18 +06:30
_isNew = false;
} else {
_isNew = true;
2020-06-29 16:15:25 +06:30
}
}
@override
void dispose() {
super.dispose();
}
@override
Widget build(BuildContext context) {
2020-10-14 13:54:42 +06:30
final weightBox = InputText(
2020-10-07 18:49:28 +06:30
labelTextKey: 'rate.discount.weight',
iconData: FontAwesomeIcons.weightHanging,
controller: _weightController);
final discountRateBox = InputText(
labelTextKey: 'rate.discount.rate',
iconData: Icons.attach_money,
2020-10-15 15:49:02 +06:30
controller: _discountController);
2020-10-07 18:49:28 +06:30
2020-06-29 16:15:25 +06:30
return LocalProgress(
inAsyncCall: _isLoading,
child: Scaffold(
appBar: AppBar(
centerTitle: true,
leading: new IconButton(
2020-10-14 13:54:42 +06:30
icon: new Icon(CupertinoIcons.back),
2020-06-29 16:15:25 +06:30
onPressed: () => Navigator.of(context).pop(),
),
backgroundColor: primaryColor,
title: Text(AppTranslations.of(context).text("discount.new")),
2020-10-15 15:49:02 +06:30
actions: [
IconButton(
icon: Icon(Icons.delete),
onPressed: _delete,
)
],
2020-06-29 16:15:25 +06:30
),
body: Container(
padding: EdgeInsets.all(18),
child: Column(
children: <Widget>[
Expanded(
child: ListView(
children: <Widget>[
2020-10-07 18:49:28 +06:30
weightBox,
discountRateBox,
2020-06-29 16:15:25 +06:30
SizedBox(height: 30),
],
),
),
2020-10-15 15:49:02 +06:30
fcsButton(context, getLocalString(context, "btn.save"),
callack: _save),
2020-06-29 16:15:25 +06:30
SizedBox(height: 10)
],
),
),
),
);
}
2020-10-15 15:49:02 +06:30
_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 {
2020-10-15 15:51:43 +06:30
_discount.id = widget.discountByWeight.id;
2020-10-15 15:49:02 +06:30
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);
2020-10-20 18:45:18 +06:30
await shipmentRateModel
.deleteDiscountByWeight(widget.discountByWeight.id);
2020-10-15 15:49:02 +06:30
Navigator.pop(context);
} catch (e) {
showMsgDialog(context, "Error", e.toString());
} finally {
setState(() {
_isLoading = false;
});
}
}
2020-06-29 16:15:25 +06:30
}