https://kotlinlang.org logo
#compose
Title
# compose
a

alxdthn

11/10/2023, 2:06 PM
Seems
Row
with
weight
inside
LazyRow
works unexpected. The
Text
lacks available horizontal space.
Copy code
@Preview
@Composable
private fun Test() {
  LazyRow(modifier = Modifier.fillMaxWidth()) {
    item {
      Column {
        Row(modifier = Modifier.fillMaxWidth()) {
          Text(
            text = "Text",
            modifier = Modifier.weight(1f),
          )
          // some content...
        }
        // some content...
      }
    }
  }
}
1
c

czuckie

11/10/2023, 2:31 PM
How would you expect it to work? And what if you had multiple items?
a

alxdthn

11/10/2023, 2:33 PM
I expect this result
c

czuckie

11/10/2023, 2:35 PM
and if there were multiple items in your row?
a

alxdthn

11/10/2023, 2:36 PM
image.png
c

czuckie

11/10/2023, 2:38 PM
if you apply a weight to something in a column or a row, it'll attempt to occupy all the available space it can (sharing if other items have weight associated with them). However, in a LazyRow, or LazyColumn, there is no defined width (or height respectively) meaning such a calculation would be impossible.
you'll need to constrain the width of the Row you're setting the weight of a child in in some way
a

alxdthn

11/10/2023, 2:44 PM
It is work right now. But in my expectations if i constrain by
fillMaxWidth
LazyRow and all child, i do the same. Thanks!
c

czuckie

11/10/2023, 2:46 PM
Nice! You could use
BoxWithConstraints
and use the scope inside the content lambda to obtain the
maxWidth
property which will reflect the size of the
BoxWithConstraints
composable (which could be different from the screen width). I'm not sure what your UI is going to eventually look like but you may not need a LazyRow (or the screen width stuff) if you're just creating the layout as it is above. Either way, keep going and good luck!
👍 1
z

Zach Klippenstein (he/him) [MOD]

11/10/2023, 3:32 PM
Does fillMaxParentWidth work for you?
2
a

alxdthn

11/10/2023, 3:39 PM
Does fillMaxParentWidth work for you?
Yes, this is exact what i need!
z

Zach Klippenstein (he/him) [MOD]

11/10/2023, 3:49 PM
That will be MUCH better than BoxWithConstraints
c

czuckie

11/10/2023, 4:15 PM
That will be MUCH better than BoxWithConstraints
Oooo, noted, thanks Zach!
b

brandonmcansh

11/10/2023, 4:43 PM
Was coming here to suggest the same thing @Zach Klippenstein (he/him) [MOD] 🤙