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

144 lines
3.8 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 {
2020-10-15 03:06:13 +06:30
final CargoType cargo;
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;
2020-10-15 03:06:13 +06:30
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) {
_cargo = widget.cargo;
_descController.text = _cargo.name;
_rateController.text = _cargo.rate.toString();
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),
2020-06-25 16:19:23 +06:30
onPressed: () => Navigator.of(context).pop(),
),
backgroundColor: primaryColor,
title: Text(AppTranslations.of(context).text("cargo.form.title")),
2020-10-15 15:49:02 +06:30
actions: [
IconButton(
icon: Icon(Icons.delete),
onPressed: _delete,
)
],
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 {
_cargo.id = widget.cargo.id;
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);
await shipmentRateModel.deleteCargoType(widget.cargo.id);
Navigator.pop(context);
} catch (e) {
showMsgDialog(context, "Error", e.toString());
} finally {
setState(() {
_isLoading = false;
});
}
}
2020-06-25 16:19:23 +06:30
}