Learn Courses My Dashboard

Conditional Navigation

Hi,
I have three views. Starts with a LoginView() that looks like this:

When user inputs the username as ‘Admin’ navigation goes to AdminView(), if user inputs ‘Player’ navigation goes to PlayerView().

LoginView() has:

struct LoginView: View {
    
    @State var userName:String = ""
    
    var body: some View {
        NavigationView {
            VStack {
                Text("Hello World")
                
                TextField("Enter username", text: $userName)
                    .padding([.trailing, .leading], 100)
                
                if userName == "Admin" {
                    NavigationLink(
                        destination: AdminView()) {
                        Text("Enter")
                    }
                }
                else if userName == "Player"{
                    NavigationLink(
                        destination: PlayerView()){
                        Text("Enter")
                    }
                }
                else{
                    Button("Enter"){
                    }
                }
            }
        }
    }
}

AdminView() has:

struct AdminView: View {
    var body: some View {
        Text("ADMIN VIEW")
            .navigationBarBackButtonHidden(true)
    }
}

PlayerView() is similar to AdminView, just has ‘PLAYER VIEW’ as text instead.

Though it works but I feel that there would be a more elegant way to implement LoginView().

Request you to please share your thoughts.

EmptyView() helped for now.