0

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
}
dev_nil
  • 89
  • 1
  • 8
  • 3
    Please include a [mre] – jnpdx Feb 02 '23 at 17:15
  • Is this issue is about touchable area? Then please check [this](https://stackoverflow.com/a/65980670/4061501) out. – Lal Krishna Feb 02 '23 at 17:45
  • How do you know the button is not clickable ? The only thing it does is setting to false a var which already contains false and is not used . – Ptit Xav Feb 02 '23 at 21:57
  • To check if they are actually detecting the tap use a "print" to see the log on the debug window or a break point. If you don't see anything it's a touchable area issue, check Lal Krishna comment, if you see a log it's your code that at a glance, does nothing like Ptit Xav mentioned – Pedro Cavaleiro Feb 03 '23 at 22:43

0 Answers0