A lightweight SwiftUI rating view component which displays a slider like rating with some animation expression.
To add RatingView to your project: Add the repository URL to your project’s package dependencies.
struct ContentView: View {
@State var isPresented: Bool = false
@State var rating: Double = 0
@State var feedback: String = ""
var body: some View {
RatingView(titleText: "How is our app experience?") { (rating: Double, feedback: String) in
self.rating = rating
isPresented.toggle()
}
.alert("Thank you", isPresented: $isPresented) {
Button("OK") {
isPresented = false
}
} message: {
Text("Thank you for providing us with \(String(format: "%.1f", rating * 5)) out of 5.")
}
}
}
To change the title, pass your custom string literal to the RatingView initializer.
RatingView(titleText: "Your custom title") { _,_ in
self.rating = rating
isPresented.toggle()
}
The trailing closure of RatingView contains two parameters. The first parameter is the rating which is a Double and the second parameter is the feedback which is a String.
RatingView(titleText: "Title") { (rating: Double, feedback: String) in
//Your business logic goes here
//rating = actual rating between 0 - 1.
//feedback = feedback text string
//Round the double value and multiply with your maximum rating amount.
//ex: if the user selected 0.75 & your maximum rating is 5, then rating * 5 = 3.75.
}
Please review the demo project under the RatingViewDemo directory for a concrete example.