1

I'm trying to close my expandable list when I click one of the item in my list. But somehow it is not triggering. I don't know where I'm making mistake. For that I'm using isExpanded Bool and when I click other items in my list im making isExpanded = false but its not working. Any idea?


struct SingleSelectionRow: View {

    @Binding var selectSingle: Int
    @Binding var selectedFilter: String?
    var title: String
    var isSelected: Bool
    var childrenList: [FilterChildren]
    @State var filterDetail: SortAndFilterData
    @State var singleSelectedFilters: [String]
    @State  var isExpanded : Bool = false
    var action: () -> Void
    @State var onClick: Bool = false

    var body: some View {

        if selectSingle == 1  {

            if childrenList.isEmpty == true {
                Button(action: {
                    self.action()
                    isExpanded = false
                    print("İS EXPANDED İS \(isExpanded)")
                }) {

                    Text(self.title.uppercased())
                        .font(.custom("DM Sans",size:15))
                        .foregroundColor(isSelected ? Color(red: 10 / 255, green: 207 / 255, blue: 131 / 255) : Color.black)
                }
            }

            if childrenList.isEmpty == false {
                DisclosureGroup(
                    isExpanded: $isExpanded,
                    content: {
                        ForEach(childrenList, id: \.id) { item in
                            Button(action: self.action) {
                                Text(item.name ?? "")
                                    .font(.custom("DM Sans",size:15))
                                    .frame(maxWidth: .infinity, alignment: .leading)

                            }
                        }
                    },
                    label: {
                        Text(self.title.uppercased())
                            .foregroundColor(isSelected ? Color(red: 10 / 255, green: 207 / 255, blue: 131 / 255) : Color.black)
                    }
                )
                .contentShape(Rectangle())
                .onTapGesture {
                    withAnimation(.easeOut) {
                        isExpanded.toggle()
                        print("İS EXPANDED İS \(isExpanded)")
                        self.action()
                    }
                }
            }
        }
    }
}
Andrew
  • 26,706
  • 9
  • 85
  • 101
Begbie
  • 283
  • 1
  • 17
  • Does this answer your question https://stackoverflow.com/a/63228810/12299030? – Asperi Mar 22 '21 at 16:58
  • No its not I saw that post but its not working like I want. As you can see in that gif when user click other parent expendable list its not closing current one. Basically I want to show only one open expendable list . – Begbie Mar 23 '21 at 06:20

0 Answers0