Colton Idle
03/21/2021, 5:18 PMColton Idle
03/21/2021, 5:19 PM@Composable
fun OffsetBackgroundContainer(
content: @Composable BoxScope.() -> Unit
){
Box {
Box(Modifier.background(Color.Green).height(100.dp))
content()
}
}
It gets me that offset effect that works sometimes, but I'm not sure how I would get the content height and set it to the modifier to replace the 100.dpSe7eN
03/21/2021, 5:22 PMBox
and use Modifier.onSizeChanged
on that box to get the heightAdam Powell
03/21/2021, 5:48 PM@Preview
@Composable
fun Boxy() {
Box {
Box(
Modifier
.matchParentSize()
.wrapContentHeight(<http://Alignment.Top|Alignment.Top>)
.fillMaxHeight(0.5f)
.background(Color.Green)
)
Card(Modifier.padding(horizontal = 16.dp).height(150.dp)) {
Text("Hello, world", Modifier.padding(16.dp))
}
}
}
Adam Powell
03/21/2021, 5:50 PM.matchParentSize()
modifier available in BoxScope
. It makes the green part measure last based on the size of the rest of the box's content. From there it's just a matter of sizing it to half the max height and aligning it within the space.Colton Idle
03/21/2021, 8:15 PMBox() {
var sizeOfContent by remember { mutableStateOf(IntSize(0, 0)) }
Box(
Modifier.background(Color.Blue)
.height(with(LocalDensity.current) { (sizeOfContent.height).toDp().times(0.50F) })
.fillMaxWidth()
)
Column(modifier.onSizeChanged { sizeOfContent = it }) {
and it actually works pretty well.
I will try the solution from Adam Powell now though.Adam Powell
03/21/2021, 8:19 PMColton Idle
03/21/2021, 8:25 PMAdam Powell
03/21/2021, 8:34 PMColton Idle
03/21/2021, 9:58 PM