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

182 lines
5.2 KiB
Dart
Raw Normal View History

2021-01-08 17:13:51 +06:30
import 'package:fcs/domain/entities/cargo_type.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-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';
import 'model/shipment_rate_model.dart';
2020-06-29 16:15:25 +06:30
class CustomEditor extends StatefulWidget {
2021-09-10 12:02:08 +06:30
final CargoType? custom;
2020-06-29 16:15:25 +06:30
CustomEditor({this.custom});
@override
_CustomEditorState createState() => _CustomEditorState();
}
class _CustomEditorState extends State<CustomEditor> {
TextEditingController _productController = new TextEditingController();
TextEditingController _feeController = new TextEditingController();
TextEditingController _shipmentRateController = new TextEditingController();
2020-06-29 16:15:25 +06:30
bool _isLoading = false;
2021-01-08 17:13:51 +06:30
CargoType _custom = new CargoType();
2020-10-15 15:49:02 +06:30
bool _isNew = false;
2020-06-29 16:15:25 +06:30
@override
void initState() {
super.initState();
if (widget.custom != null) {
2021-09-10 12:02:08 +06:30
_custom = widget.custom!;
2021-09-10 16:33:52 +06:30
_productController.text = _custom.name??"";
2021-01-08 17:13:51 +06:30
_feeController.text = _custom.customDutyFee.toStringAsFixed(2);
_shipmentRateController.text =
2021-09-10 17:14:59 +06:30
_custom.rate == null ? "" : _custom.rate.toStringAsFixed(2);
2020-10-15 15:49:02 +06:30
} else {
_isNew = true;
2020-06-29 16:15:25 +06:30
}
}
@override
void dispose() {
super.dispose();
}
@override
Widget build(BuildContext context) {
2020-10-07 18:49:28 +06:30
final productBox = InputText(
labelTextKey: 'rate.cutom.product_type',
iconData: FontAwesomeIcons.weightHanging,
controller: _productController);
final feeBox = InputText(
labelTextKey: 'rate.custom.fee',
iconData: Icons.attach_money,
controller: _feeController);
final shipmentRateBox = InputText(
labelTextKey: 'rate.custom.shipment_rate',
iconData: Icons.attach_money,
controller: _shipmentRateController);
2020-06-29 16:15:25 +06:30
return LocalProgress(
inAsyncCall: _isLoading,
child: Scaffold(
appBar: AppBar(
centerTitle: true,
leading: new IconButton(
icon: new Icon(
2020-10-14 13:54:42 +06:30
CupertinoIcons.back,
2020-06-29 16:15:25 +06:30
),
onPressed: () {
if (isDataChanged()) {
showConfirmDialog(context, "back.button_confirm", () {
Navigator.of(context).pop();
});
} else {
Navigator.of(context).pop();
}
},
2020-06-29 16:15:25 +06:30
),
backgroundColor: primaryColor,
2020-10-07 18:49:28 +06:30
title:
2021-09-10 12:02:08 +06:30
Text(AppTranslations.of(context)!.text("rate.custom.form.title")),
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
productBox,
feeBox,
shipmentRateBox,
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);
2021-01-08 17:13:51 +06:30
CargoType _customduty = CargoType(
name: _productController.text,
customDutyFee: double.parse(_feeController.text),
rate: double.parse(_shipmentRateController.text));
2020-10-15 15:49:02 +06:30
if (_isNew) {
await shipmentRateModel.addCustomDuty(_customduty);
} else {
2021-09-10 12:02:08 +06:30
_customduty.id = this._custom.id;
2020-10-15 15:49:02 +06:30
await shipmentRateModel.updateCustomDuty(_customduty);
}
Navigator.pop(context);
} catch (e) {
showMsgDialog(context, "Error", e.toString());
} finally {
setState(() {
_isLoading = false;
});
}
}
_delete() {
2020-10-17 01:40:24 +06:30
showConfirmDialog(
context, "rate.custom.edit.delete.confirm", _deleteCustomDuty);
2020-10-15 15:49:02 +06:30
}
_deleteCustomDuty() async {
setState(() {
_isLoading = true;
});
try {
var shipmentRateModel =
Provider.of<ShipmentRateModel>(context, listen: false);
2021-09-10 16:33:52 +06:30
await shipmentRateModel.deleteCustomDuty(this._custom.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 _productController.text != "" ||
_feeController.text != "" ||
_shipmentRateController.text != "";
} else {
2021-01-08 17:13:51 +06:30
CargoType _customduty = CargoType(
name: _productController.text,
customDutyFee: double.parse(_feeController.text),
rate: double.parse(_shipmentRateController.text));
2021-09-10 12:02:08 +06:30
return this._custom.isChangedForEditCustomDuty(_customduty);
}
}
2020-06-29 16:15:25 +06:30
}