Alexander Maryanovsky
03/20/2023, 9:26 AM@Composable
private fun MyLayout(){
Box(Modifier.fillMaxSize().background(Color.Yellow)){
Box(modifier = Modifier
.background(Color.DarkGray)
.sizeIn(minWidth = 200.dp, minHeight = 200.dp)
) {
Text("BOX", modifier = Modifier.align(Alignment.Center))
}
}
}
Oleksandr Balan
03/20/2023, 9:29 AMBox
composable has propagateMinConstraints
argument, which is set to false
by default. Thus min constraints from the outer box are not automatically provided to the inner box.
Try setting propagateMinConstraints = true
for outer box.Laurynas Mykolaitis
03/20/2023, 9:31 AMModifier
.fillMaxSize()
Alexander Maryanovsky
03/20/2023, 10:05 AMStylianos Gakis
03/20/2023, 10:48 AMpropagateMinConstraints
is a big part about this I think. Once I realized Surface
turns this to true by default (not configurable) I realized why I was confused about why sometimes it’d automatically make the child match the parent size and why some other times (where I was not using surface
) it would not.
This, along with the ability to sometimes not listen to the parent constraints, and are able to break from them (for good reason) were the two things that have made me question my understanding the most.
But after you figure those out I think it gets easier.Alexander Maryanovsky
03/20/2023, 10:55 AMfillMaxSize()
does is set the minWidth
to maxWidth
and minHeight
to maxHeight
of the incoming Constraints.Stylianos Gakis
03/20/2023, 10:56 AMAlexander Maryanovsky
03/20/2023, 10:56 AMsizeIn(minWidth=X)
just sets the minWidth
of the incoming Constraints to X
Box(
modifier = Modifier.fillMaxSize().background(Color.Yellow),
propagateMinConstraints = true
){
Box(modifier = Modifier
.background(Color.DarkGray)
.sizeIn(minWidth = 200.dp, minHeight = 200.dp)
) {
Text("BOX", modifier = Modifier.align(Alignment.Center))
}
}
The incoming Constraints of the inner box are (minWidth=200.dp, minHeight=200.dp, maxWidth=activityWidth, maxHeight=activityHeight)
propagateMinConstraints = true
, the incoming constraints are
minWidth=activityWidth,
minHeight=activityHeight,
maxWidth=activityWidth,
maxHeight=activityHeight
because those are the constraints of the outer boxStylianos Gakis
03/20/2023, 4:42 PMpropagateMinConstraints
.
I was using the Card
composable, which uses a Surface
under the hood, so I was expecting the min constraints to be propagated. However, if you notice inside the card composable, it wraps the content in a Column (no idea why a column in particular), which actually meant that this column is the one which gets the min constraints to be as big as the card itself, however the content of the column does not get that! Meaning that as I was trying to get the card content to align at the center vertically, I couldn’t do that. I have to instead either make my own card composable which does not wrap its content with any other composable which does not propagate min constraints, ColumnScope
verticalArrangement
for its content.
What an interesting thing, I really do wonder why it was done this way for the Card implementation tbh. Maybe as a sane default when people put many things inside the card content 🤷♂️Zach Klippenstein (he/him) [MOD]
03/21/2023, 3:32 PMAlexander Maryanovsky
03/21/2023, 3:33 PMColumn2
😉Stylianos Gakis
03/21/2023, 3:38 PMfillMaxSize()
) in order to work around the issue with the inner content not being the same size as the card where I’ve literally just set as Size(x.dp)
. I can now remove these workarounds by using my custom card.Jan Skrasek
03/21/2023, 6:49 PMColumn and Row not propagating min constraints on their cross axis by default is probably one of my least favorite things about compose. I think I saw on here a couple years ago that the team regrets that decision as well but it’s too late to change it nowThe issue here is that there is no such argument to change this on Column or Row. I have implemented a custom simple Column for this two times already.
Zach Klippenstein (he/him) [MOD]
03/21/2023, 8:58 PMStylianos Gakis
03/21/2023, 9:11 PMZach Klippenstein (he/him) [MOD]
03/21/2023, 11:13 PMStylianos Gakis
03/21/2023, 11:14 PMAlexander Maryanovsky
03/22/2023, 10:51 AMWindow.pack()
in AWT terms