How can I set the position of composable function ...
# compose
c
How can I set the position of composable function with percentage or ratio? like a bias in ConstraintLayout?
z
I am not confident but try to use Modifier.graphicLayer lamda and pass translationX and translationY value
c
I found contraintlayout for compose. But I don't know where is bias..
d
In the ConstrainScope class there are some methods that include a bias parameter. i.e. centerHorizontallyTo(...)
l
Copy code
Box(
    modifier = Modifier
        .fillMaxSize()
        .wrapContentSize(BiasAlignment(/* percentage */))
) {
    // Your composable
}
Setting position with percentage can be done in two ways: 1. If the parent size is already known (like
BoxWithConstraints
), simply pass
size * percentage
to `offset`; 2. Implement a custom
Layout
, and a
ParentDataModifier
which takes the percentage, then retrieve this parent data in the
MeasurePolicy
of this special
Layout
and calculate the actual position. You can see how
BoxScope.matchParentSize
works for method 2.
z
Don’t use BoxWithConstraints for this. It’s way overkill, very inefficient
a
use
linkTo(start = …, end = …, bias = 0.5f)
or one using
top
and
bottom