s3rius
01/30/2024, 10:59 AMAnimatedContent to show a transition, like below:
data class Stats(val name: String, val status: String)
val someStats = Stats(name = "Bob", status = "Walking")
AnimatedContent(
state = someStats
transitionSpec = coolSwipeInAnimation()
) { stats ->
Card {
Text(stats.name)
Text(stats.status)
}
}
The issue is: I only want to run the transition when someStats.name changes. someStats.status changes fairly frequently and shouldn't trigger the transition animation.
I can't change to AnimatedContent(state = someStats.name) because then I wouldn't be able to access the status inside the block anymore.
I also can't pull Text(stats.status) out of the AnimatedContent, because the whole Card should be animated when the transition runs.
Conceptionally, I'm looking for something like this:
AnimatedContent(
state = someStats,
compareBy = { state.name },
transitionSpec = coolSwipeInAnimation()
) { stats ->
// stuff
}
How could I best go about it?MR3Y
01/30/2024, 11:19 AMAnimatedContent takes contentKey parameter which lets you control what triggers an animation, if two states have the same contentKey then there won't be any animation running when changing from one to another.
So, you'll need to set contentKey to state.name
https://developer.android.com/reference/kotlin/androidx/compose/animation/package-summary#AnimatedContent(kotlin.Any,[…].Function1,kotlin.Function2)s3rius
01/30/2024, 12:03 PMcontentKey because my brain jumped to the ``contentType`` from LazyRow/Col and I disregarded it.
Thanks!