ProvidableCompositionLocal



A ProvidableCompositionLocal can be used in CompositionLocalProvider to provide values.

Summary

Public functions

infix ProvidedValue<T>
provides(value: T)

Associates a CompositionLocal key to a value in a call to CompositionLocalProvider.

Cmn
infix ProvidedValue<T>

Associates a CompositionLocal key to a lambda, compute, in a call to CompositionLocal.

Cmn
infix ProvidedValue<T>
providesDefault(value: T)

Associates a CompositionLocal key to a value in a call to CompositionLocalProvider if the key does not already have an associated value.

Cmn

Inherited properties

From androidx.compose.runtime.CompositionLocal
T

Return the value provided by the nearest CompositionLocalProvider component that invokes, directly or indirectly, the composable function that uses this property.

Cmn

Public functions

provides

infix fun provides(value: T): ProvidedValue<T>

Associates a CompositionLocal key to a value in a call to CompositionLocalProvider.

providesComputed

infix fun providesComputed(compute: CompositionLocalAccessorScope.() -> T): ProvidedValue<T>

Associates a CompositionLocal key to a lambda, compute, in a call to CompositionLocal. The compute lambda is invoked whenever the key is retrieved. The lambda is executed in the context of a CompositionLocalContext which allow retrieving the current values of other composition locals by calling CompositionLocalAccessorScope.currentValue, which is an extension function provided by the context for a CompositionLocal key.

import androidx.compose.runtime.Composable
import androidx.compose.runtime.CompositionLocalProvider
import androidx.compose.runtime.compositionLocalOf

val LocalValue = compositionLocalOf { 10 }
val LocalLargerValue = compositionLocalOf { 12 }

@Composable
fun App() {
    CompositionLocalProvider(
        LocalLargerValue providesComputed {
            LocalValue.currentValue + 10
        }
    ) {
        SomeScreen()
    }
}

providesDefault

infix fun providesDefault(value: T): ProvidedValue<T>

Associates a CompositionLocal key to a value in a call to CompositionLocalProvider if the key does not already have an associated value.