I want to create a list of ready-made cards. I wrote the card in another file, the code below.
struct PopulationPlace: View {
let image: String
let score: String
let title: String
@State private var isFavorite: Bool = false
var body: some View {
Image(image)
.resizable()
.aspectRatio(contentMode: .fill)
.frame(width: 200, height: 210)
.clipShape(RoundedRectangle(cornerRadius: 25.0))
.overlay(alignment: .topTrailing, content: {
Button {
withAnimation{
isFavorite = false
}
} label: {
Image(systemName: "heart.circle.fill")
.symbolRenderingMode(.palette)
.foregroundStyle(.red, .yellow)
.foregroundColor(.red)
.padding(.vertical, 8)
.padding(.leading, 8)
}
.padding()
})
}
}
When I launch it in the preview, the buttons respond to clicks, but when I create a list of cards, the buttons cease to be active. What am I doing wrong?
It doesn't work for me
ScrollView(.horizontal, showsIndicators: false){
HStack{
ForEach(sampleList, id: \SamplePopulation.self){
item in
PopulationPlace(image: item.image, score: item.score, title:item.title)
}
}
}
Sample data
struct SamplePopulation: Identifiable, Hashable{
let id: String = UUID().uuidString
let image: String
let score: String
let title: String
}