import 'package:fcs/domain/entities/carton.dart'; import 'package:fcs/domain/entities/cargo_type.dart'; import 'package:fcs/domain/entities/rate.dart'; import 'package:fcs/helpers/theme.dart'; import 'package:fcs/pages/rates/model/shipment_rate_model.dart'; import 'package:fcs/pages/widgets/display_text.dart'; import 'package:fcs/pages/widgets/input_text.dart'; import 'package:fcs/pages/widgets/length_picker.dart'; import 'package:fcs/pages/widgets/local_dropdown.dart'; import 'package:fcs/pages/widgets/local_text.dart'; import 'package:fcs/pages/widgets/progress.dart'; import 'package:flutter/cupertino.dart'; import 'package:flutter/material.dart'; import 'package:flutter_icons_null_safety/flutter_icons_null_safety.dart'; import 'package:provider/provider.dart'; class ShipmentRatesCal extends StatefulWidget { ShipmentRatesCal(); @override _ShipmentRatesCalState createState() => _ShipmentRatesCalState(); } class _ShipmentRatesCalState extends State { bool _isLoading = false; late CargoType _cargoType; TextEditingController _widthController = new TextEditingController(); TextEditingController _heightController = new TextEditingController(); TextEditingController _lengthController = new TextEditingController(); TextEditingController _actualWeightCtl = new TextEditingController(); double _shipmentWeight = 0; double _amount = 0; double _deliveryFee = 0; @override void initState() { super.initState(); //for shipment weight Rate rate = Provider.of(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"; _calShipmentWeight(); } _calShipmentWeight() { Rate rate = Provider.of(context, listen: false).rate; double l = double.parse(_lengthController.text); double w = double.parse(_widthController.text); double h = double.parse(_heightController.text); _cargoType.weight = double.tryParse(_actualWeightCtl.text) ?? 0; Carton box = Carton(cargoTypes: [_cargoType], length: l, width: w, height: h); 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 + _deliveryFee; _shipmentWeight = shipmentWeight.toDouble(); }); } @override void dispose() { super.dispose(); } @override Widget build(BuildContext context) { var shipmentRateModel = Provider.of(context); List cargos = shipmentRateModel.rate.cargoTypes; 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( text: _shipmentWeight.toStringAsFixed(2), labelTextKey: "box.shipment_weight", iconData: MaterialCommunityIcons.weight, ); final actualWeightBox = InputText( controller: _actualWeightCtl, labelTextKey: "box.actual_weight", iconData: MaterialCommunityIcons.weight, textInputType: TextInputType.numberWithOptions(decimal: true), ); var cargoTypeBox = LocalDropdown( callback: (v) { setState(() { _cargoType = v; _calShipmentWeight(); }); }, labelKey: "cargo.type", iconData: Icons.text_format, selectedValue: _cargoType, values: cargos, ); 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(); }, ), backgroundColor: Colors.white, shadowColor: Colors.transparent, title: LocalText(context, 'rate.cal.title', color: primaryColor, fontSize: 20), ), body: Padding( padding: const EdgeInsets.all(8.0), child: ListView( children: [ cargoTypeBox, actualWeightBox, dimBox, shipmentWeightBox, SizedBox(height: 50), Row( mainAxisAlignment: MainAxisAlignment.center, children: [ LocalText(context, "rate.delivery_fee", color: primaryColor, fontSize: 16), Text( ':\$ ${_deliveryFee.toStringAsFixed(2)}', style: TextStyle( color: primaryColor, fontSize: 16, ), ) ], ), SizedBox(height: 20), 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, ), ) ], ) ], ), ), ), ); } }