Jetpack-Compose ModalBottomSheetLayout 抛出 java.lang.IllegalArgumentException 与初始状态"Hidden"



通过在Compose中使用ModalBottomSheet,我得到了以下问题:

. lang。IllegalArgumentException:初始值必须有关联的锚。

我的组合函数有:

  1. 一个ModalBottomSheetState和一个CoroutinScope,
  2. ModalBottomSheetLayout with,
  3. 一个脚手架作为底层内容。
// 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。展开后,问题就消失了。注意,这个异常也会被ModalBottomSheetValue抛出。半扩展且没有任何初始值(默认为隐藏,因此看起来合乎逻辑)。

是否有一个已知的解决方案或一个版本的库在那里工作(我使用的版本:1.0.0和我尝试1.0.0-rc2) ?

经过调查,问题似乎是由于表单内容中的一个动态内容。我有一个列/懒列,重组时,数据是可用的。通过有一个固定的内容,这个问题消失了任何ModalBottomSheetValue。

修复

用">

; null"内容(理解高度为0 dp的内容),可组合函数可能没有足够的信息来组合模态底部表单。动态列内容就是这种情况(开始时没有内容,所以height = 0 dp)。

要解决这个问题,在工作表内容层次结构的某个地方设置最小高度为1dp:

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 */
}
}

With "null"内容(理解高度为0 dp的内容),可组合函数可能没有足够的信息来组合模态底部表单。动态列内容就是这种情况(开始时没有内容,所以height = 0 dp)。

要解决这个问题,在工作表内容层次结构的某个地方设置最小高度为1dp:

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 */
}
}

您可以在sheetContent中添加Spacer以避免超出预期

sheetContent = {
Spacer(modifier = Modifier.height(1.dp))
Box() {
/* sheet content */
}
}

对我来说,批准的解决方案不起作用。

在我的情况下,它是失败的,当我有一个未指定的高度(lazyColumn)的底部表内容,我想底部表包装的内容的高度通过使用这一行打开:

sheetState.animateTo(ModalBottomSheetValue.HalfExpanded)

对我来说,解决方案是将sheetContent包装为:

Column(Modifier.fillMaxSize()).

我希望这个解决方案可以帮助别人,这里是伪代码:

val sheetState = rememberModalBottomSheetState(ModalBottomSheetValue.Hidden)
val coroutineScope = rememberCoroutineScope()

ModalBottomSheetLayout(
sheetContent = {
Column(modifier.fillMaxSize()) { ------> The solution
/* sheet content */
}
},
sheetState = sheetState,

) {
Scaffold {
/* scaffold content */
}
}

此问题已在1.4.0-alpha04版本中修复

修复了如果工作表内容为空,ModalBottomSheetLayout会崩溃的问题。ModalBottomSheetLayout现在允许空的表单内容。如果工作表内容为空,它将只有一个隐藏状态。(Ic2288, b/200980998, b/216693030)

最新更新