Files
fcs/lib/pages/rates/shipment_rates_calculate.dart

199 lines
6.4 KiB
Dart
Raw Normal View History

2020-10-18 02:38:46 +06:30
import 'package:fcs/domain/entities/carton.dart';
2020-10-16 10:58:31 +06:30
import 'package:fcs/domain/entities/cargo_type.dart';
import 'package:fcs/domain/entities/rate.dart';
2020-10-07 02:33:06 +06:30
import 'package:fcs/helpers/theme.dart';
import 'package:fcs/pages/main/util.dart';
2020-10-07 02:33:06 +06:30
import 'package:fcs/pages/rates/model/shipment_rate_model.dart';
2020-10-15 18:48:32 +06:30
import 'package:fcs/pages/widgets/display_text.dart';
2020-10-16 10:58:31 +06:30
import 'package:fcs/pages/widgets/input_text.dart';
2020-10-15 18:48:32 +06:30
import 'package:fcs/pages/widgets/length_picker.dart';
2020-10-20 18:45:18 +06:30
import 'package:fcs/pages/widgets/local_dropdown.dart';
2020-10-15 03:06:13 +06:30
import 'package:fcs/pages/widgets/local_text.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-16 10:58:31 +06:30
import 'package:flutter/material.dart';
2020-10-15 18:48:32 +06:30
import 'package:flutter_icons/flutter_icons.dart';
2020-05-31 15:00:11 +06:30
import 'package:provider/provider.dart';
class ShipmentRatesCal extends StatefulWidget {
2020-10-07 02:33:06 +06:30
ShipmentRatesCal();
2020-05-31 15:00:11 +06:30
@override
_ShipmentRatesCalState createState() => _ShipmentRatesCalState();
}
class _ShipmentRatesCalState extends State<ShipmentRatesCal> {
bool _isLoading = false;
2020-10-16 10:58:31 +06:30
CargoType _cargoType;
2020-10-15 18:48:32 +06:30
TextEditingController _widthController = new TextEditingController();
TextEditingController _heightController = new TextEditingController();
TextEditingController _lengthController = new TextEditingController();
2020-10-16 10:58:31 +06:30
TextEditingController _actualWeightCtl = new TextEditingController();
double _shipmentWeight = 0;
double _amount = 0;
double _deliveryFee = 0;
2020-05-31 15:00:11 +06:30
@override
void initState() {
super.initState();
2020-10-15 18:48:32 +06:30
//for shipment weight
2020-10-16 10:58:31 +06:30
Rate rate = Provider.of<ShipmentRateModel>(context, listen: false).rate;
_lengthController.addListener(_calShipmentWeight);
_widthController.addListener(_calShipmentWeight);
_heightController.addListener(_calShipmentWeight);
_actualWeightCtl.addListener(_calShipmentWeight);
_cargoType = rate.defaultCargoType;
_lengthController.text = '12';
_widthController.text = '12';
_heightController.text = '12';
_actualWeightCtl.text = "10.00";
2020-10-16 10:58:31 +06:30
_calShipmentWeight();
}
_calShipmentWeight() {
Rate rate = Provider.of<ShipmentRateModel>(context, listen: false).rate;
double l = double.parse(_lengthController.text, (s) => 0);
double w = double.parse(_widthController.text, (s) => 0);
double h = double.parse(_heightController.text, (s) => 0);
_cargoType.weight = double.tryParse(_actualWeightCtl.text) ?? 0;
2020-10-18 02:38:46 +06:30
Carton box =
Carton(cargoTypes: [_cargoType], length: l, width: w, height: h);
2020-10-16 10:58:31 +06:30
var amount = box.calAmount(rate);
var shipmentWeight = box.getShipmentWeight(rate.volumetricRatio);
var effectiveWeight =
_cargoType.weight > shipmentWeight ? _cargoType.weight : shipmentWeight;
setState(() {
_deliveryFee =
effectiveWeight > rate.freeDeliveryWeight ? 0 : rate.deliveryFee;
_amount = amount == null ? 0 : amount + _deliveryFee;
2021-01-04 17:19:01 +06:30
_shipmentWeight = shipmentWeight.toDouble();
2020-10-16 10:58:31 +06:30
});
2020-05-31 15:00:11 +06:30
}
@override
void dispose() {
super.dispose();
}
@override
Widget build(BuildContext context) {
var shipmentRateModel = Provider.of<ShipmentRateModel>(context);
2020-10-20 18:45:18 +06:30
List<CargoType> cargos = shipmentRateModel.rate.cargoTypes;
2020-06-25 16:19:23 +06:30
2020-10-15 18:48:32 +06:30
final lengthBox = LengthPicker(
controller: _lengthController,
lableKey: "box.length",
);
final widthBox = LengthPicker(
controller: _widthController,
lableKey: "box.width",
);
final heightBox = LengthPicker(
controller: _heightController,
lableKey: "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 shipmentWeightBox = DisplayText(
2020-10-16 10:58:31 +06:30
text: _shipmentWeight != null ? _shipmentWeight.toStringAsFixed(2) : "0",
2020-10-15 18:48:32 +06:30
labelTextKey: "box.shipment_weight",
iconData: MaterialCommunityIcons.weight,
);
2020-10-16 10:58:31 +06:30
final actualWeightBox = InputText(
controller: _actualWeightCtl,
2020-10-15 18:48:32 +06:30
labelTextKey: "box.actual_weight",
iconData: MaterialCommunityIcons.weight,
2020-10-16 10:58:31 +06:30
textInputType: TextInputType.number,
2020-10-15 18:48:32 +06:30
);
2020-10-20 18:45:18 +06:30
var cargoTypeBox = LocalDropdown<CargoType>(
callback: (v) {
setState(() {
_cargoType = v;
2020-10-28 06:01:36 +06:30
_calShipmentWeight();
2020-10-20 18:45:18 +06:30
});
},
labelKey: "cargo.type",
iconData: Icons.text_format,
selectedValue: _cargoType,
values: cargos,
);
2020-05-31 15:00:11 +06:30
return LocalProgress(
inAsyncCall: _isLoading,
child: Scaffold(
appBar: AppBar(
centerTitle: true,
2020-06-02 15:21:26 +06:30
leading: new IconButton(
2020-10-15 03:06:13 +06:30
icon: new Icon(CupertinoIcons.back, color: primaryColor),
onPressed: () {
2021-01-04 17:19:01 +06:30
Navigator.of(context).pop();
},
2020-06-02 15:21:26 +06:30
),
2020-10-15 03:06:13 +06:30
backgroundColor: Colors.white,
shadowColor: Colors.transparent,
title: LocalText(context, 'rate.cal.title',
color: primaryColor, fontSize: 20),
2020-05-31 15:00:11 +06:30
),
body: Padding(
padding: const EdgeInsets.all(8.0),
child: ListView(
children: <Widget>[
2020-10-20 18:45:18 +06:30
cargoTypeBox,
2020-10-16 10:58:31 +06:30
actualWeightBox,
2020-10-15 18:48:32 +06:30
dimBox,
shipmentWeightBox,
2020-05-31 15:00:11 +06:30
SizedBox(height: 50),
2020-10-16 10:58:31 +06:30
Row(
mainAxisAlignment: MainAxisAlignment.center,
children: [
LocalText(context, "rate.delivery_fee",
color: primaryColor, fontSize: 16),
Text(
':\$ ${_deliveryFee.toStringAsFixed(2)}',
2020-10-16 10:58:31 +06:30
style: TextStyle(
color: primaryColor,
fontSize: 16,
),
)
],
),
2020-05-31 15:00:11 +06:30
SizedBox(height: 20),
2020-10-16 10:58:31 +06:30
Row(
mainAxisAlignment: MainAxisAlignment.center,
children: [
LocalText(context, "rate.total_estimated_amount",
color: primaryColor, fontSize: 16),
Text(
':\$${_amount.toStringAsFixed(2)}',
style: TextStyle(
color: primaryColor,
fontSize: 16,
),
)
],
)
2020-05-31 15:00:11 +06:30
],
),
),
),
);
}
}