Ilan Sasportas
04/12/2023, 1:31 PMAnimatedContent
to animate my content between each type of state.
@Composable
fun MyComposable(content: MyState, modifier: Modifier) {
AnimatedContent(
targetState = content,
transitionSpec = { fadeIn(animationSpec = tween(500)) with fadeOut(animationSpec = tween(500)) }
) { targetState, contentModifier ->
when (targetState) {
is MyState.Loading -> LoadingContent(contentModifier)
is MyState.Success -> SuccessContent(
model = targetState.successModel,
modifier = contentModifier
)
is MyState.Error -> ErrorContent(
model = targetState.errorModel,
modifier = contentModifier
)
}
}
}
Unfortunately the animation is triggered at each state change, even between 2 Success for instance, which I don’t want. Any idea how can I trigger animation only between Loading / Success / Error types ?Thomas Urbanitsch
04/12/2023, 2:00 PMJoel Denke
04/12/2023, 2:01 PMIlan Sasportas
04/12/2023, 2:02 PMOleksandr Balan
04/12/2023, 2:16 PMcontentKey
. However it is not available in AnimatedContent
without a receiver, don’t know why 🤔
But you could create own transition
and use extension function AnimatedContent
.
I would specify a state class as a key, this should not animate between success states.
val content: MyState = ...
val transition = updateTransition(targetState = content)
transition.AnimatedContent(
contentKey = { content.javaClass }
) {
}
Joel Denke
04/12/2023, 2:17 PMIlan Sasportas
04/12/2023, 2:26 PMDoris Liu
04/12/2023, 6:19 PMcontentKey
would be serving for an uncommon use case, which is why it is not a part of the AnimatedContent API without receiver to avoid confusion. But it seems the need for it is more common than we initially thought. 🙂 I'll look into adding it to all the AnimatedContent APIsIlan Sasportas
04/12/2023, 6:21 PM