Files
fcs/lib/pages/rates/discount_by_weight_editor.dart
2024-02-28 17:07:23 +06:30

191 lines
5.5 KiB
Dart

import 'package:fcs/domain/entities/discount_by_weight.dart';
import 'package:fcs/helpers/theme.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/local_app_bar.dart';
import 'package:fcs/pages/widgets/progress.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? discountByWeight;
DiscountByWeightEditor({this.discountByWeight});
@override
_DiscountByWeightEditorState createState() => _DiscountByWeightEditorState();
}
class _DiscountByWeightEditorState extends State<DiscountByWeightEditor> {
TextEditingController _weightController = new TextEditingController();
TextEditingController _discountController = new TextEditingController();
bool _isLoading = false;
bool _isNew = false;
DiscountByWeight _discountByWeight = new DiscountByWeight();
final _discountFormKey = GlobalKey<FormState>();
@override
void initState() {
super.initState();
if (widget.discountByWeight != null) {
_discountByWeight = widget.discountByWeight!;
_weightController.text = _discountByWeight.weight.toStringAsFixed(2);
_discountController.text = _discountByWeight.discount.toString();
_isNew = false;
} 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,
autovalidateMode: AutovalidateMode.onUserInteraction,
validator: (value) {
if (value == null || value.isEmpty) {
return "Please inset weight";
}
return null;
},
);
final discountRateBox = InputText(
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;
},
);
return LocalProgress(
inAsyncCall: _isLoading,
child: Scaffold(
appBar: LocalAppBar(
labelKey: 'discount.new',
backgroundColor: Colors.white,
labelColor: primaryColor,
arrowColor: primaryColor,
onBack: () {
if (isDataChanged()) {
showConfirmDialog(context, "back.button_confirm", () {
Navigator.of(context).pop();
});
} else {
Navigator.of(context).pop();
}
},
actions: [
_isNew
? Container()
: IconButton(
icon: Icon(Icons.delete, color: primaryColor),
onPressed: _delete,
)
],
),
body: Form(
key: _discountFormKey,
child: 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 {
if (!_discountFormKey.currentState!.validate()) {
return;
}
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 = this._discountByWeight.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(this._discountByWeight.id!);
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));
return this._discountByWeight.isChangedForEdit(_discount);
}
}
}