在使用 ModalBottomSheet 的时候,应该在 Compose 中使用 State 来控制 bottom sheet 的打开和关闭。同时,也需要在 bottom sheet 关闭时手动清空 bottom sheet 内部的 Composable。以下是示例代码:
@Composable fun MyScreen() { // State,用来控制 bottom sheet 的打开和关闭状态 val bottomSheetState = rememberModalBottomSheetState(...)
// 打开 bottom sheet
Button(onClick = { bottomSheetState.show() }) {
Text("Show Bottom Sheet")
}
ModalBottomSheetLayout(
sheetState = bottomSheetState,
sheetContent = {
// bottomSheetContent,显示 bottom sheet 内容
val bottomSheetContent = remember { mutableStateOf(...) }
// 关闭 bottom sheet 时清空内部 Composable
bottomSheetState.onDismissed { bottomSheetContent.value = null }
// 显示 bottom sheet 内容
bottomSheetContent.value?.let { content -> Content(content) }
},
content = {
// 主界面内容
...
}
)
}
上一篇:AndroidJetpackCompose中使用AppCompatDelegate进行语言切换无效
下一篇:AndroidJetpackCompose中使用observeAsState()Property委托必须有一个'getValue(Nothing?,KProperty<*>)'函数。