android - 如何使用 Jetpack Compose 将 BottomSheetScaffold 扩展到特定高度?

标签 android android-layout android-jetpack-compose bottom-sheet

有没有办法扩展BottomSheetScaffold到特定高度?
我的BottomSheetScaffold的内容是一个很大的列表,因此它会扩展到全屏。
无论内容如何,​​我都没有找到始终扩展到特定高度的方法。

最佳答案

您可以使用 heightIn(min = 100.dp, max = 500.dp)在工作表内容上

   val bottomSheetScaffoldState = rememberBottomSheetScaffoldState(
        bottomSheetState = BottomSheetState(BottomSheetValue.Collapsed)
    )

    BottomSheetScaffold(
        scaffoldState = bottomSheetScaffoldState,
        sheetElevation = 8.dp,
        sheetShape = RoundedCornerShape(
            bottomStart = 0.dp,
            bottomEnd = 0.dp,
            topStart = 12.dp,
            topEnd = 12.dp
        ),
        sheetContent = {
            SheetContent()
        },
        // This is the height in collapsed state
        sheetPeekHeight = 70.dp
    ) {
        MainContent(bottomSheetScaffoldState.bottomSheetState)
    }

@Composable
private fun SheetContent() {
    Column(modifier = Modifier.heightIn(min = 100.dp, max = 500.dp)) {
        Spacer(modifier = Modifier.height(16.dp))

        Text(
            text = "Places to Visit",
            textAlign = TextAlign.Center,
            fontWeight = FontWeight.Bold,
            color = Color(0xffFDD835),
            fontSize = 24.sp,
            modifier = Modifier.padding(8.dp)
        )
        LazyColumn(
            contentPadding = PaddingValues(8.dp),
            verticalArrangement = Arrangement.spacedBy(8.dp)
        ) {
            items(places) { place ->
                PlacesToBookVerticalComponent(place = place)
            }
        }
    }
}
如果有兴趣查看状态和滚动位置,您可以查看它们
@ExperimentalMaterialApi
@Composable
private fun MainContent(bottomSheetState: BottomSheetState) {

    val direction = bottomSheetState.direction
    val currentValue: BottomSheetValue = bottomSheetState.currentValue
    val targetValue: BottomSheetValue = bottomSheetState.targetValue
    val overflow = bottomSheetState.overflow.value
    val offset = bottomSheetState.offset.value

    val progress = bottomSheetState.progress
    val fraction = progress.fraction
    val from = progress.from.name
    val to = progress.to.name

    Column(
        modifier = Modifier
            .fillMaxSize()
            .background(Color(0xff6D4C41))
            .padding(top = 30.dp)
    ) {
        Text(
            color = Color.White,
            text = "direction:$direction\n" +
                    "isExpanded: ${bottomSheetState.isExpanded}\n" +
                    "isCollapsed: ${bottomSheetState.isCollapsed}\n" +
                    "isAnimationRunning: ${bottomSheetState.isAnimationRunning}"
        )

        Text(
            color = Color.White,
            text = "currentValue: ${currentValue}\n" +
                    "targetValue: ${targetValue}\n" +
                    "overflow: ${overflow}\n" +
                    "offset: $offset"
        )

        Text(
            color = Color.White,
            text = "progress: $progress\n" +
                    "fraction: ${fraction}\n" +
                    "from: ${from}\n" +
                    "to: $to"
        )
    }
}
enter image description here

关于android - 如何使用 Jetpack Compose 将 BottomSheetScaffold 扩展到特定高度?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/69529798/

相关文章:

android - 使用撰写文本链接

android - Android中的编码样式:什么时候应该使用样式而不是内联属性?

android - 如何从微调器下拉选项中自定义样式?

android - Jetpack 组合和 fragment

Android 虚拟设备不工作

android - 使用默认身份验证在 FB 上发布照片并获取照片已发布的消息的简单技术

android - 如何在 Jetpack Compose 中集成自动填充功能

android - 在将控制权转移到端点 0 之前,您是否必须声明一个接口(interface)?

java - 在Android上使用jxl修改excel电子表格

android - 通过 Android 使用 radio 或其他信号跟踪手机在现实世界中的位置