Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Jetpack Compose: Unresolved reference 'weight' - Why is the weight-property missing?

I like to put a back-button, which a use in many places in it's own composable-function.

Looks like this:

@Composable
fun BackButton(navController: NavController) {
    Spacer(modifier = Modifier.weight(1f)) // Unresolved reference 'weight'.
    Column(horizontalAlignment = Alignment.Start, modifier = Modifier.fillMaxWidth()) {
        IconButton(onClick = {
            navController.popBackStack()
        }) {
            Icon(imageVector = Icons.AutoMirrored.Default.ArrowBack,
                contentDescription = "", modifier = Modifier.size(32.dp))
        }
    }
}

The Spacer-line becomes marked as erroneous. The error-message shown is "Unresolved reference 'weight'."

What goes wrong here? Why is the Spacer-modifier missing the weight-property suddenly?

How can it fixed, without passing the modifier as an argument?

like image 242
cluster1 Avatar asked Oct 21 '25 21:10

cluster1


1 Answers

Why do you even want to use weight here? It is used to proportionally resize elements in relation to other elements in a Column or a Row. But the Spacer is not in one of those elements, and therefore there also are no other siblings that the size can be compared to.

Maybe you intended to move the Spacer one line down, so it is in the Column? Or maybe you just wanted to set its size? In that case simply use the various size Modifiers instead, as the documentation explains:

Component that represents an empty space layout, whose size can be defined using Modifier.width, Modifier.height and Modifier.size modifiers.

like image 109
Leviathan Avatar answered Oct 23 '25 11:10

Leviathan



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!