Files
fcs/lib/pages/shipment/shipment_box_editor.dart

307 lines
9.4 KiB
Dart
Raw Normal View History

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/cargo_type.dart';
2020-10-13 07:50:25 +06:30
import 'package:fcs/domain/vo/delivery_address.dart';
import 'package:fcs/helpers/theme.dart';
2020-10-18 02:38:46 +06:30
import 'package:fcs/pages/carton/cargo_type_editor.dart';
2020-10-13 07:50:25 +06:30
import 'package:fcs/pages/delivery_address/model/delivery_address_model.dart';
2021-01-11 19:35:26 +06:30
import 'package:fcs/pages/main/model/main_model.dart';
2020-10-15 03:06:13 +06:30
import 'package:fcs/pages/rates/model/shipment_rate_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';
import 'package:fcs/pages/widgets/display_text.dart';
2020-10-14 01:51:53 +06:30
import 'package:fcs/pages/widgets/length_picker.dart';
2020-10-13 07:50:25 +06:30
import 'package:fcs/pages/widgets/local_button.dart';
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-13 07:50:25 +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';
2021-09-10 16:48:21 +06:30
import 'package:flutter_icons_null_safety/flutter_icons_null_safety.dart';
2020-10-13 07:50:25 +06:30
import 'package:provider/provider.dart';
class ShipmentBoxEditor extends StatefulWidget {
2021-09-10 16:48:21 +06:30
final Carton? box;
2020-10-13 07:50:25 +06:30
ShipmentBoxEditor({this.box});
@override
_ShipmentBoxEditorState createState() => _ShipmentBoxEditorState();
}
class _ShipmentBoxEditorState extends State<ShipmentBoxEditor> {
TextEditingController _lengthCtl = new TextEditingController();
TextEditingController _widthCtl = new TextEditingController();
TextEditingController _heightCtl = new TextEditingController();
2021-09-10 16:48:21 +06:30
Carton? _box;
2020-10-13 07:50:25 +06:30
bool _isLoading = false;
2021-09-10 16:48:21 +06:30
late bool _isNew;
2020-10-14 01:51:53 +06:30
double volumetricRatio = 0;
double shipmentWeight = 0;
2020-10-13 07:50:25 +06:30
@override
void initState() {
super.initState();
2020-10-15 03:06:13 +06:30
volumetricRatio = Provider.of<ShipmentRateModel>(context, listen: false)
.rate
.volumetricRatio;
2020-10-13 07:50:25 +06:30
if (widget.box != null) {
_box = widget.box;
_isNew = false;
2024-01-23 16:28:08 +06:30
_lengthCtl.text = _box!.length.toString();
_widthCtl.text = _box!.width.toString();
_heightCtl.text = _box!.height.toString();
2020-10-13 07:50:25 +06:30
} else {
var shipmentModel =
Provider.of<DeliveryAddressModel>(context, listen: false);
_isNew = true;
2020-10-18 02:38:46 +06:30
_box = Carton(cargoTypes: []);
2021-09-10 16:48:21 +06:30
_box!.deliveryAddress = shipmentModel.defalutAddress;
2020-10-16 10:58:31 +06:30
_lengthCtl.text = "12";
_widthCtl.text = "12";
_heightCtl.text = "12";
2020-10-13 07:50:25 +06:30
}
_lengthCtl.addListener(_calShipmentWeight);
_widthCtl.addListener(_calShipmentWeight);
_heightCtl.addListener(_calShipmentWeight);
2020-10-16 10:58:31 +06:30
_calShipmentWeight();
2020-10-13 07:50:25 +06:30
}
_calShipmentWeight() {
2024-01-09 13:11:22 +06:30
double l = double.parse(_lengthCtl.text);
double w = double.parse(_widthCtl.text);
double h = double.parse(_heightCtl.text);
2020-10-13 07:50:25 +06:30
setState(() {
2020-10-16 10:58:31 +06:30
shipmentWeight = (l * w * h / volumetricRatio).ceilToDouble();
2020-10-13 07:50:25 +06:30
});
}
@override
Widget build(BuildContext context) {
2021-01-11 19:35:26 +06:30
var mainModel = Provider.of<MainModel>(context);
2020-10-13 07:50:25 +06:30
final shipmentWeightBox = DisplayText(
labelTextKey: "shipment.box.shipment.weight",
2024-01-23 16:28:08 +06:30
text: shipmentWeight.toStringAsFixed(0),
2020-10-13 07:50:25 +06:30
iconData: MaterialCommunityIcons.weight,
);
2020-10-14 01:51:53 +06:30
final lengthBox = LengthPicker(
2020-10-13 07:50:25 +06:30
controller: _lengthCtl,
2020-10-14 01:51:53 +06:30
lableKey: "shipment.box.length",
2020-10-13 07:50:25 +06:30
);
2020-10-14 01:51:53 +06:30
final widthBox = LengthPicker(
2020-10-13 07:50:25 +06:30
controller: _widthCtl,
2020-10-14 01:51:53 +06:30
lableKey: "shipment.box.width",
2020-10-13 07:50:25 +06:30
);
2020-10-14 01:51:53 +06:30
final heightBox = LengthPicker(
2020-10-13 07:50:25 +06:30
controller: _heightCtl,
2020-10-14 01:51:53 +06:30
lableKey: "shipment.box.height",
);
final dimBox = Row(
mainAxisAlignment: MainAxisAlignment.start,
children: [
Padding(
padding: const EdgeInsets.only(right: 8.0),
child: Icon(FontAwesome.arrow_circle_right, color: primaryColor),
),
SizedBox(child: lengthBox, width: 80),
SizedBox(child: widthBox, width: 80),
SizedBox(child: heightBox, width: 80),
],
2020-10-13 07:50:25 +06:30
);
final createBtn = LocalButton(
textKey: "shipment.box.add",
2020-10-16 10:58:31 +06:30
callBack: _creatCarton,
2020-10-13 07:50:25 +06:30
);
return LocalProgress(
inAsyncCall: _isLoading,
child: Scaffold(
appBar: AppBar(
centerTitle: true,
leading: new IconButton(
icon: new Icon(
2020-10-14 13:54:42 +06:30
CupertinoIcons.back,
2020-10-13 07:50:25 +06:30
color: primaryColor,
),
onPressed: () => Navigator.of(context).pop(),
),
shadowColor: Colors.transparent,
backgroundColor: Colors.white,
title: LocalText(
context,
2020-10-14 13:17:12 +06:30
_isNew ? "boxes.create.title" : "box.edit.title",
2020-10-13 07:50:25 +06:30
fontSize: 20,
color: primaryColor,
),
),
body: Padding(
padding: const EdgeInsets.all(8.0),
child: ListView(
children: [
2020-10-14 01:51:53 +06:30
LocalTitle(
textKey: "shipment.box.cargo.type",
trailing: IconButton(
icon: Icon(
Icons.add_circle,
color: primaryColor,
),
onPressed: () async {
2021-09-10 16:48:21 +06:30
CargoType? cargo = await Navigator.push<CargoType>(
2020-10-14 13:54:42 +06:30
context,
CupertinoPageRoute(
builder: (context) => CargoTypeEditor()));
2021-09-10 16:48:21 +06:30
if (cargo == null) return;
2020-10-14 01:51:53 +06:30
_addCargo(cargo);
}),
),
2021-09-13 11:09:32 +06:30
DataTable(
2020-10-13 07:50:25 +06:30
headingRowHeight: 40,
2021-09-13 11:09:32 +06:30
showCheckboxColumn: false,
2020-10-13 07:50:25 +06:30
columns: [
2021-09-13 11:09:32 +06:30
DataColumn(
2020-10-13 07:50:25 +06:30
label: LocalText(
context,
"cargo.type",
color: Colors.grey,
),
),
2021-09-13 11:09:32 +06:30
DataColumn(
2020-10-13 07:50:25 +06:30
label: LocalText(
context,
"cargo.weight",
color: Colors.grey,
),
),
],
rows: getCargoRows(context),
),
SizedBox(
height: 30,
),
2020-10-14 01:51:53 +06:30
LocalTitle(textKey: "shipment.box.dimemsion"),
dimBox,
2020-10-13 07:50:25 +06:30
shipmentWeightBox,
2020-10-14 01:51:53 +06:30
LocalTitle(textKey: "shipment.box.delivery"),
2020-10-13 07:50:25 +06:30
DefaultDeliveryAddress(
2021-09-10 16:48:21 +06:30
deliveryAddress: _box!.deliveryAddress,
2020-10-14 16:53:16 +06:30
labelKey: "shipment.box.delivery",
2020-10-13 07:50:25 +06:30
onTap: () async {
2021-09-10 16:48:21 +06:30
DeliveryAddress? d = 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(
2021-09-10 16:48:21 +06:30
deliveryAddress: _box!.deliveryAddress,
2021-01-11 19:35:26 +06:30
user: mainModel.user)),
2020-10-13 07:50:25 +06:30
);
if (d == null) return;
setState(() {
2021-09-10 16:48:21 +06:30
_box!.deliveryAddress = d;
2020-10-13 07:50:25 +06:30
});
}),
createBtn
],
),
),
),
);
}
2021-09-13 11:09:32 +06:30
List<DataRow> getCargoRows(BuildContext context) {
2024-01-23 16:28:08 +06:30
if (_box!.cargoTypes.isEmpty) {
2020-10-13 07:50:25 +06:30
return [];
}
2020-10-16 10:58:31 +06:30
double total = 0;
2021-09-10 16:48:21 +06:30
var rows = _box!.cargoTypes.map((c) {
2020-10-13 07:50:25 +06:30
total += c.weight;
2021-09-13 11:09:32 +06:30
return DataRow(
onSelectChanged: (bool? selected) async {
2021-09-10 16:48:21 +06:30
CargoType? cargo = await Navigator.push<CargoType>(
2020-10-14 01:51:53 +06:30
context,
2020-10-14 13:54:42 +06:30
CupertinoPageRoute(
builder: (context) => CargoTypeEditor(
cargo: c,
)));
2021-09-10 16:48:21 +06:30
if (cargo == null) return;
2020-10-14 01:51:53 +06:30
_addCargo(cargo);
},
2020-10-13 07:50:25 +06:30
cells: [
2021-09-13 11:09:32 +06:30
DataCell(new Text(
2021-09-10 16:48:21 +06:30
c.name == null ? "" : c.name!,
2020-10-13 07:50:25 +06:30
style: textStyle,
)),
2021-09-13 11:09:32 +06:30
DataCell(
2020-10-13 07:50:25 +06:30
Row(
mainAxisAlignment: MainAxisAlignment.end,
children: [
2024-01-23 16:28:08 +06:30
Text(c.weight.toStringAsFixed(2), style: textStyle),
2020-10-13 07:50:25 +06:30
IconButton(
icon: Icon(
Icons.remove_circle,
color: primaryColor,
),
2020-10-14 01:51:53 +06:30
onPressed: () => {_removeCargo(c)},
2020-10-13 07:50:25 +06:30
)
],
),
),
],
);
}).toList();
2021-09-13 11:09:32 +06:30
var totalRow = DataRow(
onSelectChanged: (bool? selected) {},
2020-10-13 07:50:25 +06:30
cells: [
2021-09-13 11:09:32 +06:30
DataCell(Align(
2020-10-13 07:50:25 +06:30
alignment: Alignment.centerRight,
2020-10-15 17:33:43 +06:30
child: LocalText(
context,
"shipment.cargo.total",
color: Colors.black87,
fontWeight: FontWeight.bold,
),
2020-10-13 07:50:25 +06:30
)),
2021-09-13 11:09:32 +06:30
DataCell(
2020-10-13 07:50:25 +06:30
Padding(
2020-10-15 17:33:43 +06:30
padding: const EdgeInsets.only(right: 48.0),
2020-10-13 07:50:25 +06:30
child: Align(
alignment: Alignment.centerRight,
2020-12-02 20:55:00 +06:30
child: Text(total.toStringAsFixed(2),
2020-10-13 07:50:25 +06:30
style: TextStyle(fontWeight: FontWeight.bold))),
),
),
],
);
rows.add(totalRow);
return rows;
}
2020-10-14 01:51:53 +06:30
2024-01-23 16:28:08 +06:30
_addCargo(CargoType? cargo) {
2020-10-14 01:51:53 +06:30
if (cargo == null) return;
setState(() {
2021-09-10 16:48:21 +06:30
_box!.cargoTypes.remove(cargo);
_box!.cargoTypes.add(cargo);
2020-10-14 01:51:53 +06:30
});
}
2020-10-15 03:06:13 +06:30
_removeCargo(CargoType cargo) {
2020-10-14 01:51:53 +06:30
setState(() {
2021-09-10 16:48:21 +06:30
_box!.cargoTypes.remove(cargo);
2020-10-14 01:51:53 +06:30
});
}
2020-10-16 10:58:31 +06:30
_creatCarton() {
2024-01-09 13:11:22 +06:30
double l = double.parse(_lengthCtl.text);
double w = double.parse(_widthCtl.text);
double h = double.parse(_heightCtl.text);
2021-09-10 16:48:21 +06:30
_box!.length = l;
_box!.width = w;
_box!.height = h;
2020-10-16 10:58:31 +06:30
Navigator.pop(context, _box);
}
2020-10-13 07:50:25 +06:30
}