通过在 Compose 中使用 ModalBottomSheet,我遇到了以下问题:
java.lang.IllegalArgumentException: The initial value must have an associated anchor.
我的可组合功能有:
// 1.
val sheetState = rememberModalBottomSheetState(ModalBottomSheetValue.Hidden)
val coroutineScope = rememberCoroutineScope()
// 2.
ModalBottomSheetLayout(
sheetContent = {
/* sheetContent */
},
sheetState = sheetState,
// modifier = Modifier.fillMaxSize() --> it doesn't change the result
) {
// 3.
Scaffold {
/* scaffold content */
}
}
通过将底部工作表的初始状态设置为 ModalBottomSheetValue.Expanded,问题就消失了。请注意,ModalBottomSheetValue.HalfExpanded 也会引发异常,并且没有任何初始值(默认为隐藏,因此看起来很符合逻辑)。是否有已知的解决方法或正在运行的库版本(我使用的 compose 版本:1.0.0,我尝试使用 1.0.0-rc2)?
更新
经过一番调查,问题似乎是由于工作表内容中的动态内容。我有一个 Column/LazyColumn 可以在数据可用时重新组合。通过具有固定内容,任何 ModalBottomSheetValue 的问题都会消失。
修复
对于“空”内容(理解高度为 0 dp 的内容),可组合函数可能没有足够的信息来组合模态底页。动态列内容就是这种情况(从没有内容开始,所以高度 = 0 dp)。
要解决此问题,请在工作表内容层次结构中的某处设置 1 dp 的最小高度:
val sheetState = rememberModalBottomSheetState(ModalBottomSheetValue.Hidden)
val coroutineScope = rememberCoroutineScope()
ModalBottomSheetLayout(
sheetContent = {
Box(modifier.defaultMinSize(minHeight = 1.dp)) {
/* sheet content */
}
},
sheetState = sheetState,
// modifier = Modifier.fillMaxSize() --> it doesn't change the result
) {
Scaffold {
/* scaffold content */
}
}
最佳答案
对于“空”内容(理解高度为 0 dp 的内容),可组合函数可能没有足够的信息来组合模态底页。动态列内容就是这种情况(从没有内容开始,所以高度 = 0 dp)。
要解决此问题,请在工作表内容层次结构中的某处设置 1 dp 的最小高度:
val sheetState = rememberModalBottomSheetState(ModalBottomSheetValue.Hidden)
val coroutineScope = rememberCoroutineScope()
ModalBottomSheetLayout(
sheetContent = {
Box(modifier.defaultMinSize(minHeight = 1.dp)) {
/* sheet content */
}
},
sheetState = sheetState,
// modifier = Modifier.fillMaxSize() --> it doesn't change the result
) {
Scaffold {
/* scaffold content */
}
}
关于android - Jetpack-Compose ModalBottomSheetLayout 引发 java.lang.IllegalArgumentException,初始状态为 "Hidden",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/68623965/