Stylianos Gakis
03/07/2022, 5:01 PMLayout {} which may accept arbitrary composable slots, and I want to be able to add a modifier to them to add a layoutId how would I go around doing that?Stylianos Gakis
03/07/2022, 5:01 PM@Composable
fun MyLayout(
slot1: @Composable (Modifier),
slot2: @Composable (Modifier),
) {
Layout(
content = {
slot1(Modifier.layoutId("slot1"))
slot2(Modifier.layoutId("slot2"))
},
) { measurables, constraints ->
val slot1Placeable = measurables.first { it.layoutId == "slot1" }.measure(constraints)
val slot2Placeable = measurables.first { it.layoutId == "slot2" }.measure(constraints)
// etc.
}
}
But this makes it awkward to use on the call-site, since I have to make it a lambda that receives a modifier and I can’t just call other composables easier, I have to make it look like { modifier -> WhateverComposable(modifier) }
One thing I can see doing is replacing slot1(Modifier.layoutId("slot1")) with
Box(Modifier.layoutId("slot1")) { slot1() }
Is this like weird? Feels a bit like it. Does it only work since Box is inline? Is there maybe some better approach 🤔Adam Powell
03/07/2022, 6:59 PMAdam Powell
03/07/2022, 6:59 PMAdam Powell
03/07/2022, 7:00 PMAdam Powell
03/07/2022, 7:00 PMlayoutId as the implementation detail or go all the way to defining your own ParentData for itStylianos Gakis
03/07/2022, 8:53 PMStylianos Gakis
03/07/2022, 8:53 PMParentData being mentioned once before again and I again didn’t really get it. Do you happen to have some sort of application of that on https://cs.android.com/ maybe? Something that preferably is build to showcase this functionality instead of where it’s actually used, because it the usually becomes quite hard to understand some things when too many concepts are mixed together.Adam Powell
03/07/2022, 10:11 PMAdam Powell
03/07/2022, 10:11 PMAdam Powell
03/07/2022, 10:12 PMAdam Powell
03/07/2022, 10:13 PMMeasurable here with some small utilities: https://cs.android.com/androidx/platform/frameworks/support/+/androidx-main:compose/[…]in/androidx/compose/foundation/layout/Box.kt;l=258?q=BoxScopeAdam Powell
03/07/2022, 10:13 PMAdam Powell
03/07/2022, 10:14 PMlayoutId modifier you could do that here as a shortcut, provided you have no reason to use a layoutId for anything else in the same parentAdam Powell
03/07/2022, 10:14 PM= layoutId("foo")Adam Powell
03/07/2022, 10:14 PM