英文:
Jetpack Compose ModalBottomSheet always showing
问题
ModalBottomSheet
似乎默认显示,尽管实际上应该隐藏(rememberModalBottomSheetState
使用 initialValue: SheetValue = Hidden
来初始化状态)。显然在这里漏掉了某些东西。
val bottomSheetState = rememberModalBottomSheetState()
ModalBottomSheet(
onDismissRequest = { /*TODO*/ },
sheetState = bottomSheetState,
containerColor = Color.White,
) {
Text(text = "为什么我在显示?", color = Color.Black)
}
调用 `bottomSheetState.hide()` 也似乎没有任何效果。
在使用 Jetpack Compose BOM 2023.06.00 但在 2023.05.01 上获得相同的行为。
英文:
The ModalBottomSheet
seems to be showing by default although it should actually be hidden (rememberModalBottomSheetState
initiates the state with an initialValue : SheetValue = Hidden
). Clearly missing something here.
val bottomSheetState = rememberModalBottomSheetState()
ModalBottomSheet(
onDismissRequest = { /*TODO*/ },
sheetState = bottomSheetState,
containerColor = Color.White,
) {
Text(text = "Why am I showing?", color = Color.Black)
}
calling bottomSheetState.hide()
also doesn't seem to have any effect.
Using Jetpack Compose BOM 2023.06.00 but getting the same behaviour on 2023.05.01.
答案1
得分: 3
终于弄清楚了!新的 ModalBottomSheet 条件显示,所以应该像这样工作:
val openBottomSheet = rememberSaveable { mutableStateOf(false) }
val bottomSheetState = rememberModalBottomSheetState()
if (openBottomSheet) {
ModalBottomSheet(
onDismissRequest = { openBottomSheet = false },
sheetState = bottomSheetState,
containerColor = Color.White,
) {
Text(text = "为什么我在显示?", color = Color.Black)
}
}
英文:
Finally figured this out! The new ModalBottomSheet shows conditionally, so something like this should work:
val openBottomSheet = rememberSaveable { mutableStateOf(false) }
val bottomSheetState = rememberModalBottomSheetState()
if (openBottomSheet) {
ModalBottomSheet(
onDismissRequest = { openBottomSheet = false },
sheetState = bottomSheetState,
containerColor = Color.White,
) {
Text(text = "Why am I showing?", color = Color.Black)
}
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论