Tha*_*ith 25 android kotlin android-jetpack-compose
通过在 Compose 中使用 ModalBottomSheet,我遇到以下问题:
java.lang.IllegalArgumentException:初始值必须有关联的锚点。
我的可组合函数有:
// 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 */
}
}
Run Code Online (Sandbox Code Playgroud)
通过将底部工作表的初始状态设置为 ModalBottomSheetValue.Expanded,问题就消失了。请注意,ModalBottomSheetValue.HalfExpanded 也会引发异常,并且没有任何初始值(默认值是 Hidden,因此看起来符合逻辑)。
是否有已知的解决方法或正在运行的库版本(我使用的 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 */
}
}
Run Code Online (Sandbox Code Playgroud)
Tha*_*ith 38
对于“空”内容(理解高度为 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 */
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
7313 次 |
| 最近记录: |