Hello. I've encountered an unexpected behavior in ...
# compose
s
Hello. I've encountered an unexpected behavior in my Compose layout which is critical to my project. I'm using Jetpack Compose BOM 2024.02.00. More details in the thread 🧵
This is simplified version of my project's layout. It consists of a parent box with a cyan background and fixed dimensions. Within this parent box, there is a child box (gray) with a variable width and an offset (labeled 'Offset X' in the screenshots). The width and height of the child box can be adjusted, and for simplicity, the reproducer modifies only the width and x offset. Issue: As long as the child box's width is less than or equal to the parent box's width, it grows horizontally from left to right as anticipated, with transformOrigin set at (0,0). However, when the child box's width exceeds the parent box's width, it begins expanding in both directions—ignoring the specified offset and acting as if the transformOrigin has changed to the center, despite the transformOrigin being explicitly set to (0,0). This unexpected behavior causes a significant issue for my project, as the requirement is for the child box to maintain its offset and to grow only from left to right. Efforts to manually control this behavior using Modifier.layout have not yielded the desired result; the child box grows symmetrically from the center once it exceeds the parent's width, which disrupts the layout alignment and breaks the UI logic. Is there a way to ensure that the child box maintains its offset and does not start growing from the center when its width becomes larger than the parent box's width?
e
what does your modifiers look like?
s
Which one
e
if you're using
wrapContentSize
to unbound the child, it takes an
alignment
parameter
s
I attached a gist with simplest possible code.
I'm not using
wrapContentSize
the layout size depends on a computed value.
Here is the actual issue im my project. The gray box represents the layout with variable size and offset. If I zoom in enough, you can see it misaligns once it exceeds the available screen width. To prove the rectangle coordinates are correct, I'm drawing a green rectangle using the canvas in the same layout.
Okay, I was able to workaround this issue using this hack:
But the main question remains open. 🤔
e
because that's the default when something doesn't fit, you can override with
.wrapContentSize(align = Alignment.CenterStart, unbounded = true)
s
do you mean to apply it for parent layout?
e
no, on the child that's too large for the parent
s
will it work with .requiredSize?
e
it goes outside of other size requirements, and if you use unbounded, you don't need requiredSize (it could be just size). but it should work
s
let me try
hah, it did the trick
image.png
But I mean, it does not feel intuitive to me
Nevertheless, it works now. Thank you for the help.
z
Another approach, which would work for this simple case but maybe not as practical for your actual use case, would be making the cyan box a custom layout that measures and positions the gray child directly.
s
Yeah, it's not very practical within my project because the layout is a bit more complicated than the reproducer. But thanks. Also, as you can see, I've successfully aligned the EditText with the graphics. However, I will wait for the moment when I can do the same using BTF2. Cheers 😄
👍🏻 1