Colton Idle
07/27/2021, 5:14 AMColumn() {
val pagerState = rememberPagerState(pageCount = 10)
HorizontalPager(state = pagerState) { page ->
Text(text = "Page: $page", modifier = Modifier.fillMaxWidth())
}
Button(onClick = { pagerState.animateScrollToPage(pagerState.currentPage + 1) }) {
Text(text = "Test")
}
}
But I get an error of Suspend function 'animateScrollToPage' should be called only from a coroutine or another suspend function
I thought it was okay to have an event like this in an onClick event since it's not happening on every recomposition?Kefas
07/27/2021, 5:17 AManimateScrollToPage
is a suspend function, need to run this from a coroutine.
val scope = rememberCoroutineScope()
scope.launch {
pagerState.animateScrollToPage(...)\
}
Colton Idle
07/27/2021, 5:23 AMval scope = rememberCoroutineScope()
is just the typical/standard way to create a coroutine scope in a composable?Kefas
07/27/2021, 5:24 AMAbhishek Dewan
07/27/2021, 5:25 AMKefas
07/27/2021, 5:50 AMTin Tran
07/27/2021, 6:59 AMscope.launch {}
shouldn’t be used directly inside compose, should it?Kefas
07/27/2021, 7:13 AMTin Tran
07/27/2021, 7:16 AMdimsuz
07/27/2021, 10:49 AMLaunchedEffect(state.currentPageIndex) {
pagerState.animateScrollToPage(state.currentPageIndex)
}