Hey Everyone ! I try to generate UI with Compose b...
# compose
f
Hey Everyone ! I try to generate UI with Compose based on Json, but I'm struggling with assigning scope specific modifiers like
weight
which are only available within Row and Column scopes. Is there dynamic way of telling composables that they are currently in a specific scope? I tried utilizing
context(RowScope)
annotation for methods, but that requires me to create a method for each scope. Is there any better flexible way? Basically
if( parent is Row) { add 'weight' to current composable }
Thank you 🙏
🚫 1
1
e
Do you have a code sample of where you're trying to get the row scope?
if(this is RowScope)
should work?
f
Thx for your attempt to help Elliot. I was able to find a workaround that worked so far by passing down a Modifier to its children. Let me share some sample code
Copy code
@Composable
fun BuildRowLayout(
    modifier: Modifier,
    content: List<Pair<@Composable (Modifier) -> Unit, Component>>?,
    component: Component,
) {
    Row(
        modifier = modifier.applyStyle(
            style = component.styles,
        ),
        ...
    ) {
        content?.map {
            if (requiresWeight(it.second.styles)) {
                it.first.invoke(Modifier.weight(1f))
            } else {
                it.first.invoke(Modifier)
            }
        }
    }
}
z
Passing modifiers through like this isn’t a workaround, it’s the core design and how modifiers are intended to be used.
❤️ 1
If you don’t want to make your function param take a modifier parameter, you can wrap your invocation in a Box with
propagateMinConstraints=true
.
👀 1
(And pass the modifier to the box, forgot to mention the important part)