Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

ActionSheet opens on wrong index than clicked index

Action sheet is not opening to the right clicked index, it always opens to the wrong index.
Code snippet is-

Steps are:

1: here passing data to LazyVGrid

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

2: A View that have some image, text and three dot button

3: A common view that will handle the post tap event

LazyVGrid(columns: SizeConfig.GridLayout.adaptiveGridItemLayout_140) {
     ForEach(folderData) { folderItem in
        MakeGridFolders(folderData: folderItem)
    }
}

@ViewBuilder
private func MakeGridFolders(folderData: FolderModel)-> some View {
    NavigationLink(destination: FilesView()) {
        VStack() {
            Image(App.Image.fileIcon_Light)
            HStack {
                Text(folderData.folderName)
                Spacer()
                MenuButton(isActionSheetShow: $isActionSheetShow, action: {
                    isActionSheetShow.toggle()
                })
            }
        }
    }
}

struct MenuButton: View {
@Binding var isActionSheetShow: Bool
var action: () -> Void

var body: some View {
    VStack {
        Button {
            action()
        } label: {
            Image(icon)
        }
        .confirmationDialog("", isPresented: $isActionSheetShow, titleVisibility: .hidden) {
            //Some buttons
        }
    }
}

>Solution :

You joined all dialogs with one state, so once it is toggled all of them are activated.

Instead use internal state inside each button, like

struct MenuButton: View {
@State private var isActionSheetShow: Bool = false  // << here !!
var action: () -> Void

var body: some View {
    VStack {
        Button {
            action()
        } label: {
            Image(icon)
        }
        .confirmationDialog("", isPresented: $isActionSheetShow, titleVisibility: .hidden) {
            //Some buttons
        }
    }
}

*assuming other code you will update correspondingly.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading