Is there an easy way to put a divider between `Col...
# compose
k
Is there an easy way to put a divider between `Column`/`Row` elements? Similar to
Arrangement.spacedBy()
, which creates an empty space between elements, would that allow for using other
@Composable
between elements? I have a
Column
whose each child may emit 1 or 0 UI elements, and I want to render a divider between each of them while after the last one, I don’t
At the moment, instead of putting children directly into the column, I put them into a list of
content: List<(@Composable () -> Unit)?>
, filter null values, iterate through them, and have:
Copy code
if (index != contentToRender.lastIndex) {
    HorizontalDivider()
}
It works fine. However, it’s not the API I’m happy with
s
Yup, if you do not have any
spacedBy
arrangement, I just always do
Copy code
Column {
 items.forEachIndexed { index, item ->
  if (index != 0) {
   HorizontalDivider()
  }
  Item(item)
 }
}
If you got a
spacedBy
arrangement on your column however, or if you do not want that extra 1.dp space the divider takes, you can also do
Copy code
val dividerColor = YourTheme.colorScheme.dividerColor
Column(verticalArrangement = Arrangement.spacedBy(4.dp)) {
 items.forEachIndexed { index, item ->
  Item(
   item,
   Modifier.drawWithContent {
    drawContent()
    if (index != 0) {
     drawLine(dividerColor, Offset.Zero, Offset(size.width, 0f), 1.dp.toPx())
    }
   }
  )
 }
}
Because otherwise your divider will count as an item and it will add the 4.dp spacing around the divider which you don't want
k
It definitely makes sense when you have a list of items you want to render, but the problem is when you want to define “optional” composables upfront more or less like this:
Copy code
Column {
        if (text1 != null) {
            Text(text = text1)
        }
        if (text2 != null) {
            Text(text = text2)
        }
        (...)
    }
text1
and
text2
come from an external source, and you don’t know if they are available or null. Of course, in this case, the best would be to group them into a list, filter null, iterate through them, etc. But in the actual case I have different types of composables inside the column, so it is not that easy to group them
s
Yup I get it, I agree with you that this can get tricky. I've had to write extra code around places like this where I got many optional items in some list. I am not aware of any better API existing around this unfortunately.
k
Thank you for the confirmation. I was playing around with some custom
Layout
to achieve that, and it seems to be possible, but still it would be greato to have some general API for this purpose
s
Yeah it's definitely possible with a custom layout, but you'd lose the convenience of just being in a Column and getting all of the things it does for you for free. I have not found a way to combine the two, still be in a normal Column but provide this custom functionality. There might be some room to make a custom layout and just delegate to the column measuring etc but then just add the border drawing yourself if those are public API, but I never looked into it.