If I have items that are wider than the containing Column
, how can I achieve that all items are left aligned and clipped only at their right side?
Here is an example:
@Composable
fun RandomTriangle(modifier: Modifier = Modifier) {
val width = rand.nextInt(20, 500).dp
Box(modifier
.height(30.dp)
.requiredWidth(width)
.drawBehind {
val path = Path().apply {
moveTo(0f, 0f)
lineTo(size.width, size.height / 2)
lineTo(0f, size.height)
close()
}
drawPath(path, Brush.horizontalGradient(listOf(Color.Green, Color.Blue)))
})
}
@Preview(widthDp = 100)
@Composable
fun ListTestPreview() {
Column(Modifier.fillMaxWidth()) {
List(5) { RandomTriangle(Modifier.padding(4.dp)) }
}
}
This yields the following result:
As you can see, the second, fourth and fifth triangles (from top to bottom) are oversize. They are centered in the parent Column
and clipped left and right, but I would like them to be left-aligned and clipped on the right side only.
I already tried clipToBounds()
on the Column
, and .align(Alignment.Start)
on the contained item; both do not help.