首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何使用可空值的AnimatedVisibility?

如何使用可空值的AnimatedVisibility?
EN

Stack Overflow用户
提问于 2021-12-01 14:37:08
回答 2查看 58关注 0票数 1

我发现自己经常在这种情况下。在下面的示例中,我有一些类似于plates的值,我想显示/隐藏它,这取决于它是否为空。但是隐藏它总是失败的,因为当它为空时,什么都不会呈现,而动画就会变成空的虚无。

,我怎样才能使这件事成功?我希望在动画完成之前保持plates

代码语言:javascript
复制
    AnimatedVisibility(
        visible = plates != null,
        content = {
            if (plates != null) {
                // Render plates
            } else {
                // The animation snaps to nothingness, as opposed to animating out
            }
        })
EN

回答 2

Stack Overflow用户

发布于 2021-12-01 16:15:05

这就是我最后所做的,它的工作与预期的一样!

代码语言:javascript
复制
@Composable
inline fun <T> AnimatedValueVisibility(
    value: T?,
    enter: EnterTransition = fadeIn(
        animationSpec = FloatSpec
    ) + expandVertically(
        animationSpec = SizeSpec,
        clip = false
    ),
    exit: ExitTransition = fadeOut(
        animationSpec = FloatSpec
    ) + shrinkVertically(
        animationSpec = SizeSpec,
        clip = false
    ),
    crossinline content: @Composable (T) -> Unit
) {
    val ref = remember {
        Ref<T>()
    }

    ref.value = value ?: ref.value

    AnimatedVisibility(
        visible = value != null,
        enter = enter,
        exit = exit,
        content = {
            ref.value?.let { value ->
                content(value)
            }
        }
    )
}
票数 1
EN

Stack Overflow用户

发布于 2021-12-01 15:10:51

动画总是需要内容,即使没有什么可展示的。当内容为空(或为空)时,只需显示Surface:

代码语言:javascript
复制
AnimatedVisibility(
        visible = plates != null,
        content = {
            if (plates != null) {
                // Render plates
            } else {
                Surface(modifier = Modifier.fillMaxSize()) { }
            }
        })
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/70186131

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档