I'm trying to pass an item to a sheet, if I change the values by selecting another item. The sheet keeps displaying the first item, even when I never called the sheet with the first item.
Is this a bug or am I doing something wrong?
WorkoutList.swift
import SwiftUI
struct WorkoutList: View {
private var workouts = workoutData
@State var showWorkoutDetails = false
var body: some View {
ScrollView(.horizontal, showsIndicators: false) {
HStack(spacing: 20) {
ForEach(workouts) { workoutCard in
GeometryReader { geometry in
Button(action: {self.showWorkoutDetails.toggle()}) {
WorkoutCardView(workout: workoutCard)
.rotation3DEffect(Angle(degrees: Double((geometry.frame(in: .global).minX - 30) / -30)), axis: (x: 0, y: 10, z: 0))
.sheet(isPresented: self.$showWorkoutDetails) {
WorkoutDetails(workout: workoutCard)
}
}
}
.frame(width:246, height: 360)
}
}
.padding(30)
}
}
}
private let workoutData = [
Workout(name: "Workout #1", imageName: "workoutImage", color: Color.orange),
Workout(name: "Workout #2", imageName: "workoutImage", color: Color.green),
Workout(name: "Workout #3", imageName: "workoutImage", color: Color.red),
Workout(name: "Workout #4", imageName: "workoutImage", color: Color.blue)
]
WorkoutDetails.swift
import SwiftUI
struct WorkoutDetails: View {
var workout = Workout(name: "", imageName: "", color: Color.black)
var body: some View {
GeometryReader { geometry in
ZStack {
Rectangle()
.fill(self.workout.color)
VStack(alignment: .leading) {
VStack(alignment: .leading) {
Text(self.workout.name)
.foregroundColor(.white)
.font(.title)
.fontWeight(.heavy)
.lineLimit(nil)
.frame(minWidth: 0, maxWidth: .infinity, alignment: .leading)
.frame(height: 70)
}
.frame(width: 180)
Image(self.workout.imageName)
.resizable()
.aspectRatio(contentMode: .fit)
.frame(width: geometry.size.width-60, height: 200)
.padding()
Spacer()
}
.padding(.top, 40)
.padding(.leading, 10)
}
}
.edgesIgnoringSafeArea(.bottom)
}
}
Here I want to display a workout. But whatever I change it, I always get "workout #1" and I don't know what I'm doing wrong.