Jetpack Compose ModalBottomSheet 一直显示。

huangapple go评论72阅读模式
英文:

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)
  }
}

huangapple
  • 本文由 发表于 2023年6月19日 15:58:43
  • 转载请务必保留本文链接:https://go.coder-hub.com/76504674.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定