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
相关产品推荐
相关产品推荐

