diff --git a/SwiftUIBasics/Views/RatingView.swift b/SwiftUIBasics/Views/RatingView.swift index 3ca74af..a122ea5 100644 --- a/SwiftUIBasics/Views/RatingView.swift +++ b/SwiftUIBasics/Views/RatingView.swift @@ -8,11 +8,35 @@ import SwiftUI struct RatingView: View { + @State private var rating = 0 + var body: some View { - Text(/*@START_MENU_TOKEN@*/"Hello, World!"/*@END_MENU_TOKEN@*/) + VStack { + HStack { + ForEach(1...5, id: \.self) { index in + starImage(for: index) + .onTapGesture { + handleTap(for: index) + } + } + } + } + .padding() + } + + private func starImage(for index: Int) -> some View { + Image(systemName: index <= rating ? "star.fill" : "star") + .resizable() + .frame(width: 35, height: 35) + .foregroundStyle(Color("starColor")) + } + + private func handleTap(for index: Int) { + rating = (index == rating) ? 0 : index } } + #Preview { RatingView() }