mmaillot
03/25/2021, 10:13 AMwhen
in the composable function Bubble(bubble: Bubble)
which break the padding animation. If I directly use the fun Bubble(isBig: Boolean)
, the padding animation works. Why a when
condition will break the animation ? (here my case is simple, it’s a boolean but it can be a sealed class).
Edit: put code in a gist.Filip Wiesner
03/25/2021, 10:15 AMmmaillot
03/25/2021, 10:18 AMmmaillot
03/25/2021, 10:20 AM@Composable
fun Bubble(bubble: Bubble) {
if (bubble.isBig) {
Bubble(isBig = true)
} else {
Bubble(isBig = false)
}
}
Animation doesn’t work...
If I don’t use a condition, it works:
@Composable
fun Bubble(bubble: Bubble) {
Bubble(isBig = bubble.isBig)
}
I probably miss something important with compose and animation, but why ? 😞Albert Chang
03/25/2021, 10:22 AMBubble(isBig = true)
and Bubble(isBig = false)
are 2 different compositions. Recomposition only works when the parameter of the same composable function invocation changes.mmaillot
03/25/2021, 10:28 AMAlbert Chang
03/25/2021, 10:29 AMFilip Wiesner
03/25/2021, 10:30 AMkey
help? 🤔mmaillot
03/25/2021, 10:33 AMkey
?Albert Chang
03/25/2021, 10:33 AMkey
at different points won't work.
The value for a key does not need to be globally unique, and needs only be unique amongst the invocations of key at that point in composition.
Filip Wiesner
03/25/2021, 10:34 AMAlbert Chang
03/25/2021, 10:37 AM@Composable
fun Bubble(bubble: Bubble) {
Bubble(
isBig = when (bubble.isBig) {
true -> true
false -> false
}
)
}
mmaillot
03/25/2021, 10:38 AM@Composable
fun Bubble(bubble: Bubble) {
val padding by animateDpAsState(
targetValue = if (bubble.isBig) 50.dp else 10.dp,
animationSpec = tween(durationMillis = 500, easing = LinearEasing)
)
if (bubble.isBig) {
Bubble(text = "Big bubble", modifier = Modifier.padding(padding))
} else {
Bubble(text = "Small bubble", modifier = Modifier.padding(padding))
}
}
Ok, it works with the animation in the parent. Not very easy to implement.mmaillot
03/25/2021, 10:42 AMComposable
to help compose to reuse it and keeping animation stuff. Ok, not easy to understand at first place but it’s more clear (I think). Thanks you!