Files
fcs/lib/pages/carton/carton_package_editor.dart
2024-02-09 17:40:51 +06:30

290 lines
8.5 KiB
Dart

// ignore_for_file: deprecated_member_use
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:intl/intl.dart';
import 'package:provider/provider.dart';
import '../../../domain/constants.dart';
import '../../../domain/entities/carton_size.dart';
import '../../../domain/entities/fcs_shipment.dart';
import '../../../domain/vo/local_step.dart';
import '../../../helpers/theme.dart';
import '../../domain/entities/cargo_type.dart';
import '../../domain/entities/carton.dart';
import '../../domain/entities/package.dart';
import '../../domain/entities/user.dart';
import '../fcs_shipment/model/fcs_shipment_model.dart';
import '../main/util.dart';
import '../package/model/package_model.dart';
import '../widgets/local_text.dart';
import '../widgets/progress.dart';
import '../widgets/step_widget.dart';
import 'cargo_widget.dart';
import 'carton_size_widget.dart';
import 'carton_submit.dart';
import 'model/package_selection_model.dart';
import 'package_selection_widget.dart';
class CartonPackageEditor extends StatefulWidget {
final Carton carton;
const CartonPackageEditor({Key? key, required this.carton}) : super(key: key);
@override
State<CartonPackageEditor> createState() => _CartonPackageEditorState();
}
class _CartonPackageEditorState extends State<CartonPackageEditor> {
var dateFormatter = DateFormat('dd MMM yyyy');
final NumberFormat numberFormatter = NumberFormat("#,###");
List<LocalStep> steps = [
LocalStep(lable: 'Size', stepType: StepType.SIZE),
LocalStep(lable: 'Packages', stepType: StepType.PACKAGES),
LocalStep(lable: 'Cargos', stepType: StepType.CARGOS),
LocalStep(lable: 'Submit', stepType: StepType.SUBMIT)
];
List<Package> _packages = [];
List<CargoType> _cargoTypes = [];
List<CargoType> _surchareItems = [];
int currentStep = 0;
double _length = 0;
double _width = 0;
double _height = 0;
String _selectedDeliveryType = delivery_caton;
String _billToValue = billToSender;
FcsShipment? _shipment;
String _cartonSizeType = standardCarton;
CartonSize? _standardSize;
bool _isLoading = false;
User? _consignee;
User? _sender;
@override
void initState() {
_init();
super.initState();
}
_init() async {
context.read<PackageSelectionModel>().clearSelection();
_sender = User(
name: widget.carton.senderName,
fcsID: widget.carton.senderFCSID,
id: widget.carton.senderID);
_consignee = User(
id: widget.carton.userID,
name: widget.carton.userName,
fcsID: widget.carton.fcsID);
_billToValue = widget.carton.billTo ?? billToSender;
_selectedDeliveryType = widget.carton.deliveryType ?? delivery_caton;
_cartonSizeType = widget.carton.cartonSizeType ?? customCarton;
_length = widget.carton.length;
_width = widget.carton.width;
_height = widget.carton.height;
_cargoTypes = widget.carton.cargoTypes.where((e) => !e.isCutomDuty).toList();
_surchareItems = widget.carton.cargoTypes.where((e) => e.isCutomDuty).toList();
var s = await context
.read<FcsShipmentModel>()
.getFcsShipment(widget.carton.fcsShipmentID ?? "");
_shipment = s;
_packages = await context
.read<PackageModel>()
.getPackagesByIds(widget.carton.packageIDs);
if (mounted) {
setState(() {});
}
}
@override
Widget build(BuildContext context) {
return WillPopScope(
onWillPop: () {
if (currentStep == 0) {
Navigator.of(context).pop();
}
if (currentStep > 0) {
setState(() {
currentStep -= 1;
});
}
return Future.value(false);
},
child: LocalProgress(
inAsyncCall: _isLoading,
child: Scaffold(
appBar: AppBar(
elevation: 0,
centerTitle: true,
leading: IconButton(
icon: const Icon(CupertinoIcons.back,
color: primaryColor, size: 25),
onPressed: () {
if (currentStep == 0) {
Navigator.of(context).pop();
}
if (currentStep > 0) {
setState(() {
currentStep -= 1;
});
}
},
),
backgroundColor: Colors.white,
title: LocalText(context, 'box.update_title',
color: primaryColor, fontSize: 20),
),
body: Column(
children: [
StepperWidget(
labels: steps.map((e) => e.lable).toList(),
currentStep: currentStep,
eachStepWidth: MediaQuery.of(context).size.width / 4,
onChange: (index) {
if (index > currentStep) {
return;
}
setState(() {
currentStep = index;
});
},
),
getContent(currentStep)
],
))),
);
}
Widget getContent(int index) {
var step = steps[index];
if (step.stepType == StepType.SIZE) {
return Expanded(
child: CartonSizeWidget(
deliveryType: _selectedDeliveryType,
billType: _billToValue,
sender: _sender!,
consignee: _consignee!,
shipment: _shipment,
cartonSizeType: _cartonSizeType,
standardSize: _standardSize,
length: _length,
width: _width,
height: _height,
onPrevious: () {
Navigator.pop(context);
},
onContinue: (deliveryType, billType, shipment, cartonSizeType,
{standardSize, length, width, height}) {
setState(() {
_selectedDeliveryType = deliveryType;
_billToValue = billType;
_shipment = shipment;
_cartonSizeType = cartonSizeType;
_standardSize = standardSize;
_length = length ?? 0;
_width = width ?? 0;
_height = height ?? 0;
currentStep += 1;
});
},
));
} else if (step.stepType == StepType.PACKAGES) {
return Expanded(
child: PackageSelectionWidget(
sender: _sender!,
consignee: _consignee!,
shipment: _shipment!,
packages: _packages,
onContinue: (packages) {
setState(() {
_packages = List.from(packages);
currentStep += 1;
});
},
onPrevious: (packages) {
setState(() {
_packages = List.from(packages);
currentStep -= 1;
});
},
),
);
} else if (step.stepType == StepType.CARGOS) {
return Expanded(
child: CargoWidget(
sender: _sender!,
consignee: _consignee!,
cargoTypes: _cargoTypes,
surchargeItems: _surchareItems,
onContinue: (cargoTypes, customDuties) {
setState(() {
_cargoTypes = List.from(cargoTypes);
_surchareItems = List.from(customDuties);
currentStep += 1;
});
},
onPrevious: (cargoTypes, customDuties) {
setState(() {
_cargoTypes = List.from(cargoTypes);
_surchareItems = List.from(customDuties);
currentStep -= 1;
});
},
),
);
} else {
return Expanded(
child: CartonSubmit(
isNew: false,
sender: _sender!,
consingee: _consignee!,
billToValue: _billToValue,
cartonSizeType: _cartonSizeType,
standardSize: _standardSize,
length: _length,
width: _width,
height: _height,
deliveryType: _selectedDeliveryType,
shipment: _shipment!,
packages: _packages,
cargoTypes: _cargoTypes,
surchareItems: _surchareItems,
onCreate: () {
_create();
},
onPrevious: () {
setState(() {
currentStep -= 1;
});
},
),
);
}
}
_create() async {
setState(() {
_isLoading = true;
});
try {
Navigator.pop(context, true);
} catch (e) {
showMsgDialog(context, "Error", e.toString());
} finally {
setState(() {
_isLoading = false;
});
}
}
}