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

Jetpack Compose:如何限制ModalBottomSheet仅处于隐藏与部分展开状态?

实现仅支持隐藏和部分展开的ModalBottomSheet

不需要用到hasExpandedState,这个属性只是用来判断当前SheetState是否包含Expanded状态的只读标识,没法直接修改。要实现仅支持隐藏和部分展开的需求,直接通过限制SheetState的状态切换范围即可:

  • 创建SheetState时,通过confirmValueChange拦截状态变更,只允许切换到Hidden(隐藏)和HalfExpanded(部分展开)状态,阻止切换到Expanded(完全展开)状态。

示例代码如下:

val sheetState = rememberModalBottomSheetState(
    initialValue = ModalBottomSheetValue.Hidden,
    confirmValueChange = { newValue ->
        // 仅允许在隐藏和部分展开状态间切换
        newValue == ModalBottomSheetValue.Hidden || newValue == ModalBottomSheetValue.HalfExpanded
    }
)

当用户尝试滑动底部弹窗到完全展开状态时,confirmValueChange返回false,状态切换会被阻止,这样弹窗就只能在隐藏和部分展开两种状态间切换了。

内容的提问来源于stack exchange,提问作者Ivan

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.04 04:43:10