How can i get VerticalPager (official component) t...
# compose
j
How can i get VerticalPager (official component) to have last and next number? Video shows actual code, without last and next number
Copy code
Column {
    VerticalPager(
        state = hourPageState,
        modifier = Modifier.size(50.dp),
        pageCount = timeState.hoursFromNow.count(),
        
    ) {
        Column {
            Text(
                modifier = Modifier.fillMaxWidth(),
                text = timeState.hoursFromNow[it],
                textAlign = TextAlign.Center
            )
        }
    }
}
c
Hm. I would assume contentPadding would allow you to do this? I haven't use the official component yet, but the one in accompanist allowed you to set something like this up (i know i did this with horizaontal pager)
You can also see how they did it here? (most likely not using pager tho) https://github.com/ChargeMap/Compose-NumberPicker
j
Thank you! I found a solution in Google sample code, there's a code to alter Page size and choose number of pages, it worked.
s
You can write your own pretty easily. You need: • An
Animatable
• To assign each element a certain height so that
itemIndex = animatable.value / itemHeight
• A vertical drag listener (
Modifier.draggable
or
Modifier.pointerInput
+
detectVerticalDragGestures
) to govern the animatable • Use a custom
Layout
to display items. Otherwise, if you just have texts or simple shapes, you can just use a plain
Canvas
and use
drawText
. In both cases, you need to take the
animatable.value
into account
j
Copy code
val customPageSize = object : PageSize {
    override fun Density.calculateMainAxisPageSize(
        availableSpace: Int,
        pageSpacing: Int
    ): Int {
        return (availableSpace - 2 * pageSpacing) / 3
    }
}
VerticalPager(
    state = hourPageState,
    pageSize = customPageSize,
    modifier = Modifier.size(height = 150.dp, width = 50.dp),
    pageCount = timeState.hoursFromNow.count(),
) {
    Text(
        modifier = Modifier.fillMaxWidth(),
        text = timeState.hoursFromNow[it],
        fontSize = if (hourPageState.currentPage + 1 == it) 20.sp else 12.sp,
        textAlign = TextAlign.Center
    )
}
Using this code, was possible to get this result.