Marco Pierucci
11/03/2022, 4:48 PMMarco Pierucci
11/03/2022, 4:48 PM@Composable
fun <T> Table(
modifier: Modifier = Modifier,
horizontalScrollState: ScrollState = rememberScrollState(),
columnCount: Int,
data: List<T>,
headerCellContent: @Composable BoxScope.(columnIndex: Int) -> Unit,
cellContent: @Composable BoxScope.(columnIndex: Int, item: T) -> Unit
) {
val columnWidths = remember { mutableStateMapOf<Int, Int>() }
var columnHeight by remember { mutableStateOf(0) }
Column(modifier = modifier.then(Modifier.horizontalScroll(horizontalScrollState))) {
repeat(data.size + 1) { rowIndex ->
Row {
(0 until columnCount).forEach { columnIndex ->
Box(
modifier = Modifier
.border(0.dp, MaterialTheme.colors.onSurface)
.padding(Dimensions.Space.Large)
.layout { measurable, constraints ->
val placeable = measurable.measure(constraints)
val existingWidth = columnWidths[columnIndex] ?: 0
val maxWidth = maxOf(existingWidth, placeable.width)
val maxHeight = maxOf(columnHeight, placeable.height)
if (maxWidth > existingWidth) {
columnWidths[columnIndex] = maxWidth
}
if (maxHeight > columnHeight) {
columnHeight = maxHeight
}
val height = if (rowIndex == 0) placeable.height else maxHeight
layout(width = maxWidth, height = height) {
placeable.placeRelative(0, 0)
}
}
) {
if (rowIndex == 0) {
headerCellContent(columnIndex)
} else {
cellContent(columnIndex, data[rowIndex - 1])
}
}
}
}
}
}
}
Marco Pierucci
11/03/2022, 4:49 PMArjun Achatz
11/03/2022, 5:07 PMArjun Achatz
11/03/2022, 5:08 PMArjun Achatz
11/03/2022, 5:08 PMMarco Pierucci
11/03/2022, 5:10 PMZach Klippenstein (he/him) [MOD]
11/03/2022, 5:35 PMMarco Pierucci
11/03/2022, 5:44 PMZach Klippenstein (he/him) [MOD]
11/03/2022, 5:45 PMMarco Pierucci
11/03/2022, 5:47 PMMarco Pierucci
11/03/2022, 10:18 PM@Composable
fun <T> Table(
modifier: Modifier = Modifier,
horizontalScrollState: ScrollState = rememberScrollState(),
columnCount: Int,
data: List<T>,
headerCellContent: @Composable BoxScope.(columnIndex: Int) -> Unit,
cellContent: @Composable BoxScope.(columnIndex: Int, item: T) -> Unit
) {
Row(modifier = Modifier.horizontalScroll(horizontalScrollState)) {
(0 until columnCount).forEach { columnIndex ->
Column(modifier = Modifier.width(IntrinsicSize.Max)) {
(0..data.size).forEach { index ->
Box(
modifier = Modifier
.fillMaxSize()
.border(0.dp, MaterialTheme.colors.onSurface)
) {
if (index == 0) {
headerCellContent(columnIndex)
} else {
cellContent(columnIndex, data[index - 1])
}
}
}
}
}
}
}
Now all my cells have the same width which is great. I dont think there is a way of querying the max height of the cells to apply it to be bo, with this current impl right? I'd need some sort of custom layoutArjun Achatz
11/03/2022, 10:21 PMMarco Pierucci
11/03/2022, 10:24 PM