0

When I click on a single Chip in my view, it triggers the actions in all chips. For Example, if I click on the "Date" chip, I expect self.viewModel.showDateSelection.toggle() to be executed. It does get executed but so does self.viewModel.showTypeSelection.toggle() and self.viewModel.showPointsSelection.toggle(). I cannot figure out why clicking on one chip would trigger the actions on all chips.

Screen

struct EventsScreen: View {

    @EnvironmentObject
    var viewModel: EventsViewModel

    //...

    var body: some View {
        GeometryReader { geometry in
            List {
                ZStack(alignment: .top) {
                    // ...
                    VStack {
                        // ...
                        HStack {
                            Spacer()
                            Chip(text: "Dates", action:  {
                                self.viewModel.showDateSelection.toggle()
                            })
                            Chip(text:"Type", action: {
                                self.viewModel.showTypeSelection.toggle()
                            })
                            Chip(text: "Points", action: {
                                self.viewModel.showPointsSelection.toggle()
                            })
                            Spacer()
                        }
                    }
                }
                // ...
            }
        }
    }
}

Chip

struct Chip: View {
    var text: String
    var action: () -> Void
    var body: some View {
        Button(action: self.action) {
        HStack {
            Text(text).foregroundColor(Color.white)
            Image("drop_down").foregroundColor(Color.white)
        }
        .padding(EdgeInsets.init(top: 4, leading: 16, bottom: 4, trailing: 16))
        .background(Color.black)
    .cornerRadius(40)
        }

    }
}
Adrian Le Roy Devezin
  • 672
  • 1
  • 13
  • 41

1 Answers1

1

it still issues in SwiftUI to have multiple Button in a List Item

you can use Text with onTapGesture rather than Button to workaround that

refer to this SwiftUI - Multiple Buttons in a List row

aiwiguna
  • 2,852
  • 2
  • 15
  • 26