最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

android - StackCard with Jetpack Compose - Stack Overflow

programmeradmin3浏览0评论

I’m working on developing a screen where, when scrolling some cards, they should stack at the top. I’m almost there, but I’m facing an issue: when the cards reach the end of the screen, they almost stack and then disappear. What I need is for them to remain stacked.

Can anyone help me identify what’s wrong with my code?

Here’s my code:

@Composable
fun CardView(color: Color) {
    Card(
        modifier = Modifier
            .fillMaxWidth()
            .height(500.dp),
        shape = RoundedCornerShape(15.dp),
        elevation = CardDefaults.cardElevation(defaultElevation = 5.dp),
        colors = CardDefaults.cardColors(
            containerColor = Color.Blue
        )
    ) {
        Box(
            modifier = Modifier
                .fillMaxSize()
                .background(color)
        )
    }
}

@Composable
fun HomeView() {
    val scrollState = rememberScrollState()
    val color = listOf(
        Color.Blue,
        Color.Red,
        Color.Gray,
        Color.Green
    )
    Column(
        modifier = Modifier
            .fillMaxSize()
            .verticalScroll(scrollState)
            .padding(top = 50.dp)
    ) {
        repeat(4) { index ->
            var cardY by remember { mutableStateOf(0f) }
            var cardHeight by remember { mutableStateOf(0f) }

            Box(
                modifier = Modifier
                    .offset(y = (-15).dp)
                    .onGloballyPositioned { coordinates ->
                        cardY = coordinates.positionInWindow().y
                        cardHeight = coordinates.size.height.toFloat()
                    }
                    .graphicsLayer {
                        val minY = if (cardY < 0) -cardY else 0f

                        val maximumYaxis = cardY + cardHeight
                        val height = 200f
                        val progressCard = if (minY == 0f) {
                            0f
                        } else {
                            (1f - (maximumYaxis / height)).coerceIn(0f, 1f)
                        }

                        val scaleValue = 1f - (progressCard * 0.1f)
                        scaleX = scaleValue
                        scaleY = scaleValue

                        translationY = minY + (-progressCard * 20f)

                    }
            ) {
                CardView(color[index])
            }
        }
    }
}

How I want it to work. iOS example:

发布评论

评论列表(0)

  1. 暂无评论