forked from nickGangalo/SimpleLittleStar
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRatingsView.swift
34 lines (28 loc) · 1.1 KB
/
RatingsView.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
struct RatingsView: View {
// Set these parameters in your call.
@Binding var rating: Int // Sets the rating to a state variable.
var starSize: CGFloat // The size of the stars.
var disabled: Bool // If set to 'true' the rating is not editable so you can use the view to just display results.
var starColor: Color // Sets the fill color of the stars.
var body: some View {
HStack {
Image(systemName: rating != 0 ? "star.slash" : "star.slash.fill" )
.font(.system(size: starSize))
.foregroundColor(starColor)
.onTapGesture {
rating = 0
}.disabled(disabled)
ForEach(1..<6) { idx in
ratingButton(idx: idx)
}
}
}
func ratingButton(idx: Int) -> some View {
Image(systemName: idx <= rating ? "star.fill" : "star")
.font(.system(size: starSize))
.foregroundColor(starColor)
.onTapGesture {
rating = idx
}.disabled(disabled)
}
}