zzz
zzz

Reputation: 93

Jetpack Compose can't import constrainAs

I can't import constrainAs in Jetpack Compose.

 Text("Text", Modifier.constrainAs(text) {
            top.linkTo(button.bottom, margin = 16.dp)
        })

I added constraintlayout dependency:

implementation "androidx.constraintlayout:constraintlayout-compose:1.0.0-alpha08"

But it still says Unresolved reference: constrainAs

Upvotes: 3

Views: 2758

Answers (3)

Mahdi nezam parast
Mahdi nezam parast

Reputation: 649

Better to use another version :

implementation "androidx.constraintlayout:constraintlayout-compose:1.0.0-beta02"

Upvotes: 0

Phil Dukhov
Phil Dukhov

Reputation: 87794

constrainAs belongs to ConstraintLayoutScope

it means that in can be used for any view inside ConstraintLayout, but only on a first level child.

ConstraintLayout {
    val ref = createRef()
    Box(
        // OK
        modifier = Modifier.constrainAs(ref) {

        }
    ) {
        Box(
            // Not OK
            modifier = Modifier.constrainAs(ref) {

            }
        )
    }
}

Upvotes: 8

Richard Onslow Roper
Richard Onslow Roper

Reputation: 6835

Are you adding the following import?

androidx.constraintlayout.compose.ConstraintLayout

You don't need to import constraintsAs separately.

Upvotes: 0

Related Questions