Vinay Gaba
12/20/2020, 2:11 AMModifier
// Not allowed
fun TestComponent(
modifier: Modifier = Modifier.fillMaxWidth().preferredSizeIn(maxHeight = 200.dp)
)
// Instead, it recommends that you do this
fun TestComponent(
modifier: Modifier = Modifier
)
Could someone shed some more light on why this is a guideline? Since this is intentionally enforced, I’m sure there is a very good reason to do this. It’s obviously more reusable which makes complete sense but I wonder if this is one place where it would be okay to have some flexibility.Adam Powell
12/20/2020, 2:24 AMTestComponent(Modifier.background(Color.Blue))
as opposed to
TestComponent()
?Vinay Gaba
12/20/2020, 2:26 AMAdam Powell
12/20/2020, 2:26 AMpreferred
size modifiers chained off of the modifier you receive as a parameter, e.g.
@Composable fun TestComponent(
modifier: Modifier = Modifier
) = Row(
modifier.fillMaxWidth()
.preferredSizeIn(maxHeight = 200.dp)
) {
// contents
}
Vinay Gaba
12/20/2020, 2:38 AMif the incoming constraints don't permit sizing to the preferred sizes, the incoming constraints will win
- This is assuming the incoming constraints are more restrictive than maxHeight = 200.dp
Adam Powell
12/20/2020, 2:39 AMheight(250.dp)
is also more restrictive; the incoming min
can cause a preferred max
downstream to be overriddenVinay Gaba
12/20/2020, 2:40 AM