0

I'm building a simple app in Swiftui that stores and reads data from a Firestore collection. When a user taps on an item in the grid view, I want it to open up a detail view modal with information from that tapped item.

Currently, the modal pops up and is populated by the first item in my list only. This means that when I tap on the second grid item, the detail view for that tapped item is populated with the information from the first grid item.

Additionally, the ontapgesture recognizes which Habit is being tapped because it prints the correct habit.id to the console.

How can I pass the data of the grid item being tapped, to the detail view?

Grid View

struct HabitGrid: View {
   
    @EnvironmentObject var habits: Habits
    @ObservedObject var repository = HabitRepositoryViewModel()
    
    @State var isShowingHabitDetailView = false
    
    var body: some View {
        
        LazyVGrid(columns: layout, spacing: 10){
            
            ForEach(self.repository.habits) { habit in

                Button(action: {
                    
                }) {
                        VStack{

                            Text("\(habit.name)")

                        }
                        .onTapGesture {
                            self.isShowingHabitDetailView.toggle()
                            print(habit.id)
                    
                        }
                }
                .sheet(isPresented: self.$isShowingHabitDetailView) {
                    HabitDetailView(habit: habit)
                }
            }
        }
    }
}

Detail View

struct HabitDetailView: View {
    
    @ObservedObject var repository = HabitRepositoryViewModel()
    
    var habit: Habit
    
    var body: some View {
    
    ZStack {
        
        
        VStack{

            Text("\(habit.name)")

            }
        }
    }
}

This might be very basic. I appreciate any and all help! Thanks.

Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
Simbeul
  • 441
  • 4
  • 11

0 Answers0