🌈 Create new beautiful Compose Gradients with our new wesite ->
Function

compositionLocalWithComputedDefaultOf

Create a CompositionLocal that behaves like it was provided using ProvidableCompositionLocal.providesComputed by default.

compositionLocalComputedByDefault

@Sampled
fun compositionLocalComputedByDefault() {
    val LocalBaseValue = compositionLocalOf { 10 }
    val LocalLargerValue = compositionLocalWithComputedDefaultOf {
        LocalBaseValue.currentValue + 10
    }
}

compositionLocalComputedAfterProvidingLocal

@Sampled
fun compositionLocalComputedAfterProvidingLocal() {
    val LocalValue = compositionLocalOf { 10 }
    val LocalLargerValue = compositionLocalOf { 12 }
    val LocalComputedValue = compositionLocalWithComputedDefaultOf { LocalValue.currentValue + 4 }

    // In this example `LocalLargerValue` needs to be re-provided
    // whenever `LocalValue` is provided to keep its value larger
    // then `LocalValue`. However, `LocalComputedValue` does not
    // need to be re-provided to stay larger than `LocalValue` as
    // it is calculated based on the currently provided value for
    // `LocalValue`. Whenever `LocalValue` is provided the value
    // of `LocalComputedValue` is computed based on the currently
    // provided value for `LocalValue`.

Content updated: