Compose - AnimatedVisibility 停止工作

发布于 2025-01-11 20:40:11 字数 2377 浏览 0 评论 0原文

我试图向 @Composable 添加动画,突然它停止工作,我想做的动画是在滚动 LazyColumn 时,所以我有这个:

Box(
            modifier = Modifier
                .fillMaxWidth()
                .fillMaxHeight()
                .background(White)
        ) {

            val visible = remember {
                MutableTransitionState(false).apply {
                    targetState = true
                }
            }
            val fabHeight = 64.dp
            val fabHeightPx = with(
                LocalDensity.current
            ) {
                fabHeight.roundToPx().toFloat()
            }
            val fabOffsetHeightPx = remember { mutableStateOf(0f) }
            val nestedScrollConnection = remember {
                object : NestedScrollConnection {
                    override fun onPreScroll(
                        available: Offset,
                        source: NestedScrollSource
                    ): Offset {
                        //Some checks and get the anyValue
                        visible.targetState = anyValue < 0
                        return Offset.Zero
                    }

                    override fun onPostScroll(
                        consumed: Offset,
                        available: Offset,
                        source: NestedScrollSource
                    ): Offset {
                        //Some checks but I assign the value as this
                        visible.targetState = true
                        return super.onPostScroll(consumed, available, source)
                    }
                }
            }
            LazyColumn(
                modifier = Modifier
                    .padding(start = 16.dp, end = 16.dp, top = 16.dp)
                    .nestedScroll(nestedScrollConnection)
                    .align(Alignment.TopCenter),
                verticalArrangement = Arrangement.spacedBy(16.dp),
            ) {
             ....

这是AnimatedVisibility

AnimatedVisibility(
                visible = visible.currentState,
                enter = fadeIn(),
                exit = fadeOut(),
                modifier = Modifier
                    .align(Alignment.BottomCenter)
                    .padding(bottom = 16.dp),
            ) {
                Text(text = "Hello")
            }
        }

我缺少什么?

I was trying to add an animation to a @Composable and suddenly it stop worked, the animation I want to do is when scrolling a LazyColumn so I have this :

Box(
            modifier = Modifier
                .fillMaxWidth()
                .fillMaxHeight()
                .background(White)
        ) {

            val visible = remember {
                MutableTransitionState(false).apply {
                    targetState = true
                }
            }
            val fabHeight = 64.dp
            val fabHeightPx = with(
                LocalDensity.current
            ) {
                fabHeight.roundToPx().toFloat()
            }
            val fabOffsetHeightPx = remember { mutableStateOf(0f) }
            val nestedScrollConnection = remember {
                object : NestedScrollConnection {
                    override fun onPreScroll(
                        available: Offset,
                        source: NestedScrollSource
                    ): Offset {
                        //Some checks and get the anyValue
                        visible.targetState = anyValue < 0
                        return Offset.Zero
                    }

                    override fun onPostScroll(
                        consumed: Offset,
                        available: Offset,
                        source: NestedScrollSource
                    ): Offset {
                        //Some checks but I assign the value as this
                        visible.targetState = true
                        return super.onPostScroll(consumed, available, source)
                    }
                }
            }
            LazyColumn(
                modifier = Modifier
                    .padding(start = 16.dp, end = 16.dp, top = 16.dp)
                    .nestedScroll(nestedScrollConnection)
                    .align(Alignment.TopCenter),
                verticalArrangement = Arrangement.spacedBy(16.dp),
            ) {
             ....

And this is the AnimatedVisibility

AnimatedVisibility(
                visible = visible.currentState,
                enter = fadeIn(),
                exit = fadeOut(),
                modifier = Modifier
                    .align(Alignment.BottomCenter)
                    .padding(bottom = 16.dp),
            ) {
                Text(text = "Hello")
            }
        }

What I'm missing?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

丑疤怪 2025-01-18 20:40:11

尝试使用:

var visible by remember { mutableStateOf(false) }

AnimatedVisibility 可对简单的布尔值做出反应。

Try using:

var visible by remember { mutableStateOf(false) }

The AnimatedVisibility works reacting to a simple boolean value.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文