I'm having problems importing weight function for ...
# compose
p
I'm having problems importing weight function for Column
Copy code
Column(
    modifier = modifier.weight(1f),
It gives me this error:
Unresolved reference: weight
It gives me 4 import possibilities but none of them do work
c
Is the column in a scope where weight applies, such as another column or a row?
p
good point, not, is a composable function with a column
that composable function is used in some other composables
those composables do have box, column etc...
how to resolve this?
z
One way is to give your composable a RowScope or ColumnScope receiver.
Another way is to pass the weight in from wherever you’re calling your composable, where the row/column scope is available.
p
can I see samples of that somewhere?
didn't learnt that in the codelabs
a
@Pablo
Copy code
@Composable
@Sampled
fun SimpleColumn() {
    Column {
        // The child with no weight will have the specified size.
        Box(Modifier.size(40.dp, 80.dp).background(Color.Magenta))
        // Has weight, the child will occupy half of the remaining height.
        Box(Modifier.width(40.dp).weight(1f).background(Color.Yellow))
        // Has weight and does not fill, the child will occupy at most half of the remaining height.
        // Therefore it will occupy 80.dp (its preferred height) if the assigned height is larger.
        Box(
            Modifier.size(40.dp, 80.dp)
                .weight(1f, fill = false)
                .background(Color.Green)
        )
    }
}
You can find other examples @ https://www.composables.com/foundation-layout/column
p
well there you are simply putting composables inside other composables
I was trying to add unnecessary parent compossables
a
the parent is the one who is deciding where the children will be placed. the
weight()
modifier comes with column/row
Do i understand this right that you want to have a composable that takes the remaining space wherever it is used?
p
no, I just have another fun that calls this fun, and this fun generates a composable that needs to specify a weight, but It can't because it doesn't know who is containing him
r
You can use one of the two solutions @Zach Klippenstein (he/him) [MOD] mentioned above, They are the canonical way of doing this
p
maybe can someone give some samples of that?
r
Copy code
@Composable
fun RowScope.MyComposable(modifier: Modifier = Modifier) {
    Column(modifier = modifier.weight(1f), …) {
        …
    }
}
Or you don't use the
RowScope
(or
ColumnScope
) receiver, remove the
weight(1f)
from inside
MyComposable
and let the caller pass the weight modifier
p
thank you
142 Views