open multiple sheet above others using item

74 Views Asked by At

in my app I'm using sheet with item, because I need to open multiple sheet in the view in normal behavior everything is working fine as I only need one sheet once a time

but in one page I can open same view via sheet, so when I open other sheet it will close the other one

I want to have dynamic sheet using item so if the sheet is open it will open other one on top of it

this code will explain my issue What I want is when I tap on "Open Second Sheet" it will open new sheet above it

is there solution for this issue without having multiple values and using isPresented on multiple sheet ?

enum SheetActions: Identifiable {
    case firstSheet, secondSheet

    var id: Int {
        switch self {
        case .firstSheet:
            return 1
        case .secondSheet:
            return 2
        }
    }
}

struct ContentView: View {
    @State private var currentSheet: SheetActions?

    var body: some View {
        VStack {
            Button("Open First Sheet") {
                currentSheet = .firstSheet
            }
            .padding()
        }
        .sheet(item: $currentSheet, content: { item in
            switch item {
            case .firstSheet:
                FirstSheetView(currentSheet: $currentSheet)
            case .secondSheet:
                Text("Second Sheet")
            }
        })
    }
}

struct FirstSheetView: View {
    @Binding var currentSheet: SheetActions?

    var body: some View {
        NavigationView {
            VStack {
                NavigationLink(destination: SecondView(currentSheet: $currentSheet)) {
                    Text("Navigate to Second View")
                }
                .padding()
            }
            .navigationBarTitle("First Sheet", displayMode: .inline)
            .navigationBarItems(trailing: Button(action: {
                currentSheet = nil
            }) {
                Text("Close")
            })
        }
    }
}

struct SecondView: View {
    @Binding var currentSheet: SheetActions?

    var body: some View {
        VStack {
            Button("Open Second Sheet") {
                currentSheet = .secondSheet
            }
            .padding()
        }
        .navigationBarTitle("Second View", displayMode: .inline)
    }
}


struct ContentView_Previews: PreviewProvider {
    static var previews: some View {
        ContentView()
    }
}

I did tried to have multiple

0

There are 0 best solutions below