Files
fcs/lib/pages/rates/shipment_rates_calculate.dart
2025-03-12 17:49:27 +06:30

191 lines
6.1 KiB
Dart

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_app_bar.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/material.dart';
import 'package:flutter_vector_icons/flutter_vector_icons.dart';
import 'package:provider/provider.dart';
class ShipmentRatesCal extends StatefulWidget {
const ShipmentRatesCal({super.key});
@override
_ShipmentRatesCalState createState() => _ShipmentRatesCalState();
}
class _ShipmentRatesCalState extends State<ShipmentRatesCal> {
bool isLoading = false;
late CargoType _cargoType;
TextEditingController widthController = TextEditingController();
TextEditingController heightController = TextEditingController();
TextEditingController lengthController = TextEditingController();
TextEditingController actualWeightCtl = TextEditingController();
double _shipmentWeight = 0;
double _amount = 0;
double _deliveryFee = 0;
@override
void initState() {
super.initState();
//for shipment weight
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";
_calShipmentWeight();
}
_calShipmentWeight() {
Rate rate = Provider.of<ShipmentRateModel>(context, listen: false).rate;
double l = double.tryParse(lengthController.text) ?? 0;
double w = double.tryParse(widthController.text) ?? 0;
double h = double.tryParse(heightController.text) ?? 0;
_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<ShipmentRateModel>(context);
List<CargoType> 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(width: 80, child: lengthBox),
SizedBox(width: 80, child: widthBox),
SizedBox(width: 80, child: heightBox),
],
);
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<CargoType>(
callback: (v) {
setState(() {
_cargoType = v;
_calShipmentWeight();
});
},
labelKey: "cargo.type",
iconData: Ionicons.text,
selectedValue: _cargoType,
values: cargos,
);
return LocalProgress(
inAsyncCall: isLoading,
child: Scaffold(
appBar: LocalAppBar(
labelKey: "rate.cal.title",
backgroundColor: Colors.white,
labelColor: primaryColor,
arrowColor: primaryColor),
body: Padding(
padding: const EdgeInsets.all(8.0),
child: ListView(
children: <Widget>[
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,
),
)
],
)
],
),
),
),
);
}
}