// ignore_for_file: deprecated_member_use import 'package:fcs/pages/fcs_shipment/model/fcs_shipment_model.dart'; import 'package:flutter/cupertino.dart'; import 'package:flutter/material.dart'; import 'package:intl/intl.dart'; import 'package:provider/provider.dart'; import '../../../constants.dart'; import '../../../domain/entities/carton.dart'; import '../../../domain/entities/carton_size.dart'; import '../../../domain/entities/fcs_shipment.dart'; import '../../../domain/vo/local_step.dart'; import '../../../helpers/theme.dart'; import '../../carton_size/model/carton_size_model.dart'; import '../../main/util.dart'; import '../../widgets/local_text.dart'; import '../../widgets/progress.dart'; import '../../widgets/step_widget.dart'; import '../model/carton_model.dart'; import '../model/carton_selection_model.dart'; import 'carton_selection_widget.dart'; import 'mix_carton_submit.dart'; import 'type_widget.dart'; class MixCartonEditor extends StatefulWidget { final Carton carton; const MixCartonEditor({Key? key, required this.carton}) : super(key: key); @override State createState() => _MixCartonEditorState(); } class _MixCartonEditorState extends State { var dateFormatter = DateFormat('dd MMM yyyy'); final NumberFormat numberFormatter = NumberFormat("#,###"); List steps = [ LocalStep(lable: 'Type', stepType: StepType.TYPE), LocalStep(lable: 'Cartons', stepType: StepType.CARTONS), LocalStep(lable: 'Submit', stepType: StepType.SUBMIT) ]; List _cartons = []; int currentStep = 0; double _length = 0; double _width = 0; double _height = 0; FcsShipment? _shipment; String _cartonSizeType = standardCarton; CartonSize? _standardSize; bool _isLoading = false; @override void initState() { _init(); super.initState(); } _init() async { context.read().clearSelection(); // check carton size type List cartonSizes = context.read().cartonSizes; var sameLength = cartonSizes.any((size) => size.length == widget.carton.length); var sameWidth = cartonSizes.any((size) => size.width == widget.carton.width); var sameHeight = cartonSizes.any((size) => size.height == widget.carton.height); bool isStandartSize = sameLength && sameWidth && sameHeight; if (isStandartSize) { _cartonSizeType = standardCarton; _standardSize = cartonSizes.firstWhere((size) => size.length == widget.carton.length && size.width == widget.carton.width && size.height == widget.carton.height); } else if (widget.carton.length == 0 && widget.carton.width == 0 && widget.carton.height == 0) { _cartonSizeType = packageCarton; } else { _cartonSizeType = customCarton; _length = widget.carton.length.toDouble(); _width = widget.carton.width.toDouble(); _height = widget.carton.height.toDouble(); } var s = await context .read() .getFcsShipment(widget.carton.fcsShipmentID ?? ""); _shipment = s; _cartons = await context .read() .getCartonsByIds(widget.carton.cartonIDs); 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: Column( children: [ LocalText(context, 'box.update_title', color: primaryColor, fontSize: 20), Text( widget.carton.cartonNumber ?? '', style: TextStyle(color: primaryColor, fontSize: 14), ) ], ), ), body: Column( children: [ StepperWidget( labels: steps.map((e) => e.lable).toList(), currentStep: currentStep, eachStepWidth: MediaQuery.of(context).size.width / 3, onChange: (index) { if (index > currentStep) { return; } setState(() { currentStep = index; }); }, ), getContent(currentStep) ], ))), ); } Widget getContent(int index) { var step = steps[index]; if (step.stepType == StepType.TYPE) { return Expanded( child: TypeWidget( shipment: _shipment, cartonSizeType: _cartonSizeType, standardSize: _standardSize, length: _length, width: _width, height: _height, onPrevious: () { Navigator.pop(context); }, onContinue: (shipment, cartonSizeType, {standardSize, length, width, height}) { setState(() { _shipment = shipment; _cartonSizeType = cartonSizeType; _standardSize = standardSize; _length = length ?? 0; _width = width ?? 0; _height = height ?? 0; currentStep += 1; }); }, )); } else if (step.stepType == StepType.CARTONS) { return Expanded( child: CartonSelectionWidget( shipment: _shipment!, cartons: _cartons, onContinue: (cartons) { setState(() { _cartons = List.from(cartons); currentStep += 1; }); }, onPrevious: (cartons) { setState(() { _cartons = List.from(cartons); currentStep -= 1; }); }, ), ); } else { return Expanded( child: MixCartonSubmit( isNew: false, cartonSizeType: _cartonSizeType, standardSize: _standardSize, length: _length, width: _width, height: _height, shipment: _shipment!, cartons: _cartons, onCreate: () { _update(); }, onPrevious: () { setState(() { currentStep -= 1; }); }, ), ); } } _update() async { setState(() { _isLoading = true; }); try { double length = 0; double width = 0; double height = 0; if (_cartonSizeType == standardCarton) { if (_standardSize != null) { length = _standardSize!.length; width = _standardSize!.width; height = _standardSize!.height; } } else if (_cartonSizeType == customCarton) { length = _length; width = _width; height = _height; } else { length = 0; width = 0; height = 0; } var carton = Carton( id: widget.carton.id, cartonType: mix_carton, fcsShipmentID: _shipment?.id, length: length, width: width, height: height, cartons: _cartons); await context.read().updateMixCarton(carton); Navigator.pop(context, true); } catch (e) { showMsgDialog(context, "Error", e.toString()); } finally { setState(() { _isLoading = false; }); } } }