ModifierLocalKt

public final class ModifierLocalKt


Summary

Public methods

static final @NonNull ProvidableModifierLocal<@NonNull T>
<T extends Object> modifierLocalOf(@NonNull Function0<@NonNull T> defaultFactory)

Creates a ProvidableModifierLocal and specifies a default factory.

Public methods

modifierLocalOf

@NonNull
public static final ProvidableModifierLocal<@NonNull T> <T extends Object> modifierLocalOf(@NonNull Function0<@NonNull T> defaultFactory)

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.composed
import androidx.compose.ui.modifier.modifierLocalConsumer
import androidx.compose.ui.modifier.modifierLocalProvider
import androidx.compose.ui.modifier.modifierLocalOf

// 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.composed
import androidx.compose.ui.modifier.modifierLocalConsumer
import androidx.compose.ui.modifier.modifierLocalProvider
import androidx.compose.ui.modifier.modifierLocalOf

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.modifierLocalConsumer
import androidx.compose.ui.modifier.modifierLocalProvider
import androidx.compose.ui.modifier.modifierLocalOf

// 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.modifierLocalConsumer
import androidx.compose.ui.modifier.modifierLocalProvider
import androidx.compose.ui.modifier.modifierLocalOf

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
@NonNull Function0<@NonNull T> defaultFactory

a factory to create a default value in cases where a ModifierLocal is consumed without a Provider. If this is a situation you would rather not handle, you can throw an error in this factory.

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.