itnoles
05/03/2023, 10:13 PMJeff Lockhart
05/03/2023, 10:34 PMlaunch
a coroutine and run suspend functions within the coroutine. Or make the main()
function itself a suspend fun
.itnoles
05/03/2023, 10:38 PM@Composable
fun FilterTabs(tabState: MutableState<TabState>, scrollState: ScrollState) {
TabRow(selectedTabIndex = TabState.values().toList().indexOf(tabState.value)) {
TabState.values().forEach {
Tab(
text = { Text(it.name) },
selected = tabState.value == it,
onClick = {
tabState.value = it
runBlocking {
scrollState.scrollTo(0)
}
}
)
}
}
}
Jeff Lockhart
05/03/2023, 10:43 PM@Composable
fun FilterTabs(tabState: MutableState<TabState>, scrollState: ScrollState) {
val coroutineScope = rememberCoroutineScope()
TabRow(selectedTabIndex = TabState.values().toList().indexOf(tabState.value)) {
TabState.values().forEach {
Tab(
text = { Text(it.name) },
selected = tabState.value == it,
onClick = {
tabState.value = it
coroutineScope.launch {
scrollState.scrollTo(0)
}
}
)
}
}
}
coroutineScope
is tied to the composable's lifecycle. This way your launched coroutine, and suspend functions called within in it, will be canceled when the composable's lifecycle ends.itnoles
05/03/2023, 10:43 PM