defaultMinSize

Compose Modifier

Common
fun Modifier.defaultMinSize(minWidth: Dp = Dp.Unspecified, minHeight: Dp = Dp.Unspecified) =
    this.then(UnspecifiedConstraintsElement(minWidth = minWidth, minHeight = minHeight))

Constrain the size of the wrapped layout only when it would be otherwise unconstrained: the minWidth and minHeight constraints are only applied when the incoming corresponding constraint is 0. The modifier can be used, for example, to define a default min size of a component, while still allowing it to be overidden with smaller min sizes across usages.

Code Examples

DefaultMinSizeSample

@Composable
fun DefaultMinSizeSample() {
    @Composable
    fun DefaultMinBox(modifier: Modifier = Modifier) {
        Box(modifier.defaultMinSize(minWidth = 100.dp, minHeight = 100.dp).background(Color.Blue))
    }
    // This will be a 100.dp x 100.dp blue box. Because we are not providing any min constraints
    // to the DefaultMinBox, defaultMinSize will apply its min constraints.
    DefaultMinBox()
    // This will be a 50.dp x 50.dp blue box. Because we are providing min constraints
    // to the DefaultMinBox, defaultMinSize will not apply its min constraints.
    DefaultMinBox(Modifier.requiredSizeIn(minWidth = 50.dp, minHeight = 50.dp))
    // Note that if DefaultMinBox used requiredSizeIn or sizeIn rather than
    // defaultMinSize, the min constraints would have been applied with either
    // of the above usages.
}