|
1 | 1 | # Compose-RatingBar |
2 | | -🚀⭐️📊 Rating bar to set fixed value or change rating by gestures with custom png files or Compsables |
| 2 | +🚀⭐️📊 Rating bar to set fixed value or change rating by gestures with custom png files, ImageVectors |
| 3 | + |
| 4 | + |
| 5 | + |
| 6 | + |
| 7 | +https://user-images.githubusercontent.com/35650605/194719853-f000097b-de82-4d0a-a552-86fc77b16420.mp4 |
| 8 | + |
| 9 | +## Declrations |
| 10 | + |
| 11 | +```kotlin |
| 12 | +@Composable |
| 13 | +fun RatingBar( |
| 14 | + modifier: Modifier = Modifier, |
| 15 | + rating: Float, |
| 16 | + imageVectorBackground: ImageVector, |
| 17 | + imageVectorForeground: ImageVector, |
| 18 | + tint: Color = Color.Red, |
| 19 | + itemSize: Dp = Dp.Unspecified, |
| 20 | + animationEnabled: Boolean = true, |
| 21 | + gestureEnabled: Boolean = true, |
| 22 | + itemCount: Int = 5, |
| 23 | + space: Dp = 0.dp, |
| 24 | + onRatingChange: ((Float) -> Unit)? = null |
| 25 | +) |
| 26 | +``` |
| 27 | + |
| 28 | +```kotlin |
| 29 | +@Composable |
| 30 | +fun RatingBar( |
| 31 | + modifier: Modifier = Modifier, |
| 32 | + rating: Float, |
| 33 | + painterBackground: Painter, |
| 34 | + painterForeground: Painter, |
| 35 | + tint: Color? = null, |
| 36 | + itemSize: Dp = Dp.Unspecified, |
| 37 | + animationEnabled: Boolean = true, |
| 38 | + gestureEnabled: Boolean = true, |
| 39 | + itemCount: Int = 5, |
| 40 | + space: Dp = 0.dp, |
| 41 | + onRatingChange: ((Float) -> Unit)? = null |
| 42 | +) |
| 43 | +``` |
| 44 | + |
| 45 | +```kotlin |
| 46 | +@Composable |
| 47 | +fun RatingBar( |
| 48 | + modifier: Modifier = Modifier, |
| 49 | + rating: Float, |
| 50 | + imageBackground: ImageBitmap, |
| 51 | + imageForeground: ImageBitmap, |
| 52 | + itemSize: Dp = Dp.Unspecified, |
| 53 | + animationEnabled: Boolean = true, |
| 54 | + gestureEnabled: Boolean = true, |
| 55 | + itemCount: Int = 5, |
| 56 | + space: Dp = 0.dp, |
| 57 | + onRatingChange: ((Float) -> Unit)? = null |
| 58 | +) |
| 59 | +``` |
| 60 | + |
| 61 | +## Usage |
| 62 | + |
| 63 | +```kotlin |
| 64 | +var rating by remember { mutableStateOf(3.7f) } |
| 65 | +var rating2 by remember { mutableStateOf(3.7f) } |
| 66 | +var rating3 by remember { mutableStateOf(2.3f) } |
| 67 | +var rating4 by remember { mutableStateOf(4.5f) } |
| 68 | +var rating5 by remember { mutableStateOf(1.7f) } |
| 69 | +var rating6 by remember { mutableStateOf(5f) } |
| 70 | + |
| 71 | +val imageBackground = ImageBitmap.imageResource(id = R.drawable.star_background) |
| 72 | +val imageForeground = ImageBitmap.imageResource(id = R.drawable.star_foreground) |
| 73 | + |
| 74 | +Column(modifier = Modifier.fillMaxSize()) { |
| 75 | + RatingBar( |
| 76 | + rating = rating, |
| 77 | + space = 2.dp, |
| 78 | + imageBackground = imageBackground, |
| 79 | + imageForeground = imageForeground, |
| 80 | + animationEnabled = false, |
| 81 | + gestureEnabled = true, |
| 82 | + itemSize = 60.dp |
| 83 | + ) { |
| 84 | + rating = it |
| 85 | + } |
| 86 | + |
| 87 | + Text( |
| 88 | + "Rating: $rating", |
| 89 | + fontSize = 16.sp, |
| 90 | + color = MaterialTheme.colorScheme.primary |
| 91 | + ) |
| 92 | + |
| 93 | + RatingBar( |
| 94 | + rating = rating2, |
| 95 | + painterBackground = painterResource(id = R.drawable.star_background), |
| 96 | + painterForeground = painterResource(id = R.drawable.star_foreground), |
| 97 | + tint = Color(0xff9C27B0), |
| 98 | + animationEnabled = false, |
| 99 | + gestureEnabled = false, |
| 100 | + itemSize = 60.dp |
| 101 | + ) { |
| 102 | + rating2 = it |
| 103 | + } |
| 104 | + |
| 105 | + Slider( |
| 106 | + value = rating2, |
| 107 | + onValueChange = { rating2 = it }, |
| 108 | + valueRange = 0f..5f |
| 109 | + ) |
| 110 | + |
| 111 | + Text( |
| 112 | + "Rating: $rating2", |
| 113 | + fontSize = 16.sp, |
| 114 | + color = MaterialTheme.colorScheme.primary |
| 115 | + ) |
| 116 | + |
| 117 | + RatingBar( |
| 118 | + rating = rating3, |
| 119 | + painterBackground = painterResource(id = R.drawable.star_background), |
| 120 | + painterForeground = painterResource(id = R.drawable.star_foreground), |
| 121 | + tint = Color(0xff795548), |
| 122 | + animationEnabled = true, |
| 123 | + itemSize = 60.dp |
| 124 | + ) { |
| 125 | + rating3 = it |
| 126 | + } |
| 127 | + |
| 128 | + Text( |
| 129 | + "Rating: $rating", |
| 130 | + fontSize = 18.sp, |
| 131 | + color = MaterialTheme.colorScheme.primary |
| 132 | + ) |
| 133 | + |
| 134 | + RatingBar( |
| 135 | + rating = rating4, |
| 136 | + space = 2.dp, |
| 137 | + imageVectorBackground = Icons.Default.FavoriteBorder, |
| 138 | + imageVectorForeground = Icons.Default.Favorite, |
| 139 | + tint = Color(0xffE91E63), |
| 140 | + itemSize = 60.dp |
| 141 | + ) { |
| 142 | + rating4 = it |
| 143 | + } |
| 144 | + |
| 145 | + RatingBar( |
| 146 | + rating = rating5, |
| 147 | + space = 2.dp, |
| 148 | + imageVectorBackground = ImageVector.vectorResource(id = R.drawable.outline_wb_cloudy_24), |
| 149 | + imageVectorForeground = ImageVector.vectorResource(id = R.drawable.baseline_wb_cloudy_24), |
| 150 | + tint = Color(0xff2196F3), |
| 151 | + itemSize = 60.dp |
| 152 | + ) { |
| 153 | + rating5 = it |
| 154 | + } |
| 155 | + |
| 156 | + RatingBar( |
| 157 | + rating = rating6, |
| 158 | + imageVectorBackground = ImageVector.vectorResource(id = R.drawable.twotone_person_24), |
| 159 | + imageVectorForeground = ImageVector.vectorResource(id = R.drawable.baseline_person_24), |
| 160 | + tint = Color(0xff795548), |
| 161 | + itemSize = 40.dp |
| 162 | + ) { |
| 163 | + rating6 = it |
| 164 | + } |
| 165 | + |
| 166 | + RatingBar( |
| 167 | + rating = 4.5f, |
| 168 | + space = 2.dp, |
| 169 | + imageBackground = imageBackground, |
| 170 | + imageForeground = imageForeground |
| 171 | + ) |
| 172 | + RatingBar( |
| 173 | + rating = 1.3f, |
| 174 | + space = 4.dp, |
| 175 | + imageBackground = imageBackground, |
| 176 | + imageForeground = imageForeground |
| 177 | + ) |
| 178 | +} |
| 179 | +``` |
3 | 180 |
|
4 | 181 |
|
5 | | -https://user-images.githubusercontent.com/35650605/194710761-d2ad9421-3185-4051-8d7f-1ff7f21730bb.mp4 |
6 | 182 |
|
0 commit comments