Build apps faster with our new App builder! Check it out →

onConsumedWindowInsetsChanged

Common

Modifier in Compose Foundation Layout

Calls [block] with the [WindowInsets] that have been consumed, either by [consumeWindowInsets] or one of the padding Modifiers, such as [imePadding].

Last updated:

Installation

dependencies {
   implementation("androidx.compose.foundation:foundation-layout:1.8.0-alpha04")
}

Overloads

@Stable
fun Modifier.onConsumedWindowInsetsChanged(block: (consumedWindowInsets: WindowInsets) -> Unit)

Code Example

withConsumedInsetsSample

@OptIn(ExperimentalLayoutApi::class)
fun withConsumedInsetsSample() {
    class SampleActivity : ComponentActivity() {
        override fun onCreate(savedInstanceState: Bundle?) {
            WindowCompat.setDecorFitsSystemWindows(window, false)
            super.onCreate(savedInstanceState)
            setContent {
                val remainingInsets = remember { MutableWindowInsets() }
                val safeContent = WindowInsets.safeContent
                Box(
                    Modifier.navigationBarsPadding().onConsumedWindowInsetsChanged {
                        consumedWindowInsets ->
                        remainingInsets.insets = safeContent.exclude(consumedWindowInsets)
                    }
                ) {
                    // padding can be used without recomposition when insets change.
                    val padding = remainingInsets.asPaddingValues()
                    Box(Modifier.padding(padding))
                }
            }
        }
    }
}
by @alexstyl