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

183 lines
4.9 KiB
Dart
Raw Normal View History

2020-10-15 03:06:13 +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/pages/main/util.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-25 16:19:23 +06:30
import 'package:flutter/material.dart';
2020-10-15 15:49:02 +06:30
import 'package:provider/provider.dart';
import 'model/shipment_rate_model.dart';
2020-06-25 16:19:23 +06:30
class CargoEditor extends StatefulWidget {
2021-09-10 12:02:08 +06:30
final CargoType? cargo;
2020-10-15 03:06:13 +06:30
CargoEditor({this.cargo});
2020-06-25 16:19:23 +06:30
@override
_CargoEditorState createState() => _CargoEditorState();
}
class _CargoEditorState extends State<CargoEditor> {
TextEditingController _descController = new TextEditingController();
TextEditingController _rateController = new TextEditingController();
bool _isLoading = false;
2021-09-10 12:02:08 +06:30
late CargoType _cargo;
2020-10-15 15:49:02 +06:30
bool _isNew = false;
2024-02-28 17:07:23 +06:30
final _cargoFormKey = GlobalKey<FormState>();
2020-10-15 15:49:02 +06:30
2020-06-25 16:19:23 +06:30
@override
void initState() {
super.initState();
2020-10-15 03:06:13 +06:30
if (widget.cargo != null) {
2021-09-10 12:02:08 +06:30
_cargo = widget.cargo!;
2021-09-10 16:33:52 +06:30
_descController.text = _cargo.name ?? "";
_rateController.text = _cargo.rate.toStringAsFixed(2);
2020-10-15 15:49:02 +06:30
} else {
_isNew = true;
2020-06-25 16:19:23 +06:30
}
}
@override
void dispose() {
super.dispose();
}
@override
Widget build(BuildContext context) {
2020-10-07 18:49:28 +06:30
final typeBox = InputText(
labelTextKey: 'cargo.type',
iconData: Icons.text_format,
2024-02-28 17:07:23 +06:30
controller: _descController,
autovalidateMode: AutovalidateMode.onUserInteraction,
validator: (value){
if(value==null || value.isEmpty){
return "Please insert cargo type";
}
return null;
},);
2020-10-07 18:49:28 +06:30
final rateBox = InputText(
2024-02-28 17:07:23 +06:30
labelTextKey: 'cargo.rate',
iconData: Icons.attach_money,
controller: _rateController,
autovalidateMode: AutovalidateMode.onUserInteraction,
validator: (value) {
if (value == null || value.isEmpty) {
return "Please insert rate";
}
return null;
},
);
2020-06-25 16:19:23 +06:30
return LocalProgress(
inAsyncCall: _isLoading,
child: Scaffold(
2024-01-25 17:40:35 +06:30
appBar: LocalAppBar(
labelKey: "cargo.form.title",
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-25 16:19:23 +06:30
),
2024-02-28 17:07:23 +06:30
body: Form(
key: _cargoFormKey,
child: Container(
padding: EdgeInsets.all(18),
child: Column(
children: <Widget>[
Expanded(
child: ListView(
children: <Widget>[
typeBox,
rateBox,
SizedBox(height: 30),
],
),
2020-06-25 16:19:23 +06:30
),
2024-02-28 17:07:23 +06:30
fcsButton(context, getLocalString(context, "btn.save"),
callack: _save),
SizedBox(height: 10)
],
),
2020-06-25 16:19:23 +06:30
),
),
),
);
}
2020-10-15 15:49:02 +06:30
_save() async {
2024-02-28 17:07:23 +06:30
if (!_cargoFormKey.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);
CargoType _cargo = CargoType(
name: _descController.text, rate: double.parse(_rateController.text));
if (_isNew) {
await shipmentRateModel.addCargoType(_cargo);
} else {
2021-09-10 12:02:08 +06:30
_cargo.id = this._cargo.id;
2020-10-15 15:49:02 +06:30
await shipmentRateModel.updateCargoType(_cargo);
}
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, "cargo.edit.delete.confirm", _deleteCargoType);
2020-10-15 15:49:02 +06:30
}
_deleteCargoType() async {
setState(() {
_isLoading = true;
});
try {
var shipmentRateModel =
Provider.of<ShipmentRateModel>(context, listen: false);
2021-09-10 16:33:52 +06:30
await shipmentRateModel.deleteCargoType(this._cargo.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 _descController.text != "" || _rateController.text != "";
} else {
CargoType _cargo = CargoType(
name: _descController.text, rate: double.parse(_rateController.text));
2021-09-10 12:02:08 +06:30
return this._cargo.isChangedForEdit(_cargo);
}
}
2020-06-25 16:19:23 +06:30
}