SwiftUI 2.0 List with children - 如何让显示按钮的可点击区域覆盖整个列表项

SwiftUI 2.0 List with children - how to make the tappable area of the disclosure button cover the whole list item

我正在为 iOS 14 制作一个 SwiftUI 应用程序,我有一个侧边栏列表,它使用列表的新 children: 属性使列表可扩展:

但是,目前我只能在精确单击披露箭头时展开此列表。例如,当我单击 'My Cool Project' 文本时,我希望能够扩展此列表。

这在“文件”应用程序中是可能的 - 当我单击 'Locations' 的文本时,我可以看到“位置”项的所有子项,但我不知道如何在我的应用程序中执行此操作应用

澄清一下,每个项目的 List 项是一个 Text 视图,子列表项是 NavigationLink

这种行为在 SwiftUI 中是否可行,甚至是通过 onTapGesture 编程或使用 List 以外的其他东西?提前感谢任何 help/advice!

这是一个方法演示(当然,在您的项目中,您会将 expand/collapse 状态移动到视图模型中)

struct DemoDisclosureGroups: View {
    let items: [Bookmark] = [.example1, .example2, .example3]
    @State private var flags: [Bool] = [false, false, false]

    var body: some View {
        List {
            ForEach(Array(items.enumerated()), id: \.1.id) { i, group in
                DisclosureGroup(isExpanded: $flags[i]) {
                    ForEach(group.items ?? []) { item in
                        Label(item.name, systemImage: item.icon)
                    }
                } label: {
                    Label(group.name, systemImage: group.icon)
                        .contentShape(Rectangle())
                        .onTapGesture {
                            withAnimation {
                                self.flags[i].toggle()
                            }
                        }
                }
            }
        }
    }
}

struct Bookmark: Identifiable {
    let id = UUID()
    let name: String
    let icon: String
    var items: [Bookmark]?

    // some example websites
    static let apple = Bookmark(name: "Apple", icon: "1.circle")
    static let bbc = Bookmark(name: "BBC", icon: "square.and.pencil")
    static let swift = Bookmark(name: "Swift", icon: "bolt.fill")
    static let twitter = Bookmark(name: "Twitter", icon: "mic")

    // some example groups
    static let example1 = Bookmark(name: "Favorites", icon: "star", items: [Bookmark.apple, Bookmark.bbc, Bookmark.swift, Bookmark.twitter])
    static let example2 = Bookmark(name: "Recent", icon: "timer", items: [Bookmark.apple, Bookmark.bbc, Bookmark.swift, Bookmark.twitter])
    static let example3 = Bookmark(name: "Recommended", icon: "hand.thumbsup", items: [Bookmark.apple, Bookmark.bbc, Bookmark.swift, Bookmark.twitter])
}

backup