Slide 18
Slide 18 text
@Composable
fun DerivedState() {
val scope = rememberCoroutineScope()
val lazyState = rememberLazyListState()
val showScrollToTop by remember { derivedStateOf { lazyState.firstVisibleItemIndex > 0 }}
Screen {
Scaffold(
topBar = {
TopBar("Derived State") {
if (showScrollToTop) {
TopBarAction(
imageVector = Icons.Default.KeyboardArrowUp,
contentDescription = "Scroll to top",
onClick = { scope.launch { lazyState.scrollToItem(0)}}
)
}
}
}
) { padding ->
LazyColumn(
state = lazyState,
contentPadding = padding
) {
items(buildMovies(100), key = { it.id }) {
MovieItem(movie = it)
}
}
}
}
}