Garret Yoder
03/20/2024, 2:15 PMobject?.theComposeMethod()
and it'd only display if it was not null, and would obviously display whatever the current object is.
Now that I animated it, I didn't even think about the fact that
AnimatedVisibility(object!=null) {
object?.theComposeMethod()
}
will work only for the enter animation, not the exit because the value will immediately go null and remove from composition.
There's two things I want to accomplish - animate a fade when the value changes, and get it to animate when it's removed. I really want to avoid rewriting a lot upstream of that just for a silly animation, I feel like the answer is no because once it's null its out of composition - but does anyone have a good suggestion?vide
03/20/2024, 2:18 PMAnimatedContent
vide
03/20/2024, 2:19 PMGarret Yoder
03/20/2024, 2:22 PMvide
03/20/2024, 2:27 PMGarret Yoder
03/20/2024, 2:28 PMGarret Yoder
03/20/2024, 2:28 PMvide
03/20/2024, 2:29 PMvide
03/20/2024, 2:30 PMGarret Yoder
03/20/2024, 2:30 PMGarret Yoder
03/20/2024, 2:37 PMtransitionSpec = { fadeIn() togetherWith fadeOut() }
but it seems like it still does a scale animation when the value actually switches between null/not null. The value just changing between objects does do a fade.Garret Yoder
03/20/2024, 2:41 PMvide
03/20/2024, 2:42 PMvide
03/20/2024, 2:43 PMpublic abstract infix fun ContentTransform.using(
sizeTransform: SizeTransform?
): ContentTransform
vide
03/20/2024, 2:43 PMfadeIn() togetherWith fadeOut() using null
Garret Yoder
03/20/2024, 2:44 PMGarret Yoder
03/20/2024, 2:44 PMGarret Yoder
03/20/2024, 2:48 PMtransitionSpec
it's manually specifying that scale animation. Tacking on an animation by default that wasn't specified is also a bit counterintuitive.Mofe Ejegi
03/20/2024, 4:44 PMAnimatedVisibility
composable setup as it is, you can try out this simple modification:
@Composable
fun <T : Any> NullableAnimatedVisibility(
nullableArgument: T?,
modifier: Modifier = Modifier,
enter: EnterTransition = fadeIn() + expandIn(),
exit: ExitTransition = shrinkOut() + fadeOut(),
content: @Composable AnimatedVisibilityScope.(T) -> Unit,
) {
var nonNullableArgument by remember {
mutableStateOf(nullableArgument)
}
LaunchedEffect(nullableArgument) {
nullableArgument?.let { nonNullableArgument = it }
}
AnimatedVisibility(
visible = nullableArgument != null,
modifier = modifier,
enter = enter,
exit = exit,
) {
nonNullableArgument?.let { content(it) }
}
}
It remembers the object's state so it doesn't disappear immediately it's set to null
.
There's also a way to forget the arg after the content is finally animated away, but this should be sufficient for what you're asking.