Last active
November 4, 2022 04:42
-
-
Save parthdesai1208/f1974d4459bf8e7f022eb68773759528 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//implement custom column | |
@Composable | |
fun MyOwnColumn( | |
modifier: Modifier = Modifier, | |
content: @Composable () -> Unit | |
) { | |
Layout( | |
modifier = modifier, | |
content = content | |
) { measurables, constraints -> | |
// Don't constrain child views further, measure them with given constraints | |
// List of measured children | |
val placeables = measurables.map { measurable -> | |
// Measure each child | |
measurable.measure(constraints) | |
} | |
// Track the y co-ord we have placed children up to | |
var yPosition = 0 | |
// Set the size of the layout as big as it can | |
layout(constraints.maxWidth, constraints.maxHeight) { | |
// Place children in the parent layout | |
placeables.forEach { placeable -> | |
// Position item on the screen | |
placeable.placeRelative(x = 0, y = yPosition) | |
// Record the y co-ord placed up to | |
yPosition += placeable.height | |
} | |
} | |
} | |
} | |
use it like, | |
@Composable | |
fun BodyContent(modifier: Modifier = Modifier) { | |
MyOwnColumn(modifier.padding(8.dp)) { | |
Text("MyOwnColumn") | |
Text("places items") | |
Text("vertically.") | |
Text("We've done it by hand!") | |
} | |
} |
Author
parthdesai1208
commented
Mar 8, 2022
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment