Hey there, I'm using `AnimatedContent` to show a t...
# compose
s
Hey there, I'm using
AnimatedContent
to show a transition, like below:
Copy code
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:
Copy code
AnimatedContent(
    state = someStats,
    compareBy = { state.name },
    transitionSpec = coolSwipeInAnimation()
) { stats ->
    // stuff
}
How could I best go about it?
1
m
AnimatedContent
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)
gratitude thank you 1
s
Ah jeez, I skimmed over
contentKey
because my brain jumped to the ``contentType`` from LazyRow/Col and I disregarded it. Thanks!