Olivier Patry
04/20/2021, 9:41 AMOlivier Patry
04/20/2021, 9:44 AMvar mode by remember { mutableStateOf<GameRules.Mode?>(null) }
val modeLabel = when (mode) {
is Time -> "time"
is Moves -> "moves"
is Endless -> "no limit"
else -> "choose mode"
}
val windowWidth = LocalAppWindow.current.width.toFloat()
val modeLabelOffset = remember { Animatable(-windowWidth) }
LaunchedEffect(mode) {
modeLabelOffset.animateTo(
targetValue = 0f,
animationSpec = tween(durationMillis = 1000, easing = FastOutSlowInEasing)
)
}
Text(
modeLabel,
Modifier.padding(8.dp).offset(x = modeLabelOffset.value.dp),
textAlign = TextAlign.Center
)
Given that I use the mode
as key of my LaunchedEffect
I would expect it to be relaunched.
My understanding is that the modeLabelOffset
state matches the targetValue
so nothing changes. I can't figure out how I can reset such state to its initial value (-windowWidth
) whenever the mode
changes.Olivier Patry
04/20/2021, 10:08 AMsnapTo
) working but I don't know if it's the good way to do it.
LaunchedEffect(mode) {
modeLabelOffset.snapTo(-windowWidth)
modeLabelOffset.animateTo(
targetValue = 0f,
animationSpec = tween(durationMillis = 1000, easing = FastOutSlowInEasing)
)
}
Olivier Patry
04/20/2021, 10:08 AMOlivier Patry
04/20/2021, 11:07 AMAlbert Chang
04/20/2021, 11:18 AMremember(mode) { Animatable(-windowWidth) }
?Olivier Patry
04/20/2021, 11:34 AMremember
could take a key
parameter… Exactly what I wanted!
It seems to work smoothly now, without snapTo
and without glitch.
Thanks @Albert Chang 🙇