You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Jetpack Compose中ModalBottomSheet关闭后无响应并阻塞UI的问题求助

Jetpack Compose中ModalBottomSheet关闭后无响应并阻塞UI的问题求助

大家好,我遇到了一个Jetpack Compose ModalBottomSheet的棘手问题:第一次打开底部弹窗时一切正常,但关闭它之后,弹窗就变得无响应,还直接阻塞了所有其他UI的交互操作,有没有大佬能帮忙分析下问题出在哪?

我的代码如下:

Button(
    onClick = {
        showBottomSheet = true
    }
) {
    Text(text = "Done")
}

if (showBottomSheet) StyleBottomSheet()

@OptIn(ExperimentalMaterial3Api::class)
@Composable
fun StyleBottomSheet() {
    val scope = rememberCoroutineScope()
    val sheetState = rememberModalBottomSheetState()
    var showBottomSheet by remember { mutableStateOf(false) }
    ModalBottomSheet(
        onDismissRequest = {
            showBottomSheet = false
        },
        sheetState = sheetState
    ) {
        // Sheet content
        Button(onClick = {
            scope.launch { sheetState.hide() }.invokeOnCompletion {
                if (!sheetState.isVisible) {
                    showBottomSheet = false
                }
            }
        }) {
            Text("Hide bottom sheet")
        }
    }
}

我自己琢磨了下,感觉可能是状态管理的问题?父组件里已经有一个控制弹窗显示的showBottomSheet状态,但我在StyleBottomSheet内部又重新定义了一个同名的状态,这两个状态完全独立,导致关闭弹窗时只修改了内部的状态,父组件的showBottomSheet还是保持true,相当于弹窗其实并没有真正从父组件的UI树中移除,所以才会阻塞后续的交互?

有没有正确的写法可以解决这个问题呀?

备注:内容来源于stack exchange,提问作者M.waqas

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.04.17 11:24:51