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

164 lines
4.4 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/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-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;
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,
controller: _descController);
final rateBox = InputText(
labelTextKey: 'cargo.rate',
iconData: Icons.attach_money,
controller: _rateController);
2020-06-25 16:19:23 +06:30
return LocalProgress(
inAsyncCall: _isLoading,
child: Scaffold(
appBar: AppBar(
centerTitle: true,
leading: new IconButton(
2020-10-14 13:54:42 +06:30
icon: new Icon(CupertinoIcons.back),
onPressed: () {
if (isDataChanged()) {
showConfirmDialog(context, "back.button_confirm", () {
Navigator.of(context).pop();
});
} else {
Navigator.of(context).pop();
}
},
2020-06-25 16:19:23 +06:30
),
backgroundColor: primaryColor,
2021-09-10 12:02:08 +06:30
title: Text(AppTranslations.of(context)!.text("cargo.form.title")),
2020-10-15 15:49:02 +06:30
actions: [
2021-10-09 17:08:28 +06:30
_isNew
? Container()
: IconButton(
icon: Icon(Icons.delete),
onPressed: _delete,
)
2020-10-15 15:49:02 +06:30
],
2020-06-25 16:19:23 +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
typeBox,
rateBox,
2020-06-25 16:19:23 +06:30
SizedBox(height: 30),
],
),
),
2020-10-15 15:49:02 +06:30
fcsButton(context, getLocalString(context, "btn.save"),
callack: _save),
2020-06-25 16:19:23 +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);
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
}