I have a list of percentage values that Id like to...
# compose
z
I have a list of percentage values that Id like to render in a
Row
. I use
Modifier.weight(percentage)
to acheive this and it works great, BUT - if the percentage is very small for one of the elements, its simply not rendered. I thought I could do
Modifier.requiredSize(1.dp)
but that seems to only render one of the small elements. Im guessing that this is due to the layout not having enough space. How can I get this working?
👀 1
I can technically do
Modifier.weight(fraction.coerceAtLeast(0.01f))
but that falls apart on larger screens.
k
What’s your definition of “working” in this case? What do you want to happen for those small elements? Enforce a certain minimum dp size?
z
Yes, a minimum size of 1 dp would be perfectly fine!
And of course, Ive tried minWidth, requiredWidth and practically all width related variants. Seems like the weight eventually just takes up the whole layout, which I dont think it should?
s
Weight also includes a second parameter regarding if it should take all the space it can see or not, could it be that this is what you need to set to false?
z
Unfortunately not @Stylianos Gakis! It results in nothing being drawn since the items themselves dont have any content that takes up space, i.e. if I use weight without fill for a Text then it would simply result in the Text taking up the least amount of space needed to draw itself (instead of the entire row); yet it doesnt expand beyond the available space if items come after it. Or at least this is my understanding of how it works 😄
s
Oh hmm right. Could it be an order of modifiers thing, so you can make it minWidth 1dp first, and then apply the weight. That's how I think the text example you said works too, it does take the least amount of space it needs would work no? 🤔
z
Thank god, it works, and this is what Id expect .. I just made a mistake in my initial approach that resulted in requiredWidth never being used - hence them not being rendered 🤦🏽‍♂️
return if (fraction > 0f) weight(fraction) else requiredWidth(1.dp)
Fraction for these endlessly small items were always >0f for me, hence .. the headache. Thanks for helping me discover this fist bump
s
Nice! And does it work regardless of which order these two modifiers come in?
z
Im only ever using one or the other for this case ^^ If I apply both then weight handles the sizing, Id think that you would be able to set a minimum size in conjunction with weight but that doesnt seem to be the case.
💪 1