简体   繁体   English

在 Jetpack Compose 中将视图固定到底部工作表

[英]Pin a view to Bottom Sheet in Jetpack Compose

I'm trying to implement a persistent Bottom Sheet in Jetpack Compose which has a button that should be pinned to the bottom of this sheet at all time, it shouldn't get dragged with the other contents when expanding.我正在尝试在 Jetpack Compose 中实现一个持久的底部工作表,该工作表有一个按钮,该按钮应始终固定在此工作表的底部,展开时不应与其他内容一起拖动。 I couldn't find an easy way to do it except manipulating other views heights relative to the bottom sheet state which is hard to maintain.除了操纵相对于难以维护的底部工作表状态的其他视图高度之外,我找不到一种简单的方法来做到这一点。 Any idea how can I achieve that?知道如何实现吗?

Here's an illustration for what I'm trying to achieve这是我要实现的目标的说明

Tricky part is getting initial offset of button or sheet content.棘手的部分是获得按钮或工作表内容的初始偏移量。 I did it with a class我上了一堂课

class OffsetWrapper(var offset: Float = 0f)

Then all you need to do is calling Modifier.offset{} on Button with initial position of button minus current offset of bottomSheetState.offset.value然后你需要做的就是在 Button 上调用Modifier.offset{}Button的初始位置减去bottomSheetState.offset.value的当前偏移量

@ExperimentalMaterialApi
@Composable
private fun SheetContent(bottomSheetState: BottomSheetState) {

    val offsetWrapper = remember { OffsetWrapper() }

    val offset = bottomSheetState.offset.value
    if (offsetWrapper.offset == 0f) {
        offsetWrapper.offset = offset
    }

    Column(
        modifier = Modifier.fillMaxSize()
    ) {
        Button(
            modifier = Modifier
                .padding(8.dp)
                .fillMaxWidth()
                .offset {
                    IntOffset(0,
                        (-bottomSheetState.offset.value+ offsetWrapper.offset).toInt()
                    )
                }
            ,
            onClick = { /*TODO*/ }) {
            Text("Button")
        }
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM