Aside from setting `IntrinsicSize.Max` on the `Row...
# compose
l
Aside from setting
IntrinsicSize.Max
on the
Row
height and using
fillMaxHeight
on the
Divider
within it, is there another way to make the
Divider
fill the maximum height of a
Row
that contains other Composable elements with dynamic heights such as
Text
? I can’t use
IntrinsicSize.Max
since I’m using
BoxWithConstraints
within the
Row
, which is not allowed in Compose(will get a RuntimeException).
z
That is one of the primary uses for intrinsics.
And this restriction is one of the reasons to avoid BoxWithConstraints as much as possible.
l
In my case, it’s a little bit complicated. But I do need to use
BoxWithConstraints
to get the max width then do some calculations… So I thought getting rid of
IntrinsicSize.Max
and to achieve same effect using another way.
z
Are you composing different things based on those calculations?
l
So I give this maxWidth manually to Coil
ImageRequest
, so it can load network images with proper dimensions. And why Coil could not figure out the size constraint itself is because I put the Coil Image as an
InlineContent
of a
Text
, so the constraints is fixed from the
PlaceHolder
not parent Composable. I am basically implementing a markdown display feature that an image might be inserted inside a text. The idea is once the image is loaded successfully, I can reset size of the
PlaceHolder
so it can display the image properly. 😅
z
Yea, I don't think you need BoxWithConstraints for that
l
Any other way I could get the maxWidth right away without using
BoxWithConstraints
? I actually thought of other solutions such as getting the width from the
Modifier.onSizeChange{}
callback. But I am afraid this would be after some frames and it might too late as I basically need to pass that
maxWidth
limitation immediately to the
ImageRequest
.
My current solution for getting rid of
IntrinsicSize.Max
is I am using
Modifier.onSizeChange{}
to get the maxHeight of `Divider`'s sibling Composables and then set the maxHeight to the
Divider
itself. It works 😅
z
Why do you need to pass those constraints in manually? Coil's docs say
AsyncImage
already does this itself, and there seem to be ways to get even more involved if you need (
ConstraintsSizeResolver
)
s
Could you try replacing
Row
with
ConstraintLayout
instead? Link the `Divider`'s top and bottom to the parent's top and bottom, then add
height = Dimension.fillToConstraints
should work.
👍 1
l
@Son Phan wow, I just tried and it does work. Nice! And I just realised I have not used
ConstraintLayout
for ages!🤣
😁 1
Why do you need to pass those constraints in manually?
In a normal layout I don't need to and Coil can figure it out by the constraint from parent. But in my case the image is wrapped in an
InlineContent
and then passed to the
Text
- so now the constraint becomes the
PlaceHolder
size of the
InlineContent
which is not ideal because the size of
PlaceHolder
is hardcoded initially and usually pretty small.
z
Couldn't you just pass a custom
SizeResolver
then?
l
Couldn't you just pass a custom
SizeResolver
then?
That's exact what I am doing now. My code to build the
ImageRequest
is like:
Copy code
val request = ImageRequest.Builder(context)
                    .data(inlineImage.url)
                    .size(Dimension(maxWidthInPx), Dimension.Undefined)
                    .build()
The
size(...)
in the chain creates a custom
SizeResolver
under the hood. But here I need the
maxWidthInPx
is which coming from
BoxWithConstraintsScope.constraints.maxWidth
...