I have a custom layout where I have something like...
# compose
d
I have a custom layout where I have something like
Copy code
MyLayout(slot = { visible -> AnimatedVisibility(visible) { ... } })
and the logic is such that I want that
visible
to be calculated based on available height and slot size: I measure my "slot" then if it fits, I pass visibility =
true
, otherwise
false
. I wanted to use
SubcomposeLayout
for this but turns out that to calculate
visible
I need to
subcompose/measure
that slot and to
subcompose/measure
it I need to calculate
visible
🐔 🥚 Is this possible? Inner State + recompose?
a
No, you've created an infinite data dependency loop. You need the measured size of your content to decide the parameter to pass to generate that content to measure. Don't do that. 🙂
d
Yeah, seems like it. How would I solve this though? I have an animated visibility of content which I want to determine based on the layout size. Use case: zoom buttons on the map (aka Google Maps). If non-modal bottom sheet opens and insets content so that zoom buttons do not fit, I'd like to fade them away. After it hides: fade them back in.
a
so you have a single element (the container for the zoom buttons) that you would like to disappear when there isn't enough space available to show them? That's doable at the layout phase when the right information is available. If you use
Layout
or
Modifier.layout
you can choose not to place an element and it will not appear
as for the animation, that gets into questions of desired UX and you may want to start such an animation before the available space is known. During placement you can
.placeWithLayer
to set an alpha value for the placeable
a useful thought experiment for how to implement this for your use case is to consider what should happen if the available space immediately changes such that there isn't enough available to show the zoom buttons. There isn't time to animate before something doesn't fit. How does it behave and why?
d
Yeah, I basically have 3 button sections: top, bottom and middle. Zoom controls are in the middle and only they should be animated (supposedly). My custom layout currently does exactly what you've said in message #1: doesn't place them (without animation). I didn't know about
placeWithLayer
will check it out. And I'll also run the thought experiment, thanks!
👍 1
a
If you're okay with awkward placement/clipping during the animation sometimes then that's one avenue, if not, you'll want to think through what your animation signals really are. For example you might want to have the animation be orthogonal to the placement of the buttons; you might want to animate the alpha of the buttons based on something else happening in the UI that also incidentally changes the size available via an animation, and the placement is more of a backstop
d
Oh, all nice points. Thank you for summing up/clarifying!
👍 1