What is an idiomatic way to write "slot-based" cus...
# compose
d
What is an idiomatic way to write "slot-based" custom layouts? For example I want to have 3 slots:
Copy code
Layout(content = { leftSlot(); centerSlot(); rightSlot(); }) { measurables, constraints -> ... }
And layout logic is based on the assumption that I'll have 3 measureables: for example I want to always expand
centerSlot
. But I've found that if a
centerSlot()
composable will not emit any UI (return Unit), I'll have
measurables.size == 2
, and also I won't know which one of them is missing. For now I solved this by wrapping each slot in a box (e.g.
Box { leftSlot() }
), but I'm not sure if this is the best way.
r
Have a look at the
androidx.compose.material.TextField
code. If you go down the rabbit hole enough levels you end up in
private fun IconsWithTextFieldLayout
, which does a call to
layout()
. Please notice that it passes a variable set of composables in the content argument. But they have a
Modifier.layoutId(SomeId)
so that they can be found again inside the measurePolicy block.
d
Oh,
layoutId
is the thing I've unsuccessfully tried to remember yesterday 🙂 Thank you, will look into this example you've suggested.
a
If you want to treat the contents of each slot as single units like that then yes, you'll want to call each one in an appropriate sub-layout and work with those groups.
👍 2