SwiftUI – 如何使用列表项点击更改 @State 属性?

SwiftUI – How can I change a @State property with a list item tap?

在我的列表中,我有一些静态项目。当用户点击另一个列表项时,我想动态 hide/show 一个项目,即当用户点击一个特定的列表项时,我想更改 @State 属性。

我该怎么做?

struct EditTransactionView : View {
    @State var date = Date()
    @State private var showingDateSelector = false // How do I change this with a tap on the date list item?

    var body: some View {
        NavigationView {
            List {
                DateView(date: $date)
                if showingDateSelector {
                    DatePicker(
                        $date,
                        maximumDate: Date(),
                        displayedComponents: .date )
                }
            }
        }
    }
}

像这样:

struct EditTransactionView : View {
    @State var date = Date()
    @State private var showingDateSelector = false // How do I change this with a tap on the date list item?

    var body: some View {
        NavigationView {
            List {
                Button(action: { self.showingDateSelector.toggle() }) {
                    DateView(date: $date)
                }
                if showingDateSelector {
                    DatePicker(
                        $date,
                        maximumDate: Date(),
                        displayedComponents: .date )
                }
            }
        }
    }
}