ios – How to navigate view in SwiftUI using .navigationDestination condition


I want to navigate view using .navigationDestionation using Condition. or .navigationDestination cant use like that ?

struct SuccessfulView: View {
    enum SuccessType {
        case verifyPhoneNumber
        case setPincode
    }
    
    let successType: SuccessType
    
    @State private var moveToSetPinCode = false
    @State private var moveToMainMenu = false
    
    var body: some View {
        VStack {
            Spacer()
            
            Image("checklist")
            
            Text("Congrats!")
                .font(.system(size: 55, weight: .bold))
                .foregroundStyle(Color("hanBlue"))
            
            Text(successMessage)
                .frame(width: 300, height: 42)
                .font(.system(size: 16, weight: .semibold))
                .foregroundStyle(Color("hanBlue"))
                .multilineTextAlignment(.center)
            
            Spacer()
            
            Button(action :{
                if successType != .setPincode {
                    moveToMainMenu = true
                } else {
                    moveToSetPinCode = true
                }
            }) {
                Text("Great!")
                    .foregroundStyle(Color.white)
                    .frame(width: 153, height: 60)
                    .background(Color("hanBlue"))
                    .clipShape(RoundedRectangle(cornerRadius: 20))
            }
            
            Spacer()
        }
    }
    
    private var successMessage: String {
        switch successType {
        case.verifyPhoneNumber:
            return "Verification has been done Successfully"
        case.setPincode:
            return "Account Registed Successfully"
        }
    }
}

VerifyPhoneNumberView.swift

struct VerifyPhoneNumberView: View {
    @State private var verifyCode = ""
    @State private var setVerify = false
    
    var body: some View {
        VStack {
            Text("Please add your mobile phone number")
                .font(.system(size: 16, weight: .medium))
                .foregroundStyle(Color.gray)
                .frame(width: 165, height: 52)
                .multilineTextAlignment(.center)
            
            Spacer()
            
            Text("Enter Verification Code (5-digit)")
                .font(.system(size: 12, weight: .regular))
                .foregroundStyle(Color.gray)
                .frame(maxWidth: .infinity, alignment: .leading)
                .padding(.leading, 33)
            
            UnderlineTextFieldMain(placeholder: "56234", text: $verifyCode)
                .padding(.horizontal)
                .padding(.bottom)
                .padding(.bottom)
            
            Button {
                setVerify = true
            } label: {
                Text("Verify")
                    .font(.system(size: 22, weight: .medium))
                    .foregroundStyle(Color.white)
                    .frame(width: 153, height: 59, alignment: .center)
                    .background(Color("hanBlue"))
                    .clipShape(RoundedRectangle(cornerRadius: 20))
                    .padding(.top)
                    .padding(.top)
            }
            .navigationDestination(isPresented: $setVerify, destination: {
                SuccessfulView(successType: .verifyPhoneNumber)
            })
            .buttonStyle(PlainButtonStyle())
            
            Spacer()
        }
        .navigationTitle("Verify your Number")
    }
}

I want my SuccessfulView navigate using condition.

Example Flow :
VerifyPhoneNumberView > SuccessfulView(.verifyPhoneNumber) > SetPinCodeView > SuccessfulView(.setPinCode) > otherView.

Latest articles

spot_imgspot_img

Related articles

Leave a reply

Please enter your comment!
Please enter your name here

spot_imgspot_img