Files
fcs/lib/pages/fcs_shipment/fcs_shipment_editor.dart

295 lines
10 KiB
Dart
Raw Normal View History

2020-10-07 14:42:07 +06:30
import 'package:fcs/domain/entities/fcs_shipment.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/fcs_shipment/model/fcs_shipment_model.dart';
2020-10-07 18:19:12 +06:30
import 'package:fcs/pages/main/model/language_model.dart';
2020-10-07 02:33:06 +06:30
import 'package:fcs/pages/main/model/main_model.dart';
2020-10-07 18:19:12 +06:30
import 'package:fcs/pages/widgets/display_text.dart';
2020-10-08 03:32:52 +06:30
import 'package:fcs/pages/widgets/input_date.dart';
2020-10-07 18:19:12 +06:30
import 'package:fcs/pages/widgets/input_text.dart';
2020-10-13 07:50:25 +06:30
import 'package:fcs/pages/widgets/local_button.dart';
2020-10-07 18:19:12 +06:30
import 'package:fcs/pages/widgets/local_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-10-08 03:32:52 +06:30
import 'package:flutter/material.dart';
2021-09-10 12:00:08 +06:30
import 'package:flutter_vector_icons/flutter_vector_icons.dart';
2020-06-01 14:24:45 +06:30
import 'package:font_awesome_flutter/font_awesome_flutter.dart';
import 'package:intl/intl.dart';
import 'package:provider/provider.dart';
2020-10-07 02:33:06 +06:30
import '../main/util.dart';
2020-06-01 14:24:45 +06:30
2020-10-07 02:33:06 +06:30
class FcsShipmentEditor extends StatefulWidget {
2021-09-10 12:00:08 +06:30
final FcsShipment? shipment;
2020-10-07 02:33:06 +06:30
FcsShipmentEditor({this.shipment});
2020-06-01 14:24:45 +06:30
@override
2020-10-07 02:33:06 +06:30
_FcsShipmentEditorState createState() => _FcsShipmentEditorState();
2020-06-01 14:24:45 +06:30
}
2020-10-07 02:33:06 +06:30
class _FcsShipmentEditorState extends State<FcsShipmentEditor> {
2020-06-01 14:24:45 +06:30
var dateFormatter = new DateFormat('dd MMM yyyy');
TextEditingController _shipmentNumberController = new TextEditingController();
TextEditingController _cutoffDateController = new TextEditingController();
TextEditingController _arrivalDateController = new TextEditingController();
TextEditingController _departureDateControler = new TextEditingController();
TextEditingController _consigneeController = new TextEditingController();
TextEditingController _portController = new TextEditingController();
TextEditingController _destinationController = new TextEditingController();
TextEditingController _statusController = new TextEditingController();
2020-10-07 14:42:07 +06:30
FcsShipment _shipment = new FcsShipment();
2020-06-01 14:24:45 +06:30
bool _isLoading = false;
2021-09-10 12:00:08 +06:30
String? _currentShipmentType;
2020-10-08 03:32:52 +06:30
bool _isNew = false;
2020-06-01 14:24:45 +06:30
@override
void initState() {
super.initState();
2020-10-08 03:32:52 +06:30
_isNew = widget.shipment == null;
2020-06-01 14:24:45 +06:30
if (widget.shipment != null) {
2021-09-10 12:00:08 +06:30
_shipment = widget.shipment!;
2020-06-01 14:24:45 +06:30
_shipmentNumberController.text = _shipment.shipmentNumber;
2020-10-08 03:32:52 +06:30
_cutoffDateController.text = dateFormatter.format(_shipment.cutoffDate);
2020-06-01 14:24:45 +06:30
_arrivalDateController.text = dateFormatter.format(_shipment.arrivalDate);
_departureDateControler.text =
dateFormatter.format(_shipment.departureDate);
_statusController.text = _shipment.status;
2020-10-08 03:32:52 +06:30
_currentShipmentType = _shipment.shipType;
_consigneeController.text = _shipment.consignee;
_portController.text = _shipment.port;
_destinationController.text = _shipment.destination;
} else {
var mainModel = Provider.of<MainModel>(context, listen: false);
_currentShipmentType = mainModel.setting.shipmentTypes[0];
2020-06-01 14:24:45 +06:30
}
}
@override
void dispose() {
super.dispose();
}
@override
Widget build(BuildContext context) {
2020-10-07 18:19:12 +06:30
var languageModel = Provider.of<LanguageModel>(context);
2020-10-08 03:32:52 +06:30
var mainModel = Provider.of<MainModel>(context);
2020-06-01 14:24:45 +06:30
2020-10-13 07:50:25 +06:30
final createBtn = LocalButton(
textKey: "FCSshipment.create",
callBack: _create,
2020-06-01 14:24:45 +06:30
);
2020-10-13 07:50:25 +06:30
final updateBtn = LocalButton(
textKey: "FCSshipment.update",
callBack: _update,
2020-06-01 14:24:45 +06:30
);
return LocalProgress(
inAsyncCall: _isLoading,
child: Scaffold(
appBar: AppBar(
centerTitle: true,
2020-10-07 18:19:12 +06:30
shadowColor: Colors.transparent,
2020-06-01 14:24:45 +06:30
leading: new IconButton(
2020-10-14 13:54:42 +06:30
icon: new Icon(CupertinoIcons.back, color: primaryColor),
onPressed: () {
if (isDataChanged()) {
showConfirmDialog(context, "back.button_confirm", () {
Navigator.of(context).pop();
});
} else {
Navigator.of(context).pop();
}
},
2020-06-01 14:24:45 +06:30
),
2020-10-07 18:19:12 +06:30
backgroundColor: Colors.white,
title: LocalText(
context,
"FCSshipment.form.title",
color: primaryColor,
fontSize: 18,
),
2020-06-01 14:24:45 +06:30
),
2020-10-08 03:32:52 +06:30
body: Padding(
padding: const EdgeInsets.all(10.0),
child: ListView(children: <Widget>[
InputText(
labelTextKey: "FCSshipment.number",
iconData: Ionicons.ios_airplane,
controller: _shipmentNumberController,
),
InputDate(
labelTextKey: "FCSshipment.cutoff_date",
iconData: Icons.date_range,
controller: _cutoffDateController,
),
2020-12-01 19:02:21 +06:30
// InputDate(
// labelTextKey: "FCSshipment.departure_date",
// iconData: Icons.date_range,
// controller: _departureDateControler,
// ),
2020-10-08 03:32:52 +06:30
InputDate(
2020-10-21 02:59:10 +06:30
labelTextKey: "FCSshipment.ETA",
2020-10-08 03:32:52 +06:30
iconData: Icons.date_range,
2020-10-21 02:59:10 +06:30
controller: _arrivalDateController,
2020-10-08 03:32:52 +06:30
),
DropdownButtonFormField(
value: _currentShipmentType == "" ? null : _currentShipmentType,
decoration: InputDecoration(
enabledBorder: UnderlineInputBorder(
borderSide: BorderSide(color: primaryColor)),
fillColor: Colors.white,
2020-10-12 08:26:27 +06:30
labelStyle: languageModel.isEng
? newLabelStyle(color: Colors.black54, fontSize: 20)
: newLabelStyleMM(color: Colors.black54, fontSize: 20),
2021-09-10 14:29:55 +06:30
labelText: AppTranslations.of(context)!
2020-10-08 03:32:52 +06:30
.text('FCSshipment.shipment_type'),
icon: Icon(Ionicons.ios_airplane, color: primaryColor)),
items: mainModel.setting.shipmentTypes
.map((e) => DropdownMenuItem(child: Text(e), value: e))
.toList(),
2021-09-10 12:00:08 +06:30
onChanged: (String? selected) => {
2020-10-08 03:32:52 +06:30
setState(() {
_currentShipmentType = selected;
})
},
),
InputText(
labelTextKey: 'FCSshipment.consignee',
iconData: Icons.work,
controller: _consigneeController),
InputText(
labelTextKey: 'FCSshipment.port_of_loading',
iconData: FontAwesomeIcons.ship,
controller: _portController),
InputText(
labelTextKey: 'FCSshipment.final_destination',
iconData: MaterialCommunityIcons.location_enter,
controller: _destinationController),
_isNew
? Container()
: Container(
padding: EdgeInsets.only(top: 5),
child: DisplayText(
text: _statusController.text,
iconData: Icons.av_timer,
labelTextKey: 'FCSshipment.status',
)),
_isNew ? createBtn : updateBtn,
SizedBox(height: 15)
]),
2020-06-01 14:24:45 +06:30
),
),
);
}
2020-10-07 18:19:12 +06:30
2020-10-08 03:32:52 +06:30
FcsShipment _getPayload() {
FcsShipment fcsShipment = FcsShipment();
fcsShipment.id = _shipment.id;
fcsShipment.shipmentNumber = _shipmentNumberController.text;
2021-09-10 12:00:08 +06:30
fcsShipment.shipType = _currentShipmentType!;
2020-10-08 03:32:52 +06:30
fcsShipment.consignee = _consigneeController.text;
fcsShipment.port = _portController.text;
fcsShipment.destination = _destinationController.text;
try {
var cutoffDate = _cutoffDateController.text;
var arrivalDate = _arrivalDateController.text;
2020-12-01 19:02:21 +06:30
// var depDate = _departureDateControler.text;
2020-10-08 03:32:52 +06:30
fcsShipment.cutoffDate =
2021-09-10 12:00:08 +06:30
(cutoffDate == "" ? null : dateFormatter.parse(cutoffDate))!;
2020-10-08 03:32:52 +06:30
fcsShipment.arrivalDate =
2021-09-10 12:00:08 +06:30
(arrivalDate == "" ? null : dateFormatter.parse(arrivalDate))!;
2020-12-01 19:02:21 +06:30
// fcsShipment.departureDate =
// depDate == "" ? null : dateFormatter.parse(depDate);
2020-10-08 03:32:52 +06:30
} catch (e) {
showMsgDialog(context, "Error", e.toString()); // shold never happen
}
return fcsShipment;
}
Future<bool> _validate(FcsShipment fcsShipment) async {
if (fcsShipment.shipmentNumber == "") {
await showMsgDialog(context, "Error", "Invalid shipment number!");
return false;
}
if (fcsShipment.shipType == null) {
await showMsgDialog(context, "Error", "Invalid shipment type!");
return false;
}
if (fcsShipment.cutoffDate == null) {
await showMsgDialog(context, "Error", "Invalid cutoff date!");
return false;
}
if (fcsShipment.arrivalDate == null) {
await showMsgDialog(context, "Error", "Invalid ETA date!");
return false;
}
2020-12-01 19:02:21 +06:30
// if (fcsShipment.departureDate == null) {
// await showMsgDialog(context, "Error", "Invalid departure date!");
// return false;
// }
2020-10-08 03:32:52 +06:30
return true;
}
Future<void> _create() async {
FcsShipment fcsShipment = _getPayload();
bool valid = await _validate(fcsShipment);
if (!valid) {
return;
}
setState(() {
_isLoading = true;
});
var shipmentModel = Provider.of<FcsShipmentModel>(context, listen: false);
try {
await shipmentModel.create(fcsShipment);
Navigator.pop(context);
} catch (e) {
showMsgDialog(context, "Error", e.toString());
} finally {
setState(() {
_isLoading = false;
});
}
}
Future<void> _update() async {
FcsShipment fcsShipment = _getPayload();
bool valid = await _validate(fcsShipment);
if (!valid) {
return;
}
setState(() {
_isLoading = true;
});
var shipmentModel = Provider.of<FcsShipmentModel>(context, listen: false);
try {
await shipmentModel.update(fcsShipment);
2020-10-19 14:02:34 +06:30
Navigator.pop(context, true);
2020-10-08 03:32:52 +06:30
} catch (e) {
showMsgDialog(context, "Error", e.toString());
} finally {
setState(() {
_isLoading = false;
});
}
}
isDataChanged() {
if (_isNew) {
var mainModel = Provider.of<MainModel>(context, listen: false);
return _shipmentNumberController.text != "" ||
_cutoffDateController.text != "" ||
_arrivalDateController.text != "" ||
_consigneeController.text != "" ||
_portController.text != "" ||
_destinationController.text != "" ||
_currentShipmentType != mainModel.setting.shipmentTypes[0];
} else {
FcsShipment fcsShipment = _getPayload();
2021-09-10 12:00:08 +06:30
return widget.shipment!.isChangedForEdit(fcsShipment);
}
}
2020-06-01 14:24:45 +06:30
}