我发现自己经常在这种情况下。在下面的示例中,我有一些类似于plates的值,我想显示/隐藏它,这取决于它是否为空。但是隐藏它总是失败的,因为当它为空时,什么都不会呈现,而动画就会变成空的虚无。
,我怎样才能使这件事成功?我希望在动画完成之前保持plates 。
AnimatedVisibility(
visible = plates != null,
content = {
if (plates != null) {
// Render plates
} else {
// The animation snaps to nothingness, as opposed to animating out
}
})发布于 2021-12-01 16:15:05
这就是我最后所做的,它的工作与预期的一样!
@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)
}
}
)
}发布于 2021-12-01 15:10:51
动画总是需要内容,即使没有什么可展示的。当内容为空(或为空)时,只需显示Surface:
AnimatedVisibility(
visible = plates != null,
content = {
if (plates != null) {
// Render plates
} else {
Surface(modifier = Modifier.fillMaxSize()) { }
}
})https://stackoverflow.com/questions/70186131
复制相似问题