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

使用BottomSheetScaffold时如何让LazyColumn末尾内容不被底部面板遮挡

解决BottomSheetScaffold中LazyColumn底部内容被常驻面板遮挡的方案

该问题的核心原因是BottomSheetScaffold的主体内容区域默认填充整个父容器高度,不会自动预留sheetPeekHeight对应的底部空间,因此LazyColumn滚动到底部时,末尾元素会被常驻显示的底部面板覆盖,可通过以下两种方案解决:

  • 方案1:固定底部padding适配
    如果你的sheetPeekHeight是固定值,直接给LazyColumn添加对应高度的底部padding即可:
@Composable
fun MainContent() {
    LazyColumn(
        contentPadding = PaddingValues(bottom = 100.dp)
    ) {
        items(count = 5) { itemIndex ->
            when (itemIndex) {
                0 -> {
                    Image(
                        modifier = Modifier
                            .fillMaxWidth()
                            .height(100.dp), 
                        contentDescription = "test",
                        painter = painterResource(id = R.drawable.image)
                    )
                }
                else -> {
                    Text(
                        text = "列表项 $itemIndex",
                        modifier = Modifier.padding(16.dp).fillMaxWidth()
                    )
                }
            }
        }
    }
}
  • 方案2:使用Scaffold内置padding参数适配(更推荐)
    BottomSheetScaffold的内容回调默认传入已自动计算好的innerPadding参数,包含状态栏、底部常驻面板的预留高度,直接应用该参数即可,后续修改sheetPeekHeight无需手动调整padding值:
@ExperimentalMaterialApi
@Composable
fun HomeScreen() {
    val bottomSheetScaffoldState = rememberBottomSheetScaffoldState(
        bottomSheetState = BottomSheetState(BottomSheetValue.Collapsed)
    )
    
    BottomSheetScaffold(
        scaffoldState = bottomSheetScaffoldState,
        sheetContent = {
            Text("this is test", modifier = Modifier.fillMaxWidth().height(60.dp))
        },
        sheetPeekHeight = 100.dp,
        sheetShape = RoundedCornerShape(topEnd = 52.dp, topStart = 52.dp),
        backgroundColor = Color.White
    ) { innerPadding ->
        MainContent(contentPadding = innerPadding)
    }
}

@Composable
fun MainContent(contentPadding: PaddingValues) {
    LazyColumn(
        contentPadding = contentPadding,
        // 如需适配软键盘,额外添加imePadding即可
        modifier = Modifier.imePadding()
    ) {
        items(count = 5) { itemIndex ->
            when (itemIndex) {
                0 -> {
                    Image(
                        modifier = Modifier
                            .fillMaxWidth()
                            .height(100.dp), 
                        contentDescription = "test",
                        painter = painterResource(id = R.drawable.image)
                    )
                }
            }
        }
    }
}

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.09.29 01:36:05