bodo
12/23/2020, 10:41 AM@Composable
fun ShowAnimatedHeader(oldTitle: String, newTitle: String) {
Box {
AnimatedVisibility(visible = false, exit = slideOutVertically()) {
Text(text = oldTitle)
}
AnimatedVisibility(visible = true, enter = slideInVertically()) {
Text(text = newTitle)
}
}
}
Every time the oldTitle or newTitle change i want to animate the old one out and the new one in. but it is only executed the first time the composable is calledAdam Powell
12/23/2020, 5:58 PMAdam Powell
12/23/2020, 6:00 PMAdam Powell
12/23/2020, 6:01 PMContentSlider(newTitle) { title ->
Text(title)
}
Doris Liu
12/23/2020, 7:47 PMvisible
booleans for both based on which one is showing. Though based on your description, it sounds more like the titles can change arbitrarily, in which case I'd go with Adam's suggestion above. We were considering an API like fun <T> AnimatedVisibility(contentState: T, enter:.., exit:.., content: @Composable (T) -> Unit)
, which would solve use cases like this. Unfortunately we have not got around to building it. The shape of that API coincides with the ContentSlider
composable proposed above.
One tricky detail to note is that if you are changing the titles arbitrarily, it's best to keep track of all of the old titles that haven't finished animating yet. By only tracking oldTitle
and newTitle
, you might end up having an animating old title snaps to disappearance when it was replaced by a different oldTitle. "Composition as a value" that Adam mentioned could potentially allow us to track all of those old composition values as we animate them out. But until then, you might want to be aware of this.Adam Powell
12/23/2020, 8:00 PMContentSlider
there using the new updateTransition
API I didn't find it 😅 that was the first thing I reached for; I was hoping I had found a good excuse to take it for a spinDoris Liu
12/23/2020, 8:12 PMAdam Powell
12/24/2020, 4:42 AMAdam Powell
12/24/2020, 4:43 AMGraphicsLayerScope
should itself implement `Density`; something to file for the new year 😄bodo
12/24/2020, 8:35 PM