我正在尝试实现modalBottomSheet
的BottomSheetScaffold
一些自定义实现。
这是我的BottomSheetScaffold
BottomSheetScaffold(
sheetPeekHeight = 0.dp,
scaffoldState = bottomSheetState,
sheetBackgroundColor = Color.Transparent,
backgroundColor = Color.Transparent,
sheetElevation = 0.dp,
sheetShape = RoundedCornerShape(topStart = 36.dp, topEnd = 36.dp),
snackbarHost = snackBarHost,
sheetContent = { bottomSheet() }) {
Box(Modifier.fillMaxSize()) {
val coroutineScope = rememberCoroutineScope()
sheetContent()
Scrim(
color = Primary,
alpha = bottomSheetState.currentFraction * 0.5f,
onDismiss = {
coroutineScope.launch { bottomSheetState.bottomSheetState.collapse() }
},
visible = bottomSheetState.bottomSheetState.targetValue != BottomSheetValue.Collapsed && bottomSheetState.bottomSheetState.currentValue != BottomSheetValue.Collapsed
)
}
}
当这个脚手架被某个屏幕调用时,sheetContent()
将被替换为屏幕内容。我这里的问题是,当bottomSheet()
在该屏幕上为空时,因此没有高度,底部表单状态认为它被扩展了,而我只是没有将可组合性放在bottomSheet()
内,它只是根据某些条件填充,没有默认可组合性。因此,Scrim()
函数将是可见的当我点击它时,这个异常将抛出
java.lang.IllegalArgumentException: The target value must have an associated anchor.
虽然sheetContent
是必要的,但没有办法处理空值,因为BottomSheetState
类处理的滑动需要锚来获得高度和空值,导致意想不到的结果
这是最新版本compose 1.2.x中引入的一个错误。为了在开始时停止显示底部工作表,我只在数据不为空时添加sheetContent(这在以前的版本中是不可能的)。现在我有另一个问题。第一次尝试时不会触发展开bottomSheet的单击事件。我总是需要点击两次。