159 lines
4.9 KiB
Dart
159 lines
4.9 KiB
Dart
// ignore_for_file: use_build_context_synchronously
|
|
|
|
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/input_text.dart';
|
|
import 'package:fcs/pages/widgets/local_app_bar.dart';
|
|
import 'package:fcs/pages/widgets/progress.dart';
|
|
import 'package:flutter/material.dart';
|
|
import 'package:flutter_vector_icons/flutter_vector_icons.dart';
|
|
import 'package:font_awesome_flutter/font_awesome_flutter.dart';
|
|
import 'package:provider/provider.dart';
|
|
|
|
import '../main/util.dart';
|
|
|
|
class ShipmentRatesEdit extends StatefulWidget {
|
|
const ShipmentRatesEdit({super.key});
|
|
|
|
@override
|
|
_ShipmentRatesEditState createState() => _ShipmentRatesEditState();
|
|
}
|
|
|
|
class _ShipmentRatesEditState extends State<ShipmentRatesEdit> {
|
|
bool _isLoading = false;
|
|
TextEditingController minWeight = TextEditingController();
|
|
TextEditingController deliveryFee = TextEditingController();
|
|
TextEditingController volumetricRatio = TextEditingController();
|
|
TextEditingController diffDiscountWeight = TextEditingController();
|
|
TextEditingController diffWeightRate = TextEditingController();
|
|
|
|
late Rate rate;
|
|
|
|
@override
|
|
void initState() {
|
|
super.initState();
|
|
var shipmentRateModel =
|
|
Provider.of<ShipmentRateModel>(context, listen: false);
|
|
rate = shipmentRateModel.rate;
|
|
|
|
minWeight.text = rate.freeDeliveryWeight.toStringAsFixed(2);
|
|
deliveryFee.text = rate.deliveryFee.toStringAsFixed(2);
|
|
volumetricRatio.text = rate.volumetricRatio.toStringAsFixed(2);
|
|
diffDiscountWeight.text = rate.diffDiscountWeight.toStringAsFixed(2);
|
|
diffWeightRate.text = rate.diffWeightRate.toStringAsFixed(2);
|
|
}
|
|
|
|
@override
|
|
void dispose() {
|
|
super.dispose();
|
|
}
|
|
|
|
@override
|
|
Widget build(BuildContext context) {
|
|
final minWigBox = InputText(
|
|
labelTextKey: 'rate.min_weight',
|
|
iconData: FontAwesomeIcons.weightHanging,
|
|
controller: minWeight);
|
|
|
|
final feeBox = InputText(
|
|
labelTextKey: 'rate.delivery_fee',
|
|
iconData: Fontisto.dollar,
|
|
controller: deliveryFee);
|
|
|
|
final ratioBox = InputText(
|
|
labelTextKey: 'rate.volumetric_ratio',
|
|
iconData: FontAwesomeIcons.weightHanging,
|
|
controller: volumetricRatio);
|
|
|
|
final diffDiscountWeightBox = InputText(
|
|
labelTextKey: 'rate.diff_discount_weight',
|
|
iconData: FontAwesomeIcons.weightHanging,
|
|
controller: diffDiscountWeight);
|
|
|
|
final diffWeightRateBox = InputText(
|
|
labelTextKey: 'rate.diff_weight_rate',
|
|
iconData: Fontisto.dollar,
|
|
controller: diffWeightRate);
|
|
|
|
return LocalProgress(
|
|
inAsyncCall: _isLoading,
|
|
child: Scaffold(
|
|
appBar: LocalAppBar(
|
|
labelKey: "rate.edit.title",
|
|
backgroundColor: Colors.white,
|
|
labelColor: primaryColor,
|
|
arrowColor: primaryColor,
|
|
onBack: () {
|
|
if (isDataChanged()) {
|
|
showConfirmDialog(context, "back.button_confirm", () {
|
|
Navigator.of(context).pop();
|
|
});
|
|
} else {
|
|
Navigator.of(context).pop();
|
|
}
|
|
},
|
|
),
|
|
body: Container(
|
|
padding: EdgeInsets.all(18),
|
|
child: Column(
|
|
children: <Widget>[
|
|
Expanded(
|
|
child: ListView(
|
|
children: <Widget>[
|
|
minWigBox,
|
|
feeBox,
|
|
ratioBox,
|
|
diffDiscountWeightBox,
|
|
diffWeightRateBox,
|
|
SizedBox(height: 10),
|
|
],
|
|
),
|
|
),
|
|
fcsButton(context, getLocalString(context, "btn.save"),
|
|
callack: _save),
|
|
SizedBox(height: 30)
|
|
],
|
|
),
|
|
),
|
|
),
|
|
);
|
|
}
|
|
|
|
_save() async {
|
|
setState(() {
|
|
_isLoading = true;
|
|
});
|
|
try {
|
|
var shipmentRateModel =
|
|
Provider.of<ShipmentRateModel>(context, listen: false);
|
|
Rate rate = Rate(
|
|
deliveryFee: double.parse(deliveryFee.text),
|
|
freeDeliveryWeight: double.parse(minWeight.text),
|
|
volumetricRatio: double.parse(volumetricRatio.text),
|
|
diffDiscountWeight: double.parse(diffDiscountWeight.text),
|
|
diffWeightRate: double.parse(diffWeightRate.text));
|
|
|
|
await shipmentRateModel.updateRate(rate);
|
|
Navigator.pop(context);
|
|
} catch (e) {
|
|
showMsgDialog(context, "Error", e.toString());
|
|
} finally {
|
|
setState(() {
|
|
_isLoading = false;
|
|
});
|
|
}
|
|
}
|
|
|
|
isDataChanged() {
|
|
Rate rate = Rate(
|
|
deliveryFee: double.parse(deliveryFee.text),
|
|
freeDeliveryWeight: double.parse(minWeight.text),
|
|
volumetricRatio: double.parse(volumetricRatio.text),
|
|
diffDiscountWeight: double.parse(diffDiscountWeight.text),
|
|
diffWeightRate: double.parse(diffWeightRate.text),
|
|
);
|
|
return rate.isChangedForEdit(rate);
|
|
}
|
|
}
|