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

191 lines
5.5 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/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';
2024-01-25 17:40:35 +06:30
import 'package:fcs/pages/widgets/local_app_bar.dart';
2020-10-07 02:33:06 +06:30
import 'package:fcs/pages/widgets/progress.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 {
2021-09-10 12:02:08 +06:30
final DiscountByWeight? discountByWeight;
2020-10-15 10:53:41 +06:30
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;
2021-09-10 12:02:08 +06:30
bool _isNew = false;
2020-10-15 10:53:41 +06:30
DiscountByWeight _discountByWeight = new DiscountByWeight();
2024-02-28 17:07:23 +06:30
final _discountFormKey = GlobalKey<FormState>();
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) {
2021-09-10 12:02:08 +06:30
_discountByWeight = widget.discountByWeight!;
2020-12-02 20:55:00 +06:30
_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(
2024-02-28 17:07:23 +06:30
labelTextKey: 'rate.discount.weight',
iconData: FontAwesomeIcons.weightHanging,
controller: _weightController,
autovalidateMode: AutovalidateMode.onUserInteraction,
validator: (value) {
if (value == null || value.isEmpty) {
return "Please inset weight";
}
return null;
},
);
2020-10-07 18:49:28 +06:30
final discountRateBox = InputText(
2024-02-28 17:07:23 +06:30
labelTextKey: 'rate.discount.rate',
iconData: Icons.attach_money,
controller: _discountController,
autovalidateMode: AutovalidateMode.onUserInteraction,
validator: (value) {
if (value == null || value.isEmpty) {
return "Please insert discount rate";
}
return null;
},
);
2020-10-07 18:49:28 +06:30
2020-06-29 16:15:25 +06:30
return LocalProgress(
inAsyncCall: _isLoading,
child: Scaffold(
2024-01-25 17:40:35 +06:30
appBar: LocalAppBar(
labelKey: 'discount.new',
backgroundColor: Colors.white,
labelColor: primaryColor,
arrowColor: primaryColor,
onBack: () {
if (isDataChanged()) {
showConfirmDialog(context, "back.button_confirm", () {
Navigator.of(context).pop();
2024-01-25 17:40:35 +06:30
});
} else {
Navigator.of(context).pop();
}
},
2020-10-15 15:49:02 +06:30
actions: [
2021-10-09 17:08:28 +06:30
_isNew
? Container()
: IconButton(
2024-01-25 17:40:35 +06:30
icon: Icon(Icons.delete, color: primaryColor),
2021-10-09 17:08:28 +06:30
onPressed: _delete,
)
2020-10-15 15:49:02 +06:30
],
2020-06-29 16:15:25 +06:30
),
2024-02-28 17:07:23 +06:30
body: Form(
key: _discountFormKey,
child: Container(
padding: EdgeInsets.all(18),
child: Column(
children: <Widget>[
Expanded(
child: ListView(
children: <Widget>[
weightBox,
discountRateBox,
SizedBox(height: 30),
],
),
2020-06-29 16:15:25 +06:30
),
2024-02-28 17:07:23 +06:30
fcsButton(context, getLocalString(context, "btn.save"),
callack: _save),
SizedBox(height: 10)
],
),
2020-06-29 16:15:25 +06:30
),
),
),
);
}
2020-10-15 15:49:02 +06:30
_save() async {
2024-02-28 17:07:23 +06:30
if (!_discountFormKey.currentState!.validate()) {
2021-10-11 17:09:47 +06:30
return;
}
2020-10-15 15:49:02 +06:30
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 {
2021-09-10 12:02:08 +06:30
_discount.id = this._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
2021-09-10 16:33:52 +06:30
.deleteDiscountByWeight(this._discountByWeight.id!);
2020-10-15 15:49:02 +06:30
Navigator.pop(context);
} catch (e) {
showMsgDialog(context, "Error", e.toString());
} finally {
setState(() {
_isLoading = false;
});
}
}
isDataChanged() {
if (_isNew) {
return _weightController.text != "" || _discountController.text != "";
} else {
DiscountByWeight _discount = DiscountByWeight(
weight: double.parse(_weightController.text),
discount: double.parse(_discountController.text));
2021-09-10 12:02:08 +06:30
return this._discountByWeight.isChangedForEdit(_discount);
}
}
2020-06-29 16:15:25 +06:30
}