1

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

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
        }
    }
}

1 Answers1

2

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.

Asperi
  • 228,894
  • 20
  • 464
  • 690
  • But what is if used MenuButton as @ViewBuilder function like `@ViewBuilder private func MenuButton ()` then how will use @State property to bind the UI – Vikash Kumar Chaubey Aug 17 '22 at 10:57