今天閱讀「適用於 Android 開發人員的 Jetpack Compose」第三章「架構與狀態」的「提升狀態的位置」、「進階狀態和連帶效果」
collectAsStateWithLifecycle()
會監聽生命週期,從 StateFlow 收集各個值,並透過 Compose 的 State API 呈現最新的值
dependencies {
implementation "androidx.lifecycle:lifecycle-runtime-compose:$lifecycle_version"
}
rememberUpdatedState
Box(modifier = modifier.fillMaxSize(), contentAlignment = Alignment.Center) {
val currentOnTimeout by rememberUpdatedState(onTimeout)
LaunchedEffect(Unit) {
delay(SplashWaitTime)
currentOnTimeout()
}
Image(painterResource(id = R.drawable.ic_crane_drawer), contentDescription = null)
}
openDrawer
中想要使用scaffoldState.drawerState.open()
的話要使用共常式
CraneHomeContent(
modifier = modifier,
onExploreItemClicked = onExploreItemClicked,
openDrawer = {
scope.launch {
scaffoldState.drawerState.open()
}
}
)
derivedStateOf
val showButton by remember {
derivedStateOf {
listState.firstVisibleItemIndex > 0
}
}