【问题标题】:Jetpack-Compose ModalBottomSheetLayout throws java.lang.IllegalArgumentException with initial state "Hidden"Jetpack-Compose ModalBottomSheetLayout 抛出 java.lang.IllegalArgumentException,初始状态为“隐藏”
【发布时间】:2021-08-02 15:09:06
【问题描述】:

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

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

我的可组合函数有:

  1. ModalBottomSheetState 和 CooutinScope,
  2. ModalBottomSheetLayout 与,
  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.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 */
    }
}

【问题讨论】:

    标签: android kotlin android-jetpack-compose


    【解决方案1】:

    对于“空”内容(理解高度为 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 */
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2023-01-12
      • 1970-01-01
      • 1970-01-01
      • 2019-05-05
      • 2016-12-13
      • 1970-01-01
      • 2021-08-23
      相关资源
      最近更新 更多