KamilH
11/15/2024, 6:09 AMArrangement.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’tKamilH
11/15/2024, 6:11 AMcontent: List<(@Composable () -> Unit)?>
, filter null values, iterate through them, and have:
if (index != contentToRender.lastIndex) {
HorizontalDivider()
}
It works fine. However, it’s not the API I’m happy withStylianos Gakis
11/15/2024, 8:58 AMspacedBy
arrangement, I just always do
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
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 wantKamilH
11/15/2024, 9:07 AMColumn {
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 themStylianos Gakis
11/15/2024, 9:09 AMKamilH
11/15/2024, 11:39 AMLayout
to achieve that, and it seems to be possible, but still it would be greato to have some general API for this purposeStylianos Gakis
11/15/2024, 12:02 PM