Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there really no RatingBar in Jetpack Compose?

I need to add UI that will let a user thumb over a star rating bar and set a rating. So far, I haven't found a single custom rating bar that does this. There are plenty of non-interactive ones, but I can't use those.

I can't believe this is missing from Jetpack Compose.

like image 263
Marty Miller Avatar asked Sep 03 '25 13:09

Marty Miller


1 Answers

Here My RatingBar:

@Composable
fun RatingBar(
    rating: Float,
    maxRating: Int = 5,
    onRatingChanged: (Float) -> Unit
) {
    Row {
        for (i in 1..maxRating) {
            IconButton(onClick = { onRatingChanged(i.toFloat()) }) {
                if (i <= rating) Icons.Default.Star else Icons.Default.StarBorder
            }
        }
    }
}

When you call it:

var rating by remember { mutableStateOf(3.5f) }
            RatingBar(
                rating = rating,
                onRatingChanged = { newRating ->
                    rating = newRating
                },
)
like image 160
Nrohpos Avatar answered Sep 05 '25 06:09

Nrohpos