Last active
August 21, 2023 11:24
Animated worm page indicator composable for Jetpack Compose
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import androidx.compose.animation.core.animateDpAsState | |
import androidx.compose.foundation.Canvas | |
import androidx.compose.foundation.layout.Arrangement | |
import androidx.compose.foundation.layout.Row | |
import androidx.compose.foundation.layout.requiredWidth | |
import androidx.compose.foundation.layout.size | |
import androidx.compose.runtime.Composable | |
import androidx.compose.runtime.getValue | |
import androidx.compose.ui.Modifier | |
import androidx.compose.ui.geometry.CornerRadius | |
import androidx.compose.ui.geometry.Size | |
import androidx.compose.ui.graphics.Color | |
import androidx.compose.ui.unit.Dp | |
import androidx.compose.ui.unit.dp | |
@Composable | |
fun WormPageIndicator( | |
totalPages: Int, | |
currentPage: Int, | |
modifier: Modifier = Modifier, | |
indicatorSize: Dp = 6.dp, | |
color: Color = Color.White, | |
spacing: Dp = indicatorSize, | |
selectedMultiplier: Int = 3 | |
) { | |
assert( | |
value = currentPage in 0 until totalPages, | |
lazyMessage = { "Current page index is out of range." } | |
) | |
val rowWidth = (indicatorSize * (selectedMultiplier + (totalPages - 1))) + (spacing * (totalPages - 1)) | |
Row( | |
modifier = modifier | |
.requiredWidth(rowWidth), | |
horizontalArrangement = Arrangement.SpaceBetween | |
) { | |
for (i in 0 until totalPages) { | |
val selected = i == currentPage | |
val height = indicatorSize | |
val width: Dp by animateDpAsState( | |
if (selected) indicatorSize * selectedMultiplier else indicatorSize | |
) | |
Canvas( | |
modifier = Modifier | |
.size(width, height), | |
onDraw = { | |
drawRoundRect( | |
color = color, | |
cornerRadius = CornerRadius(height.toPx() / 2), | |
size = Size(width.toPx(), height.toPx()) | |
) | |
} | |
) | |
} | |
} | |
} |
I've discarded lazyRow and used it with HorizontalPager - works like a charm!
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
anyone used it with LazyRow? Can share a sample?