Files
fcs/lib/pages/shipment/shipment_box_editor.dart
2021-01-07 18:15:39 +06:30

304 lines
9.4 KiB
Dart

import 'package:fcs/domain/entities/carton.dart';
import 'package:fcs/domain/entities/cargo_type.dart';
import 'package:fcs/domain/vo/delivery_address.dart';
import 'package:fcs/helpers/theme.dart';
import 'package:fcs/pages/carton/cargo_type_editor.dart';
import 'package:fcs/pages/delivery_address/model/delivery_address_model.dart';
import 'package:fcs/pages/rates/model/shipment_rate_model.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';
import 'package:fcs/pages/widgets/length_picker.dart';
import 'package:fcs/pages/widgets/local_button.dart';
import 'package:fcs/pages/widgets/local_text.dart';
import 'package:fcs/pages/widgets/local_title.dart';
import 'package:fcs/pages/widgets/my_data_table.dart';
import 'package:fcs/pages/widgets/progress.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_icons/flutter_icons.dart';
import 'package:provider/provider.dart';
class ShipmentBoxEditor extends StatefulWidget {
final Carton 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();
Carton _box;
bool _isLoading = false;
bool _isNew;
double volumetricRatio = 0;
double shipmentWeight = 0;
@override
void initState() {
super.initState();
volumetricRatio = Provider.of<ShipmentRateModel>(context, listen: false)
.rate
.volumetricRatio;
if (widget.box != null) {
_box = widget.box;
_isNew = false;
_lengthCtl.text = _box.length.toString();
_widthCtl.text = _box.width.toString();
_heightCtl.text = _box.height.toString();
} else {
var shipmentModel =
Provider.of<DeliveryAddressModel>(context, listen: false);
_isNew = true;
_box = Carton(cargoTypes: []);
_box.deliveryAddress = shipmentModel.defalutAddress;
_lengthCtl.text = "12";
_widthCtl.text = "12";
_heightCtl.text = "12";
}
_lengthCtl.addListener(_calShipmentWeight);
_widthCtl.addListener(_calShipmentWeight);
_heightCtl.addListener(_calShipmentWeight);
_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);
setState(() {
shipmentWeight = (l * w * h / volumetricRatio).ceilToDouble();
});
}
@override
Widget build(BuildContext context) {
var deliveryAddressModel = Provider.of<DeliveryAddressModel>(context);
final shipmentWeightBox = DisplayText(
labelTextKey: "shipment.box.shipment.weight",
text: shipmentWeight == null ? "" : shipmentWeight.toStringAsFixed(0),
iconData: MaterialCommunityIcons.weight,
);
final lengthBox = LengthPicker(
controller: _lengthCtl,
lableKey: "shipment.box.length",
);
final widthBox = LengthPicker(
controller: _widthCtl,
lableKey: "shipment.box.width",
);
final heightBox = LengthPicker(
controller: _heightCtl,
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),
],
);
final createBtn = LocalButton(
textKey: "shipment.box.add",
callBack: _creatCarton,
);
return LocalProgress(
inAsyncCall: _isLoading,
child: Scaffold(
appBar: AppBar(
centerTitle: true,
leading: new IconButton(
icon: new Icon(
CupertinoIcons.back,
color: primaryColor,
),
onPressed: () => Navigator.of(context).pop(),
),
shadowColor: Colors.transparent,
backgroundColor: Colors.white,
title: LocalText(
context,
_isNew ? "boxes.create.title" : "box.edit.title",
fontSize: 20,
color: primaryColor,
),
),
body: Padding(
padding: const EdgeInsets.all(8.0),
child: ListView(
children: [
LocalTitle(
textKey: "shipment.box.cargo.type",
trailing: IconButton(
icon: Icon(
Icons.add_circle,
color: primaryColor,
),
onPressed: () async {
CargoType cargo = await Navigator.push<CargoType>(
context,
CupertinoPageRoute(
builder: (context) => CargoTypeEditor()));
_addCargo(cargo);
}),
),
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,
),
LocalTitle(textKey: "shipment.box.dimemsion"),
dimBox,
shipmentWeightBox,
LocalTitle(textKey: "shipment.box.delivery"),
DefaultDeliveryAddress(
deliveryAddress: _box.deliveryAddress,
labelKey: "shipment.box.delivery",
onTap: () async {
DeliveryAddress d = await Navigator.push<DeliveryAddress>(
context,
CupertinoPageRoute(
builder: (context) => DeliveryAddressSelection(
deliveryAddress: _box.deliveryAddress,
deliveryAddresses:
deliveryAddressModel.deliveryAddresses)),
);
if (d == null) return;
setState(() {
_box.deliveryAddress = d;
});
}),
createBtn
],
),
),
),
);
}
List<MyDataRow> getCargoRows(BuildContext context) {
if (_box.cargoTypes == null) {
return [];
}
double total = 0;
var rows = _box.cargoTypes.map((c) {
total += c.weight;
return MyDataRow(
onSelectChanged: (bool selected) async {
CargoType cargo = await Navigator.push<CargoType>(
context,
CupertinoPageRoute(
builder: (context) => CargoTypeEditor(
cargo: c,
)));
_addCargo(cargo);
},
cells: [
MyDataCell(new Text(
c.name == null ? "" : c.name,
style: textStyle,
)),
MyDataCell(
Row(
mainAxisAlignment: MainAxisAlignment.end,
children: [
Text(c.weight == null ? "0" : c.weight.toStringAsFixed(2),
style: textStyle),
IconButton(
icon: Icon(
Icons.remove_circle,
color: primaryColor,
),
onPressed: () => {_removeCargo(c)},
)
],
),
),
],
);
}).toList();
var totalRow = MyDataRow(
onSelectChanged: (bool selected) {},
cells: [
MyDataCell(Align(
alignment: Alignment.centerRight,
child: LocalText(
context,
"shipment.cargo.total",
color: Colors.black87,
fontWeight: FontWeight.bold,
),
)),
MyDataCell(
Padding(
padding: const EdgeInsets.only(right: 48.0),
child: Align(
alignment: Alignment.centerRight,
child: Text(total.toStringAsFixed(2),
style: TextStyle(fontWeight: FontWeight.bold))),
),
),
],
);
rows.add(totalRow);
return rows;
}
_addCargo(CargoType cargo) {
if (cargo == null) return;
setState(() {
_box.cargoTypes.remove(cargo);
_box.cargoTypes.add(cargo);
});
}
_removeCargo(CargoType cargo) {
setState(() {
_box.cargoTypes.remove(cargo);
});
}
_creatCarton() {
double l = double.parse(_lengthCtl.text, (s) => 0);
double w = double.parse(_widthCtl.text, (s) => 0);
double h = double.parse(_heightCtl.text, (s) => 0);
_box.length = l;
_box.width = w;
_box.height = h;
Navigator.pop(context, _box);
}
}