PagerState

Function

Common
fun PagerState(
    currentPage: Int = 0,
    @FloatRange(from = -0.5, to = 0.5) currentPageOffsetFraction: Float = 0f,
    pageCount: () -> Int,
): PagerState

Creates a default PagerState to be used with a Pager

Please refer to the sample to learn how to use this API.

Parameters

currentPageThe pager that should be shown first.
currentPageOffsetFractionThe offset of the initial page as a fraction of the page size. This should vary between -0.5 and 0.5 and indicates how to offset the initial page from the snapped position.
pageCountThe amount of pages this Pager will have.

Code Examples

PagerWithStateSample

@Composable
fun PagerWithStateSample() {
    // You can use PagerState to define an initial page
    val state = rememberPagerState(initialPage = 5) { 10 }
    HorizontalPager(modifier = Modifier.fillMaxSize(), state = state) { page ->
        Box(
            modifier =
                Modifier.padding(10.dp).background(Color.Blue).fillMaxWidth().aspectRatio(1f),
            contentAlignment = Alignment.Center,
        ) {
            Text(text = page.toString(), fontSize = 32.sp)
        }
    }
}