Yingding Wang
10/19/2022, 8:57 PMYingding Wang
10/19/2022, 9:04 PMModifier.width(IntrinsicSize.Max / 2)
, unfortunately I can not perform arithmetic operation on IntrinsicSize.Max
Jiri Bruchanov
10/19/2022, 9:14 PM@Preview
@Composable
fun TestPreview() {
Box(modifier = Modifier
.fillMaxSize()
.background(Color.Black)) {
Box(
modifier = Modifier
.background(Color.Red)
.fillMaxWidth(0.25f)
.fillMaxHeight(0.25f)
)
}
}
Jiri Bruchanov
10/19/2022, 9:14 PMYingding Wang
10/19/2022, 9:59 PMPablichjenkov
10/20/2022, 4:24 AMJiri Bruchanov
10/20/2022, 6:36 AMYingding Wang
10/20/2022, 10:16 AMPablichjenkov
10/20/2022, 6:47 PMBoxWithConstraints(
modifier = modifier
.fillMaxSize()
.onGloballyPositioned { coordinates ->
boxWithConstraintsHeight = (coordinates.size.height.toFloat())
}
) { // boxWithConstraintsHeightMax could have been obtained from boxConstraintScope but the
// value is pre-layout so we are not sure whether the Parent will fill the whole size or nor.
val boxConstraintScope = this
Column(
modifier = Modifier
.fillMaxSize()
.verticalScroll(rememberScrollState())
.background(Color.Black),
//verticalArrangement = Arrangement.spacedBy(20.dp)
) {
val spacerHeight = boxWithConstraintsHeight.times(0.25)
val spacerHeightDp = with(density) { spacerHeight.toInt().toDp() }
Log.d("Pablo", "spacerHeightDp = ${spacerHeightDp}")
val redBoxHeight = boxWithConstraintsHeight.times(2)
val redBoxHeightDp = with(density) { redBoxHeight.toInt().toDp() }
Log.d("Pablo", "redBoxHeightDp = ${redBoxHeightDp}")
Spacer(
modifier = Modifier
.fillMaxWidth(0.75F)
.height(spacerHeightDp)
.background(Color.Green)
)
Box(
modifier = Modifier
.background(Color.Red)
.fillMaxWidth(0.5F)
.height(redBoxHeightDp)
.absoluteOffset(y = 100.dp)
.onGloballyPositioned { coordinates ->
boxHeight = (coordinates.size.height.toFloat())
}
)
}
/* This work with "Row and Column" but the fraction is related to the size that is left to
fill not the parent size
Spacer(
modifier = Modifier.fillMaxWidth().fillMaxHeight(0.5F)
)
Box(
modifier = Modifier
.background(Color.Red)
.fillMaxWidth(0.5F)
.fillMaxHeight(0.5F)
)*/
}
What I do is wrap a Column/Row within a Box set to fillMaxSize. Then capture the Box layout parameters in onGloballyPositioned, which is invoked after the layout phase finishes. I have a reactive state within the composable hierarchy that reacts to the Box change. Then the children of the Box react to this change.Pablichjenkov
10/20/2022, 6:50 PMPablichjenkov
10/20/2022, 6:52 PMPablichjenkov
10/20/2022, 7:41 PM