val state = when (orientation) {
Orientation.Landscape -> animateDpAsState(32.dp)
Orientation.Portrait -> immutableStateOf(16.dp)
}
I could use
mutableStateOf(16.dp)
of course, just thought maybe something more lightweight exists.
a
Adam Powell
04/06/2022, 5:27 PM
why would you use a constant argument to
animateDpAsState
? 🤔
d
dimsuz
04/06/2022, 5:38 PM
do you mean 32.dp above? or the
state
value itself?
I want to animate only in landscape, othewise component is the same:
Copy code
MyComponent(offset = state.value) // animates offset only in landscape, otherwise it's always 16.dp
dimsuz
04/06/2022, 5:56 PM
ah, actually there's an
if
there, sorry:
animateDpAsState(if (someFlag) 0.dp else 32.dp)
. otherwise the question is still valid.
a
Adam Powell
04/06/2022, 7:41 PM
I'd probably fold it into the
when
with something like
Copy code
animateDpAsState(
when (orientation) {
Landscape -> if (someFlag) 0.dp else 32.dp
Portrait -> 16.dp
}
)
and as a bonus you can get animation between those two for things like window resizing changing the orientation
👍 1
👍🏽 1
Adam Powell
04/06/2022, 7:42 PM
but no, there's no
immutableStateOf
and you should never need one. Either use the constant directly, or if you're making a late-binding decision and you want a snapshot read to occur more deeply in your UI somewhere, use a