solidogen
06/28/2021, 4:28 PMAndrew Neal
06/28/2021, 4:35 PMsolidogen
06/28/2021, 4:36 PMAndrew Neal
06/28/2021, 4:43 PMBox
with a Modifier
. But you can see how making one is mostly a matter of accepting PagerState
and using that to derive any scrolling or scaling effects.
@ExperimentalPagerApi
@Composable
fun HorizontalPagerIndicator(
pagerState: PagerState,
modifier: Modifier = Modifier,
activeColor: Color = LocalContentColor.current.copy(alpha = LocalContentAlpha.current),
inactiveColor: Color = activeColor.copy(ContentAlpha.disabled),
indicatorWidth: Dp = 8.dp,
indicatorHeight: Dp = indicatorWidth,
spacing: Dp = indicatorWidth,
indicatorShape: Shape = CircleShape,
) {
Box(
modifier = modifier,
contentAlignment = Alignment.CenterStart
) {
Row(
horizontalArrangement = Arrangement.spacedBy(spacing),
verticalAlignment = Alignment.CenterVertically,
) {
val indicatorModifier = Modifier
.size(width = indicatorWidth, height = indicatorHeight)
.background(color = inactiveColor, shape = indicatorShape)
repeat(pagerState.pageCount) {
Box(indicatorModifier)
}
}
Box(
Modifier
.offset {
val scrollPosition = (pagerState.currentPage + pagerState.currentPageOffset)
.coerceIn(0f, (pagerState.pageCount - 1).toFloat())
IntOffset(
x = ((spacing + indicatorWidth) * scrollPosition).roundToPx(),
y = 0
)
}
.size(width = indicatorWidth, height = indicatorHeight)
.background(
color = activeColor,
shape = indicatorShape,
)
)
}
}
solidogen
06/28/2021, 4:44 PMsolidogen
06/28/2021, 4:45 PM