Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 25 additions & 1 deletion SwiftUIBasics/Views/RatingView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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()
}