https://kotlinlang.org logo
#compose
Title
# compose
d

dimsuz

04/06/2022, 4:44 PM
is there some kind of
immutableStateOf(value)
? Usecase:
Copy code
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
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
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
() -> T
lambda instead of trying to construct a
State<T>
around something
✔️ 1
d

dimsuz

04/07/2022, 1:14 PM
I see, that's a nice bonus!
2 Views