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

manueldidonna

09/14/2020, 12:47 PM
Which is the most performing way to wrap a composable just to add a modifier?
s

Se7eN

09/14/2020, 12:49 PM
I use a
Box
c

caelum19

09/14/2020, 1:48 PM
Can't pass a Modifier parameter?
m

manueldidonna

09/14/2020, 2:20 PM
I can't pass a Modifier to achieve something like this:
Copy code
MenuOptions(callbacks) {
    ComposableToWrap()
}
I use
Box
too
I've found something useful from compose source code ui/platform/Wrapper.kt
Copy code
@Composable
private fun simpleLayout(modifier: Modifier = Modifier, children: @Composable () -> Unit) {
    Layout(modifier = modifier, children = children) { measurables, constraints ->
        val placeables = measurables.map { measurable ->
            measurable.measure(constraints)
        }

        val width = placeables.fold(0) { maxWidth, placeable ->
            max(maxWidth, (placeable.width))
        }

        val height = placeables.fold(0) { minWidth, placeable ->
            max(minWidth, (placeable.height))
        }

        layout(width, height) {
            placeables.forEach { placeable ->
                placeable.place(0, 0)
            }
        }
    }
}
s

Se7eN

09/14/2020, 2:45 PM
m

manueldidonna

09/14/2020, 3:04 PM
@Se7eN why?
Anyway Box will be refactored soon. It will replace Stack
s

Se7eN

09/14/2020, 3:16 PM
Damn I didn't know that. Check out the link it says a box can be used as a wrapper composable, but of course it's not the official docs.
m

manueldidonna

09/14/2020, 3:19 PM
Yeah, I've seen these docs. Now Box under the hood is a Column with same sane defaults. I think we should analyze the performance impact of a box over simpleLayout() to just add a Modifier to a composabe. Maybe it's irrilevant.
👍 1
c

caelum19

09/14/2020, 6:39 PM
What about adding a Modifier as a parameter to the ComposableToWrap function? like children: @Composable (Modifier) -> Unit ?
3 Views