CompositionLocalConsumerModifierNode
Interface
Common
interface CompositionLocalConsumerModifierNode : DelegatableNode
Implementing this interface allows your Modifier.Node
subclass to read
CompositionLocals
via the currentValueOf
function. The values of each
CompositionLocal will be resolved based on the context of the layout node that the modifier is
attached to, meaning that the modifier will see the same values of each CompositionLocal as its
corresponding layout node.
Code Examples
CompositionLocalConsumingModifierSample
@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"
}
}
fun Modifier.backgroundColor() = this then backgroundColorElement
Box(Modifier.backgroundColor()) { Text("Hello, world!") }
}