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

323 lines
11 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';
import 'package:fcs/pages/widgets/local_text.dart';
import 'package:fcs/pages/widgets/popupmenu.dart';
2020-10-07 02:33:06 +06:30
import 'package:fcs/pages/widgets/progress.dart';
2020-10-08 03:32:52 +06:30
import 'package:flutter/material.dart';
2020-06-01 14:24:45 +06:30
import 'package:flutter_icons/flutter_icons.dart';
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 {
2020-10-07 14:42:07 +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;
2020-10-08 03:32:52 +06:30
String _currentShipmentType;
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) {
_shipment = widget.shipment;
_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;
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-07 18:19:12 +06:30
final createBtn = fcsButton(
context,
getLocalString(context, "FCSshipment.create"),
2020-10-08 03:32:52 +06:30
callack: _create,
2020-06-01 14:24:45 +06:30
);
2020-10-07 18:19:12 +06:30
final updateBtn = fcsButton(
context,
getLocalString(context, "FCSshipment.update"),
2020-10-08 03:32:52 +06:30
callack: _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-07 18:19:12 +06:30
icon: new Icon(Icons.close, color: primaryColor),
2020-06-01 14:24:45 +06:30
onPressed: () => Navigator.of(context).pop(),
),
2020-10-07 18:19:12 +06:30
backgroundColor: Colors.white,
title: LocalText(
context,
"FCSshipment.form.title",
color: primaryColor,
fontSize: 18,
),
2020-10-08 03:32:52 +06:30
actions: _isNew ? [] : [menuPopWidget(context)],
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,
),
InputDate(
labelTextKey: "FCSshipment.ETA",
iconData: Icons.date_range,
controller: _arrivalDateController,
),
InputDate(
labelTextKey: "FCSshipment.departure_date",
iconData: Icons.date_range,
controller: _departureDateControler,
),
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),
2020-10-08 03:32:52 +06:30
labelText: AppTranslations.of(context)
.text('FCSshipment.shipment_type'),
icon: Icon(Ionicons.ios_airplane, color: primaryColor)),
items: mainModel.setting.shipmentTypes
.map((e) => DropdownMenuItem(child: Text(e), value: e))
.toList(),
onChanged: (selected) => {
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
Widget menuPopWidget(BuildContext context) {
return PopupMenuButton<PopupMenu>(
elevation: 3.2,
icon: Icon(Icons.more_vert, color: primaryColor),
tooltip: 'This is tooltip',
onSelected: (choice) {
if (choice.id == 1) {
Navigator.pop(context);
} else if (choice.id == 2) {
Navigator.pop(context);
} else if (choice.id == 3) {
Navigator.pop(context);
} else if (choice.id == 4) {
Navigator.pop(context);
}
},
itemBuilder: (BuildContext context) {
List<PopupMenu> menuPopup = <PopupMenu>[
PopupMenu(
id: 3,
status: "FCSshipment.commercial_invoice",
),
PopupMenu(id: 2, status: "FCSshipment.packing_list"),
PopupMenu(
id: 5,
status: "FCSshipment.dms",
),
PopupMenu(
id: 4,
status: "FCSshipment.cargo_manifest",
),
];
return menuPopup.map((PopupMenu choice) {
return PopupMenuItem<PopupMenu>(
value: choice,
child: Row(
crossAxisAlignment: CrossAxisAlignment.start,
children: <Widget>[
Container(
padding: EdgeInsets.only(left: 15),
child: LocalText(
context,
choice.status,
color: primaryColor,
)),
],
),
);
}).toList();
});
}
2020-10-08 03:32:52 +06:30
FcsShipment _getPayload() {
FcsShipment fcsShipment = FcsShipment();
fcsShipment.id = _shipment.id;
fcsShipment.shipmentNumber = _shipmentNumberController.text;
fcsShipment.shipType = _currentShipmentType;
fcsShipment.consignee = _consigneeController.text;
fcsShipment.port = _portController.text;
fcsShipment.destination = _destinationController.text;
try {
var cutoffDate = _cutoffDateController.text;
var arrivalDate = _arrivalDateController.text;
var depDate = _departureDateControler.text;
fcsShipment.cutoffDate =
cutoffDate == "" ? null : dateFormatter.parse(cutoffDate);
fcsShipment.arrivalDate =
arrivalDate == "" ? null : dateFormatter.parse(arrivalDate);
fcsShipment.departureDate =
depDate == "" ? null : dateFormatter.parse(depDate);
} 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;
}
if (fcsShipment.departureDate == null) {
await showMsgDialog(context, "Error", "Invalid departure date!");
return false;
}
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);
Navigator.pop(context);
} catch (e) {
showMsgDialog(context, "Error", e.toString());
} finally {
setState(() {
_isLoading = false;
});
}
}
2020-06-01 14:24:45 +06:30
}