You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Material3 ModalBottomSheet旋转设备时崩溃问题求助

Material3 ModalBottomSheet承载网格布局时旋转设备崩溃问题

旋转设备时应用崩溃,报错信息:

java.lang.IllegalStateException: The offset was read before being initialized. Did you access the offset in a phase before layout, like effects or composition?

复现代码(LazyVerticalGrid实现)

使用Material3的ModalBottomSheet承载包含21项的LazyVerticalGrid时触发崩溃:

val bottomSheetState = rememberModalBottomSheetState()
val colors = List(21) { Color.Blue }

ModalBottomSheet(
    modifier = Modifier.widthIn(max = 450.dp),
    onDismissRequest = {},
    sheetState = bottomSheetState
) {
    LazyVerticalGrid(
        columns = GridCells.Fixed(7),
        verticalArrangement = Arrangement.spacedBy(10.dp),
        horizontalArrangement = Arrangement.spacedBy(10.dp),
    ) {
        items(colors) { color ->
            Box(
                modifier = Modifier
                    .background(color = color)
                    .aspectRatio(1f)
            )
        }
    }
}

验证非LazyVerticalGrid问题(Column实现网格)

将数据项减少到14个时崩溃不会发生,但改用固定尺寸的Column实现网格布局,同样会触发崩溃:

val numRows = 3
val numCols = 7
val circleSize = 48.dp
val circleSpacing = 8.dp

...
Column() {    // 替代LazyVerticalGrid
    for (row in 0 until numRows) {
        Row(
            modifier = Modifier.fillMaxWidth(),
            horizontalArrangement = Arrangement.SpaceEvenly
        ) {
            for (col in 0 until numCols) {
                val colorIndex = row * numCols + col
                if (colorIndex < colors.size) {
                    Box(
                        modifier = Modifier
                            .size(circleSize)
                            .background(colors[colorIndex], shape = CircleShape)
                    )
                }
            }
        }
        Spacer(modifier = Modifier.height(circleSpacing))
    }
}
...

疑问:这会不会是因为ModalBottomSheet仍处于实验阶段的API问题?

内容的提问来源于stack exchange,提问作者Wilfred

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.08 12:17:46