Do this method utilizing a @State var choice
, and .background
modifier
on the Button
.
struct ContentView: View {
let arr = ["item-1","item-2","item-3","item-4"]
@State var choice = "" // <-- right here, regulate to subcategory kind
var physique: some View {
ScrollView(.horizontal) {
HStack {
ForEach(arr, id: .self) { subcategory in
Button {
print("chosen SubCategory",subcategory)
// vm.getProducts(subcat_id: subcategory.id)
choice = subcategory // <-- right here
} label: {
// SubCatView(subcategory: subcategory)
Textual content(subcategory)
}
// --- right here
.background((choice == subcategory ? Colour.yellow : Colour.clear))
}
}
}
}
}