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

282 lines
8.5 KiB
Dart
Raw Normal View History

2020-10-13 07:50:25 +06:30
import 'package:fcs/domain/entities/box.dart';
import 'package:fcs/domain/entities/cargo.dart';
import 'package:fcs/domain/vo/delivery_address.dart';
import 'package:fcs/helpers/theme.dart';
import 'package:fcs/pages/box/cargo_type_editor.dart';
import 'package:fcs/pages/delivery_address/model/delivery_address_model.dart';
import 'package:fcs/pages/main/model/main_model.dart';
import 'package:fcs/pages/widgets/bottom_up_page_route.dart';
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/my_data_table.dart';
import 'package:fcs/pages/widgets/progress.dart';
import 'package:flutter/material.dart';
import 'package:flutter_icons/flutter_icons.dart';
import 'package:provider/provider.dart';
class ShipmentBoxEditor extends StatefulWidget {
final Box box;
ShipmentBoxEditor({this.box});
@override
_ShipmentBoxEditorState createState() => _ShipmentBoxEditorState();
}
class _ShipmentBoxEditorState extends State<ShipmentBoxEditor> {
TextEditingController _lengthCtl = new TextEditingController();
TextEditingController _widthCtl = new TextEditingController();
TextEditingController _heightCtl = new TextEditingController();
Box _box;
bool _isLoading = false;
bool _isNew;
2020-10-14 01:51:53 +06:30
DeliveryAddress _deliveryAddress;
double volumetricRatio = 0;
double shipmentWeight = 0;
List<Cargo> cargos = [];
2020-10-13 07:50:25 +06:30
@override
void initState() {
super.initState();
volumetricRatio =
Provider.of<MainModel>(context, listen: false).setting.volumetricRatio;
if (widget.box != null) {
_box = widget.box;
_deliveryAddress = _box.shippingAddress;
_isNew = false;
} else {
var shipmentModel =
Provider.of<DeliveryAddressModel>(context, listen: false);
_deliveryAddress = shipmentModel.defalutAddress;
_isNew = true;
_box = Box();
2020-10-14 01:51:53 +06:30
_lengthCtl.text = "0";
_widthCtl.text = "0";
_heightCtl.text = "0";
2020-10-13 07:50:25 +06:30
}
_lengthCtl.addListener(_calShipmentWeight);
_widthCtl.addListener(_calShipmentWeight);
_heightCtl.addListener(_calShipmentWeight);
}
_calShipmentWeight() {
double l = double.parse(_lengthCtl.text, (s) => 0);
double w = double.parse(_widthCtl.text, (s) => 0);
double h = double.parse(_heightCtl.text, (s) => 0);
2020-10-14 01:51:53 +06:30
print("$l $w $h");
2020-10-13 07:50:25 +06:30
setState(() {
shipmentWeight = l * w * h / volumetricRatio;
});
}
@override
Widget build(BuildContext context) {
final shipmentWeightBox = DisplayText(
labelTextKey: "shipment.box.shipment.weight",
text: shipmentWeight == null ? "" : shipmentWeight.toStringAsFixed(0),
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",
);
return LocalProgress(
inAsyncCall: _isLoading,
child: Scaffold(
appBar: AppBar(
centerTitle: true,
leading: new IconButton(
icon: new Icon(
Icons.close,
color: primaryColor,
),
onPressed: () => Navigator.of(context).pop(),
),
shadowColor: Colors.transparent,
backgroundColor: Colors.white,
title: LocalText(
context,
_isNew ? "boxes.new" : "box.edit.title",
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 {
Cargo cargo = await Navigator.push<Cargo>(
context, BottomUpPageRoute(CargoTypeEditor()));
_addCargo(cargo);
}),
),
2020-10-13 07:50:25 +06:30
MyDataTable(
headingRowHeight: 40,
columns: [
MyDataColumn(
label: LocalText(
context,
"cargo.type",
color: Colors.grey,
),
),
MyDataColumn(
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(
deliveryAddress: _deliveryAddress,
onTap: () async {
DeliveryAddress d = await Navigator.push<DeliveryAddress>(
context,
BottomUpPageRoute(DeliveryAddressSelection(
deliveryAddress: _deliveryAddress,
)),
);
if (d == null) return;
setState(() {
this._deliveryAddress = d;
});
}),
createBtn
],
),
),
),
);
}
List<MyDataRow> getCargoRows(BuildContext context) {
if (cargos == null || cargos == null) {
return [];
}
int total = 0;
var rows = cargos.map((c) {
total += c.weight;
return MyDataRow(
2020-10-14 01:51:53 +06:30
onSelectChanged: (bool selected) async {
Cargo cargo = await Navigator.push<Cargo>(
context,
BottomUpPageRoute(CargoTypeEditor(
cargo: c,
)));
_addCargo(cargo);
},
2020-10-13 07:50:25 +06:30
cells: [
MyDataCell(new Text(
c.type == null ? "" : c.type,
style: textStyle,
)),
MyDataCell(
Row(
mainAxisAlignment: MainAxisAlignment.end,
children: [
Text(c.weight == null ? "0" : c.weight.toString(),
style: textStyle),
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();
var totalRow = MyDataRow(
onSelectChanged: (bool selected) {},
cells: [
MyDataCell(Align(
alignment: Alignment.centerRight,
2020-10-14 01:51:53 +06:30
child: Text("Total",
2020-10-13 07:50:25 +06:30
style: TextStyle(
fontWeight: FontWeight.bold,
)),
)),
MyDataCell(
Padding(
padding: const EdgeInsets.only(right: 45.0),
child: Align(
alignment: Alignment.centerRight,
child: Text(total.toString(),
style: TextStyle(fontWeight: FontWeight.bold))),
),
),
],
);
rows.add(totalRow);
return rows;
}
2020-10-14 01:51:53 +06:30
_addCargo(Cargo cargo) {
if (cargo == null) return;
setState(() {
cargos.remove(cargo);
cargos.add(cargo);
});
}
_removeCargo(Cargo cargo) {
setState(() {
cargos.remove(cargo);
});
}
2020-10-13 07:50:25 +06:30
}