androidx.compose.ui.modifier
Interfaces
ModifierLocalConsumer |
A Modifier that can be used to consume |
Cmn
|
ModifierLocalModifierNode |
A |
Cmn
|
ModifierLocalProvider |
A Modifier that can be used to provide |
Cmn
|
ModifierLocalReadScope |
This scope gives us access to modifier locals that are provided by other modifiers to the left of this modifier, or above this modifier in the layout tree. |
Cmn
|
Classes
ModifierLocal |
|
Cmn
|
ModifierLocalMap |
An opaque key-value holder of |
Cmn
|
ProvidableModifierLocal |
|
Cmn
|
Top-level functions summary
ModifierLocalMap |
Creates an empty |
Cmn
|
ModifierLocalMap |
<T : Any?> modifierLocalMapOf(entry: Pair<ModifierLocal<T>, T>)Creates a |
Cmn
|
ModifierLocalMap |
<T : Any?> modifierLocalMapOf(key: ModifierLocal<T>)Creates a |
Cmn
|
ModifierLocalMap |
modifierLocalMapOf(Creates a |
Cmn
|
ModifierLocalMap |
modifierLocalMapOf(Creates a |
Cmn
|
ProvidableModifierLocal<T> |
<T : Any?> modifierLocalOf(defaultFactory: () -> T)Creates a |
Cmn
|
Extension functions summary
Modifier |
Modifier.modifierLocalConsumer(consumer: ModifierLocalReadScope.() -> Unit)A Modifier that can be used to consume |
Cmn
|
Modifier |
<T : Any?> Modifier.modifierLocalProvider(A Modifier that can be used to provide |
Cmn
|
Top-level functions
modifierLocalMapOf
fun <T : Any?> modifierLocalMapOf(entry: Pair<ModifierLocal<T>, T>): ModifierLocalMap
Creates a ModifierLocalMap with a single key and value. The provided entry should have Pair::first be the ModifierLocal key, and the Pair::second be the corresponding value.
modifierLocalMapOf
fun <T : Any?> modifierLocalMapOf(key: ModifierLocal<T>): ModifierLocalMap
Creates a ModifierLocalMap with a single key and value initialized to null.
modifierLocalMapOf
fun modifierLocalMapOf(
entry1: Pair<ModifierLocal<*>, Any>,
entry2: Pair<ModifierLocal<*>, Any>,
vararg entries: Pair<ModifierLocal<*>, Any>
): ModifierLocalMap
Creates a ModifierLocalMap with multiple keys and values. The provided entries should have each item's Pair::first be the ModifierLocal key, and the Pair::second be the corresponding value.
modifierLocalMapOf
fun modifierLocalMapOf(
key1: ModifierLocal<*>,
key2: ModifierLocal<*>,
vararg keys: ModifierLocal<*>
): ModifierLocalMap
Creates a ModifierLocalMap with several keys, all initialized with values of null
modifierLocalOf
fun <T : Any?> modifierLocalOf(defaultFactory: () -> T): ProvidableModifierLocal<T>
Creates a ProvidableModifierLocal and specifies a default factory.
import androidx.compose.foundation.clickable import androidx.compose.foundation.layout.Box import androidx.compose.runtime.mutableStateOf import androidx.compose.runtime.remember import androidx.compose.ui.Modifier import androidx.compose.ui.composed import androidx.compose.ui.modifier.modifierLocalConsumer import androidx.compose.ui.modifier.modifierLocalOf import androidx.compose.ui.modifier.modifierLocalProvider // Define the type of data. val ModifierLocalMessage = modifierLocalOf { "Unknown" } Box( Modifier // Provide an instance associated with the data type. .modifierLocalProvider(ModifierLocalMessage) { "World" } .composed { var message by remember { mutableStateOf("") } Modifier // Use the data type to read the message. .modifierLocalConsumer { message = ModifierLocalMessage.current } .clickable { println("Hello $message") } } )
Sample 2: Modifier sending a message to another to its left.
import androidx.compose.foundation.clickable import androidx.compose.foundation.layout.Box import androidx.compose.runtime.mutableStateOf import androidx.compose.runtime.remember import androidx.compose.ui.Modifier import androidx.compose.ui.composed import androidx.compose.ui.modifier.modifierLocalConsumer import androidx.compose.ui.modifier.modifierLocalOf import androidx.compose.ui.modifier.modifierLocalProvider class Sender(val onMessageReceived: (String) -> Unit) { fun sendMessage(message: String) { onMessageReceived(message) } } // Define the type of data. val ModifierLocalSender = modifierLocalOf<Sender> { error("No sender provided by parent.") } Box( Modifier // Provide an instance associated with the sender type. .modifierLocalProvider(ModifierLocalSender) { Sender { println("Message Received: $it") } } .composed { var sender by remember { mutableStateOf<Sender?>(null) } Modifier // Use the sender type to fetch an instance. .modifierLocalConsumer { sender = ModifierLocalSender.current } // Use this instance to send a message to the parent. .clickable { sender?.sendMessage("Hello World") } } )
Here are examples where a modifier can communicate with another across layout nodes:
Sample 1: Modifier sending a message to a modifier on a child layout node.
import androidx.compose.foundation.clickable import androidx.compose.foundation.layout.Box import androidx.compose.runtime.mutableStateOf import androidx.compose.runtime.remember import androidx.compose.ui.Modifier import androidx.compose.ui.modifier.modifierLocalConsumer import androidx.compose.ui.modifier.modifierLocalOf import androidx.compose.ui.modifier.modifierLocalProvider // Define the type of data. val ModifierLocalMessage = modifierLocalOf { "Unknown" } Box( // Provide an instance associated with the data type. Modifier.modifierLocalProvider(ModifierLocalMessage) { "World" } ) { var message by remember { mutableStateOf("") } Box( Modifier // Use the data type to read the message. .modifierLocalConsumer { message = ModifierLocalMessage.current } .clickable { println("Hello $message") } ) }
Sample 2: Modifier sending a message to a modifier on a parent layout node.
import androidx.compose.foundation.clickable import androidx.compose.foundation.layout.Box import androidx.compose.runtime.mutableStateOf import androidx.compose.runtime.remember import androidx.compose.ui.Modifier import androidx.compose.ui.modifier.modifierLocalConsumer import androidx.compose.ui.modifier.modifierLocalOf import androidx.compose.ui.modifier.modifierLocalProvider class Sender(val onMessageReceived: (String) -> Unit) { fun sendMessage(message: String) { onMessageReceived(message) } } // Define the type of data. val ModifierLocalSender = modifierLocalOf<Sender> { error("No sender provided by parent.") } Box( Modifier // Provide an instance associated with the sender type. .modifierLocalProvider(ModifierLocalSender) { Sender { println("Message Received: $it") } } ) { var sender by remember { mutableStateOf<Sender?>(null) } Box( Modifier // Use the sender type to fetch an instance. .modifierLocalConsumer { sender = ModifierLocalSender.current } // Use this instance to send a message to the parent. .clickable { sender?.sendMessage("Hello World") } ) }
| Parameters | |
|---|---|
defaultFactory: () -> T |
a factory to create a default value in cases where a Here are examples where a modifier can communicate with another in the same modifier chain: Sample 1: Modifier sending a message to another to its right. |
Extension functions
modifierLocalConsumer
fun Modifier.modifierLocalConsumer(consumer: ModifierLocalReadScope.() -> Unit): Modifier
A Modifier that can be used to consume ModifierLocals that were provided by other modifiers to the left of this modifier, or above this modifier in the layout tree.
modifierLocalProvider
fun <T : Any?> Modifier.modifierLocalProvider(
key: ProvidableModifierLocal<T>,
value: () -> T
): Modifier
A Modifier that can be used to provide ModifierLocals that can be read by other modifiers to the right of this modifier, or modifiers that are children of the layout node that this modifier is attached to.