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

193 lines
5.6 KiB
Dart
Raw Normal View History

2025-03-12 17:49:27 +06:30
// ignore_for_file: use_build_context_synchronously
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';
2025-03-12 17:49:27 +06:30
import 'package:flutter_vector_icons/flutter_vector_icons.dart';
2020-06-29 16:15:25 +06:30
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;
2025-03-12 17:49:27 +06:30
const DiscountByWeightEditor({super.key, this.discountByWeight});
2020-06-29 16:15:25 +06:30
@override
_DiscountByWeightEditorState createState() => _DiscountByWeightEditorState();
}
class _DiscountByWeightEditorState extends State<DiscountByWeightEditor> {
2025-03-12 17:49:27 +06:30
TextEditingController weightController = TextEditingController();
TextEditingController discountController = TextEditingController();
2020-06-29 16:15:25 +06:30
bool _isLoading = false;
2021-09-10 12:02:08 +06:30
bool _isNew = false;
2025-03-12 17:49:27 +06:30
DiscountByWeight _discountByWeight = 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!;
2025-03-12 17:49:27 +06:30
weightController.text = _discountByWeight.weight.toStringAsFixed(2);
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,
2025-03-12 17:49:27 +06:30
controller: weightController,
2024-02-28 17:07:23 +06:30
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',
2025-03-12 17:49:27 +06:30
iconData: Fontisto.dollar,
controller: discountController,
2024-02-28 17:07:23 +06:30
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(
2025-03-12 17:49:27 +06:30
labelKey: 'discount.form',
2024-01-25 17:40:35 +06:30
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),
2025-03-12 17:49:27 +06:30
SizedBox(height: 30)
2024-02-28 17:07:23 +06:30
],
),
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);
2025-03-12 17:49:27 +06:30
DiscountByWeight discount = DiscountByWeight(
weight: double.parse(weightController.text),
discount: double.parse(discountController.text));
2020-10-15 15:49:02 +06:30
if (_isNew) {
2025-03-12 17:49:27 +06:30
await shipmentRateModel.addDiscountByWeight(discount);
2020-10-15 15:49:02 +06:30
} else {
2025-03-12 17:49:27 +06:30
discount.id = _discountByWeight.id;
await shipmentRateModel.updateDiscountByWeight(discount);
2020-10-15 15:49:02 +06:30
}
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);
2025-03-12 17:49:27 +06:30
await shipmentRateModel.deleteDiscountByWeight(_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) {
2025-03-12 17:49:27 +06:30
return weightController.text != "" || discountController.text != "";
} else {
2025-03-12 17:49:27 +06:30
DiscountByWeight discount = DiscountByWeight(
weight: double.parse(weightController.text),
discount: double.parse(discountController.text));
return _discountByWeight.isChangedForEdit(discount);
}
}
2020-06-29 16:15:25 +06:30
}