LayoutModifierNode
interface LayoutModifierNode : DelegatableNode
A Modifier.Node
that changes how its wrapped content is measured and laid out. It has the same
measurement and layout functionality as the androidx.compose.ui.layout.Layout
component, while
wrapping exactly one layout due to it being a modifier. In contrast, the
androidx.compose.ui.layout.Layout
component is used to define the layout behavior of multiple
children.
This is the androidx.compose.ui.Modifier.Node
equivalent of
androidx.compose.ui.layout.LayoutModifier
Functions
fun MeasureScope.measure(measurable: Measurable, constraints: Constraints): MeasureResult
The function used to measure the modifier. The measurable
corresponds to the wrapped
content, and it can be measured with the desired constraints according to the logic of the
LayoutModifierNode
. The modifier needs to choose its own size, which can depend on the size
chosen by the wrapped content (the obtained Placeable
), if the wrapped content was
measured. The size needs to be returned as part of a MeasureResult
, alongside the placement
logic of the Placeable
, which defines how the wrapped content should be positioned inside
the LayoutModifierNode
. A convenient way to create the MeasureResult
is to use the
MeasureScope.layout
factory function.
A LayoutModifierNode
uses the same measurement and layout concepts and principles as a
androidx.compose.ui.layout.Layout
, the only difference is that they apply to exactly one
child. For a more detailed explanation of measurement and layout, see
androidx.compose.ui.layout.MeasurePolicy
.
fun IntrinsicMeasureScope.minIntrinsicWidth(measurable: IntrinsicMeasurable, height: Int): Int
The function used to calculate IntrinsicMeasurable.minIntrinsicWidth
.
fun IntrinsicMeasureScope.minIntrinsicHeight(measurable: IntrinsicMeasurable, width: Int): Int
The lambda used to calculate IntrinsicMeasurable.minIntrinsicHeight
.
fun IntrinsicMeasureScope.maxIntrinsicWidth(measurable: IntrinsicMeasurable, height: Int): Int
The function used to calculate IntrinsicMeasurable.maxIntrinsicWidth
.
fun IntrinsicMeasureScope.maxIntrinsicHeight(measurable: IntrinsicMeasurable, width: Int): Int
The lambda used to calculate IntrinsicMeasurable.maxIntrinsicHeight
.
Code Examples
LayoutModifierNodeSample
@Composable
fun LayoutModifierNodeSample() {
class VerticalPadding(var padding: Dp) : LayoutModifierNode, Modifier.Node() {
override fun MeasureScope.measure(
measurable: Measurable,
constraints: Constraints,
): MeasureResult {
val paddingPx = padding.roundToPx()
val placeable = measurable.measure(constraints.offset(vertical = -paddingPx))
return layout(placeable.width, placeable.height + paddingPx) {
placeable.placeRelative(0, paddingPx)
}
}
}
data class VerticalPaddingElement(val padding: Dp) : ModifierNodeElement<VerticalPadding>() {
override fun create() = VerticalPadding(padding)
override fun update(node: VerticalPadding) {
node.padding = padding
}
override fun InspectorInfo.inspectableProperties() {
name = "verticalPadding"
properties["padding"] = padding
}
}
fun Modifier.verticalPadding(padding: Dp) = this then VerticalPaddingElement(padding)
Box(Modifier.background(Color.Gray).verticalPadding(50.dp)) {
Box(Modifier.fillMaxSize().background(Color.DarkGray))
}
}