2020-10-12 08:26:27 +06:30
|
|
|
import 'package:fcs/domain/constants.dart';
|
2020-10-18 02:38:46 +06:30
|
|
|
import 'package:fcs/domain/entities/carton.dart';
|
2020-10-15 03:06:13 +06:30
|
|
|
import 'package:fcs/domain/entities/shipment.dart';
|
2020-10-08 11:38:05 +06:30
|
|
|
import 'package:fcs/domain/vo/delivery_address.dart';
|
2020-10-07 02:33:06 +06:30
|
|
|
import 'package:fcs/helpers/theme.dart';
|
2020-10-08 15:54:43 +06:30
|
|
|
import 'package:fcs/pages/delivery_address/model/delivery_address_model.dart';
|
2020-10-07 02:33:06 +06:30
|
|
|
import 'package:fcs/pages/main/model/main_model.dart';
|
2020-10-16 10:58:31 +06:30
|
|
|
import 'package:fcs/pages/main/util.dart';
|
2020-10-07 14:42:07 +06:30
|
|
|
import 'package:fcs/pages/shipment/model/shipment_model.dart';
|
2020-10-13 07:50:25 +06:30
|
|
|
import 'package:fcs/pages/widgets/defalut_delivery_address.dart';
|
|
|
|
|
import 'package:fcs/pages/widgets/delivery_address_selection.dart';
|
2020-10-12 08:26:27 +06:30
|
|
|
import 'package:fcs/pages/widgets/display_text.dart';
|
2020-10-18 02:38:46 +06:30
|
|
|
import 'package:fcs/pages/widgets/fcs_id_icon.dart';
|
2020-10-12 08:26:27 +06:30
|
|
|
import 'package:fcs/pages/widgets/input_date.dart';
|
|
|
|
|
import 'package:fcs/pages/widgets/input_time.dart';
|
2020-10-14 01:51:53 +06:30
|
|
|
import 'package:fcs/pages/widgets/local_button.dart';
|
|
|
|
|
import 'package:fcs/pages/widgets/local_radio_buttons.dart';
|
2020-10-07 19:53:47 +06:30
|
|
|
import 'package:fcs/pages/widgets/local_text.dart';
|
2020-10-14 01:51:53 +06:30
|
|
|
import 'package:fcs/pages/widgets/local_title.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-13 07:50:25 +06:30
|
|
|
import 'package:flutter/material.dart';
|
2020-06-01 14:42:42 +06:30
|
|
|
import 'package:intl/intl.dart';
|
2020-05-29 15:54:26 +06:30
|
|
|
import 'package:provider/provider.dart';
|
2020-10-18 02:38:46 +06:30
|
|
|
import 'package:url_launcher/url_launcher.dart';
|
2020-05-29 15:54:26 +06:30
|
|
|
|
2020-10-13 07:50:25 +06:30
|
|
|
import 'box_row.dart';
|
|
|
|
|
import 'shipment_box_editor.dart';
|
2020-10-18 02:38:46 +06:30
|
|
|
import 'widgets.dart';
|
2020-05-29 15:54:26 +06:30
|
|
|
|
2020-10-07 14:42:07 +06:30
|
|
|
class ShipmentEditor extends StatefulWidget {
|
2020-10-12 08:26:27 +06:30
|
|
|
final Shipment shipment;
|
|
|
|
|
ShipmentEditor({this.shipment});
|
2020-05-29 15:54:26 +06:30
|
|
|
|
|
|
|
|
@override
|
2020-10-07 14:42:07 +06:30
|
|
|
_ShipmentEditorState createState() => _ShipmentEditorState();
|
2020-05-29 15:54:26 +06:30
|
|
|
}
|
|
|
|
|
|
2020-10-07 14:42:07 +06:30
|
|
|
class _ShipmentEditorState extends State<ShipmentEditor> {
|
2020-06-01 14:42:42 +06:30
|
|
|
var dateFormatter = new DateFormat('dd MMM yyyy');
|
2020-10-18 02:38:46 +06:30
|
|
|
var timeFormatter = new DateFormat('jm');
|
2020-06-01 14:42:42 +06:30
|
|
|
|
2020-05-29 15:54:26 +06:30
|
|
|
TextEditingController _fromTimeEditingController =
|
|
|
|
|
new TextEditingController();
|
|
|
|
|
TextEditingController _toTimeEditingController = new TextEditingController();
|
2020-06-01 14:42:42 +06:30
|
|
|
TextEditingController _pickupDate = new TextEditingController();
|
2020-05-29 15:54:26 +06:30
|
|
|
|
2020-10-16 10:58:31 +06:30
|
|
|
Shipment _shipment;
|
2020-05-29 15:54:26 +06:30
|
|
|
bool _isLoading = false;
|
2020-06-01 14:42:42 +06:30
|
|
|
var now = new DateTime.now();
|
2020-10-12 08:26:27 +06:30
|
|
|
bool _isNew;
|
2020-05-29 15:54:26 +06:30
|
|
|
|
2020-10-16 10:58:31 +06:30
|
|
|
String _selectedShipmentType;
|
2020-06-30 16:27:56 +06:30
|
|
|
|
2020-05-29 15:54:26 +06:30
|
|
|
@override
|
|
|
|
|
void initState() {
|
|
|
|
|
super.initState();
|
2020-10-12 08:26:27 +06:30
|
|
|
|
|
|
|
|
if (widget.shipment != null) {
|
|
|
|
|
_isNew = false;
|
2020-10-16 10:58:31 +06:30
|
|
|
_shipment = widget.shipment;
|
2020-10-18 02:38:46 +06:30
|
|
|
_selectedShipmentType = _shipment.shipmentType;
|
2020-10-16 10:58:31 +06:30
|
|
|
_fromTimeEditingController.text = _shipment.pickupTimeStart;
|
|
|
|
|
_toTimeEditingController.text = _shipment.pickupTimeEnd;
|
|
|
|
|
_pickupDate.text = dateFormatter.format(_shipment.pickupDate ?? now);
|
2020-06-03 00:42:31 +06:30
|
|
|
} else {
|
2020-10-12 08:26:27 +06:30
|
|
|
_isNew = true;
|
2020-10-18 02:38:46 +06:30
|
|
|
_selectedShipmentType = shipment_local_pickup;
|
2020-10-14 01:51:53 +06:30
|
|
|
_pickupDate.text = dateFormatter.format(now);
|
2020-10-18 02:38:46 +06:30
|
|
|
_fromTimeEditingController.text = "${timeFormatter.format(now)}";
|
|
|
|
|
_toTimeEditingController.text = "${timeFormatter.format(now)}";
|
2020-10-16 10:58:31 +06:30
|
|
|
_shipment = Shipment(boxes: []);
|
|
|
|
|
var shipmentModel =
|
|
|
|
|
Provider.of<DeliveryAddressModel>(context, listen: false);
|
|
|
|
|
_shipment.pickupAddress = shipmentModel.defalutAddress;
|
|
|
|
|
_pickupDate.text = dateFormatter.format(now);
|
2020-05-29 15:54:26 +06:30
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@override
|
|
|
|
|
void dispose() {
|
|
|
|
|
super.dispose();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@override
|
|
|
|
|
Widget build(BuildContext context) {
|
2020-10-13 07:50:25 +06:30
|
|
|
MainModel mainModel = Provider.of<MainModel>(context);
|
|
|
|
|
ShipmentModel pickupModel = Provider.of<ShipmentModel>(context);
|
2020-10-18 02:38:46 +06:30
|
|
|
final shipmentNumberBox = getShipmentNumberStatus(context, _shipment);
|
|
|
|
|
bool isLocalPickup = _selectedShipmentType == shipment_local_pickup;
|
|
|
|
|
bool isCourierPickup = _selectedShipmentType == shipment_courier_pickup;
|
|
|
|
|
bool isLocalDropoff = _selectedShipmentType == shipment_local_dropoff;
|
|
|
|
|
bool isCourierDropoff = _selectedShipmentType == shipment_courier_dropoff;
|
2020-10-12 08:26:27 +06:30
|
|
|
final fromTimeBox = InputTime(
|
2020-10-08 11:43:53 +06:30
|
|
|
labelTextKey: 'shipment.from',
|
|
|
|
|
iconData: Icons.timer,
|
|
|
|
|
controller: _fromTimeEditingController);
|
2020-05-29 15:54:26 +06:30
|
|
|
|
2020-10-14 01:51:53 +06:30
|
|
|
final toTimeBox = Container(
|
|
|
|
|
width: 150,
|
|
|
|
|
child: InputTime(
|
|
|
|
|
iconData: Icons.timer_off,
|
|
|
|
|
labelTextKey: 'shipment.to',
|
|
|
|
|
controller: _toTimeEditingController));
|
2020-06-01 14:42:42 +06:30
|
|
|
|
2020-10-14 01:51:53 +06:30
|
|
|
final pickupDateBox = InputDate(
|
2020-10-13 07:50:25 +06:30
|
|
|
labelTextKey: "shipment.date",
|
|
|
|
|
iconData: Icons.date_range,
|
|
|
|
|
controller: _pickupDate,
|
|
|
|
|
);
|
2020-10-18 02:38:46 +06:30
|
|
|
final curierDropoffAddress = Padding(
|
|
|
|
|
padding: EdgeInsets.all(8),
|
|
|
|
|
child: FloatingActionButton.extended(
|
|
|
|
|
onPressed: _openCourierWebsite,
|
|
|
|
|
icon: Icon(Icons.open_in_new, color: primaryColor),
|
|
|
|
|
label: Text(
|
|
|
|
|
'Visit courier websie \nfor nearest drop-off',
|
|
|
|
|
style: TextStyle(fontSize: 16, color: primaryColor),
|
|
|
|
|
),
|
|
|
|
|
backgroundColor: Colors.white,
|
|
|
|
|
));
|
2020-10-14 01:51:53 +06:30
|
|
|
final pickupAddressBox = DefaultDeliveryAddress(
|
2020-10-16 10:58:31 +06:30
|
|
|
deliveryAddress: _shipment.pickupAddress,
|
2020-10-14 01:51:53 +06:30
|
|
|
iconData: Icons.location_on,
|
2020-10-13 07:50:25 +06:30
|
|
|
labelKey: "shipment.location",
|
|
|
|
|
onTap: () async {
|
2020-10-16 10:58:31 +06:30
|
|
|
DeliveryAddress address = await Navigator.push<DeliveryAddress>(
|
2020-10-13 07:50:25 +06:30
|
|
|
context,
|
2020-10-14 13:54:42 +06:30
|
|
|
CupertinoPageRoute(
|
|
|
|
|
builder: (context) => DeliveryAddressSelection(
|
2020-10-16 10:58:31 +06:30
|
|
|
deliveryAddress: _shipment.pickupAddress,
|
2020-10-14 13:54:42 +06:30
|
|
|
)),
|
2020-10-13 07:50:25 +06:30
|
|
|
);
|
2020-10-16 10:58:31 +06:30
|
|
|
if (address == null) return;
|
2020-10-13 07:50:25 +06:30
|
|
|
setState(() {
|
2020-10-16 10:58:31 +06:30
|
|
|
_shipment.pickupAddress = address;
|
2020-10-13 07:50:25 +06:30
|
|
|
});
|
|
|
|
|
},
|
2020-06-02 14:52:31 +06:30
|
|
|
);
|
2020-10-18 02:38:46 +06:30
|
|
|
var localDropoffAddressBox = Row(children: [
|
|
|
|
|
FcsIDIcon(),
|
|
|
|
|
Expanded(
|
|
|
|
|
child: DisplayText(
|
|
|
|
|
text: mainModel.setting.usaAddress,
|
|
|
|
|
),
|
|
|
|
|
)
|
|
|
|
|
]);
|
2020-10-15 17:33:43 +06:30
|
|
|
final createBtn = LocalButton(
|
|
|
|
|
textKey: "shipment.create",
|
2020-10-18 02:38:46 +06:30
|
|
|
callBack: _save,
|
2020-10-15 17:33:43 +06:30
|
|
|
);
|
|
|
|
|
|
|
|
|
|
final updateBtn = LocalButton(
|
|
|
|
|
textKey: "shipment.update",
|
2020-10-18 02:38:46 +06:30
|
|
|
callBack: _save,
|
2020-10-15 17:33:43 +06:30
|
|
|
);
|
2020-10-14 01:51:53 +06:30
|
|
|
|
2020-05-29 15:54:26 +06:30
|
|
|
return LocalProgress(
|
|
|
|
|
inAsyncCall: _isLoading,
|
|
|
|
|
child: Scaffold(
|
|
|
|
|
appBar: AppBar(
|
2020-05-31 15:00:11 +06:30
|
|
|
centerTitle: true,
|
|
|
|
|
leading: new IconButton(
|
2020-10-13 07:50:25 +06:30
|
|
|
icon: new Icon(
|
2020-10-14 13:54:42 +06:30
|
|
|
CupertinoIcons.back,
|
2020-10-13 07:50:25 +06:30
|
|
|
color: primaryColor,
|
|
|
|
|
),
|
2020-12-04 17:28:21 +06:30
|
|
|
onPressed: () {
|
|
|
|
|
showConfirmDialog(context, "back.button_confirm", () {
|
|
|
|
|
Navigator.of(context).pop();
|
|
|
|
|
});
|
|
|
|
|
},
|
2020-05-31 15:00:11 +06:30
|
|
|
),
|
2020-10-12 08:26:27 +06:30
|
|
|
shadowColor: Colors.transparent,
|
|
|
|
|
backgroundColor: Colors.white,
|
|
|
|
|
title: LocalText(
|
|
|
|
|
context,
|
|
|
|
|
_isNew ? "shipment.new.title" : "shipment.edit.title",
|
|
|
|
|
fontSize: 20,
|
|
|
|
|
color: primaryColor,
|
|
|
|
|
),
|
2020-05-29 15:54:26 +06:30
|
|
|
),
|
2020-10-12 08:26:27 +06:30
|
|
|
body: Padding(
|
|
|
|
|
padding: const EdgeInsets.all(10.0),
|
|
|
|
|
child: ListView(
|
2020-05-29 15:54:26 +06:30
|
|
|
children: <Widget>[
|
2020-10-18 02:38:46 +06:30
|
|
|
_isNew ? Container() : Center(child: shipmentNumberBox),
|
2020-10-14 01:51:53 +06:30
|
|
|
LocalTitle(textKey: "shipment.type"),
|
|
|
|
|
LocalRadioButtons(
|
2020-10-16 10:58:31 +06:30
|
|
|
values: pickupModel.shipmentTypes,
|
|
|
|
|
selectedValue: _selectedShipmentType,
|
2020-10-14 01:51:53 +06:30
|
|
|
callback: (v) {
|
|
|
|
|
setState(() {
|
2020-10-16 10:58:31 +06:30
|
|
|
_selectedShipmentType = v;
|
2020-10-14 01:51:53 +06:30
|
|
|
});
|
|
|
|
|
}),
|
2020-10-18 02:38:46 +06:30
|
|
|
...(isLocalDropoff
|
|
|
|
|
? [
|
|
|
|
|
LocalTitle(textKey: "shipment.location.dropoff"),
|
|
|
|
|
localDropoffAddressBox
|
|
|
|
|
]
|
|
|
|
|
: []),
|
|
|
|
|
...(isCourierDropoff
|
|
|
|
|
? [
|
|
|
|
|
LocalTitle(textKey: "shipment.courier.dropoff"),
|
|
|
|
|
curierDropoffAddress
|
|
|
|
|
]
|
|
|
|
|
: []),
|
|
|
|
|
...(isCourierPickup || isLocalPickup
|
|
|
|
|
? [
|
|
|
|
|
LocalTitle(textKey: "shipment.location"),
|
|
|
|
|
pickupAddressBox,
|
|
|
|
|
LocalTitle(textKey: "shipment.date.time"),
|
|
|
|
|
pickupDateBox,
|
|
|
|
|
fromTimeBox,
|
|
|
|
|
toTimeBox,
|
|
|
|
|
]
|
|
|
|
|
: []),
|
2020-10-14 01:51:53 +06:30
|
|
|
LocalTitle(
|
|
|
|
|
textKey: "boxes.name",
|
|
|
|
|
trailing: IconButton(
|
|
|
|
|
icon: Icon(
|
|
|
|
|
Icons.add_circle,
|
|
|
|
|
color: primaryColor,
|
|
|
|
|
),
|
2020-10-16 10:58:31 +06:30
|
|
|
onPressed: () async {
|
2020-10-18 02:38:46 +06:30
|
|
|
Carton box = await Navigator.push(
|
2020-10-14 01:51:53 +06:30
|
|
|
context,
|
2020-10-14 13:54:42 +06:30
|
|
|
CupertinoPageRoute(
|
|
|
|
|
builder: (context) => ShipmentBoxEditor()),
|
2020-10-14 01:51:53 +06:30
|
|
|
);
|
2020-10-16 10:58:31 +06:30
|
|
|
_addBox(box);
|
2020-10-14 01:51:53 +06:30
|
|
|
},
|
2020-10-12 08:26:27 +06:30
|
|
|
),
|
|
|
|
|
),
|
|
|
|
|
Column(
|
2020-10-16 10:58:31 +06:30
|
|
|
children: getBoxList(context, _shipment.boxes),
|
2020-10-12 08:26:27 +06:30
|
|
|
),
|
2020-10-15 17:33:43 +06:30
|
|
|
_isNew ? createBtn : updateBtn,
|
2020-05-29 15:54:26 +06:30
|
|
|
],
|
|
|
|
|
),
|
|
|
|
|
),
|
|
|
|
|
),
|
|
|
|
|
);
|
|
|
|
|
}
|
2020-06-26 16:17:40 +06:30
|
|
|
|
2020-10-18 02:38:46 +06:30
|
|
|
List<Widget> getBoxList(BuildContext context, List<Carton> boxes) {
|
2020-10-13 07:50:25 +06:30
|
|
|
return boxes.asMap().entries.map((_box) {
|
2020-06-26 16:17:40 +06:30
|
|
|
return InkWell(
|
2020-10-16 10:58:31 +06:30
|
|
|
onTap: () async {
|
2020-10-18 02:38:46 +06:30
|
|
|
Carton box = await Navigator.of(context).push(CupertinoPageRoute(
|
2020-10-14 13:54:42 +06:30
|
|
|
builder: (context) => ShipmentBoxEditor(box: _box.value)));
|
2020-10-16 10:58:31 +06:30
|
|
|
_saveBox(box);
|
2020-06-26 16:17:40 +06:30
|
|
|
},
|
2020-10-16 10:58:31 +06:30
|
|
|
child: Row(
|
|
|
|
|
children: [
|
|
|
|
|
Expanded(child: BoxRow(box: _box.value)),
|
|
|
|
|
InkWell(
|
|
|
|
|
onTap: () => _removeBox(_box.value),
|
|
|
|
|
child: Padding(
|
|
|
|
|
padding: const EdgeInsets.all(8.0),
|
2020-10-18 02:38:46 +06:30
|
|
|
child: Icon(Icons.remove_circle, color: primaryColor),
|
2020-10-16 10:58:31 +06:30
|
|
|
),
|
|
|
|
|
),
|
|
|
|
|
],
|
|
|
|
|
),
|
2020-06-26 16:17:40 +06:30
|
|
|
);
|
|
|
|
|
}).toList();
|
|
|
|
|
}
|
2020-10-14 01:51:53 +06:30
|
|
|
|
2020-10-18 02:38:46 +06:30
|
|
|
_addBox(Carton box) {
|
2020-10-16 10:58:31 +06:30
|
|
|
if (box == null) return;
|
2020-10-18 02:38:46 +06:30
|
|
|
box.cartonType = carton_from_shipments;
|
2020-10-16 10:58:31 +06:30
|
|
|
_shipment.boxes.add(box);
|
|
|
|
|
setState(() {});
|
|
|
|
|
}
|
|
|
|
|
|
2020-10-18 02:38:46 +06:30
|
|
|
_saveBox(Carton box) {
|
2020-10-16 10:58:31 +06:30
|
|
|
if (box == null) return;
|
|
|
|
|
setState(() {});
|
|
|
|
|
}
|
|
|
|
|
|
2020-10-18 02:38:46 +06:30
|
|
|
_removeBox(Carton box) {
|
2020-10-16 10:58:31 +06:30
|
|
|
if (box == null) return;
|
|
|
|
|
_shipment.boxes.remove(box);
|
|
|
|
|
setState(() {});
|
|
|
|
|
}
|
|
|
|
|
|
2020-10-18 02:38:46 +06:30
|
|
|
_save() async {
|
2020-10-16 10:58:31 +06:30
|
|
|
_shipment.shipmentType = this._selectedShipmentType;
|
|
|
|
|
_shipment.pickupDate = dateFormatter.parse(_pickupDate.text);
|
|
|
|
|
_shipment.pickupTimeStart = _fromTimeEditingController.text;
|
|
|
|
|
_shipment.pickupTimeEnd = _toTimeEditingController.text;
|
|
|
|
|
setState(() {
|
|
|
|
|
_isLoading = true;
|
|
|
|
|
});
|
|
|
|
|
try {
|
|
|
|
|
ShipmentModel shipmentModel =
|
|
|
|
|
Provider.of<ShipmentModel>(context, listen: false);
|
|
|
|
|
if (_isNew) {
|
|
|
|
|
await shipmentModel.createShipment(_shipment);
|
|
|
|
|
} else {
|
2020-10-18 02:38:46 +06:30
|
|
|
await shipmentModel.updateShipment(_shipment);
|
2020-10-16 10:58:31 +06:30
|
|
|
}
|
2020-10-18 02:38:46 +06:30
|
|
|
Navigator.pop(context, true);
|
2020-10-16 10:58:31 +06:30
|
|
|
} catch (e) {
|
|
|
|
|
showMsgDialog(context, "Error", e.toString());
|
|
|
|
|
} finally {
|
|
|
|
|
setState(() {
|
|
|
|
|
_isLoading = false;
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
}
|
2020-10-18 02:38:46 +06:30
|
|
|
|
|
|
|
|
_openCourierWebsite() {
|
|
|
|
|
MainModel mainModel = Provider.of<MainModel>(context, listen: false);
|
|
|
|
|
launch("${mainModel.setting.courierWebsite}");
|
|
|
|
|
}
|
2020-05-29 15:54:26 +06:30
|
|
|
}
|