Interface

CompositionLocalConsumerModifierNode

Implementing this interface allows your Modifier.Node subclass to read CompositionLocals via the currentValueOf function.

CompositionLocalConsumingModifierSample

@Sampled
@Composable
fun CompositionLocalConsumingModifierSample() {
    val localBackgroundColor = compositionLocalOf { Color.White }
    class BackgroundColor :
        Modifier.Node(), DrawModifierNode, CompositionLocalConsumerModifierNode {
        override fun ContentDrawScope.draw() {
            val backgroundColor = currentValueOf(localBackgroundColor)
            drawRect(backgroundColor)
            drawContent()
        }
    }
    val backgroundColorElement =
        object : ModifierNodeElement<BackgroundColor>() {
            override fun create() = BackgroundColor()

            override fun update(node: BackgroundColor) {}

            override fun hashCode() = System.identityHashCode(this)

            override fun equals(other: Any?) = (other === this)

            override fun InspectorInfo.inspectableProperties() {
                name = "backgroundColor"
            }
        }
Jetpack Compose beyond MaterialFree components, screens and templates for apps that don’t look like Google’s. Copy them into your app, or let your coding agent add them.Browse for free

Content updated: