---
title: "recalculateWindowInsets"
description: "This recalculates the [WindowInsets] based on the size and position. This only works when
[Constraints] have [fixed width][Constraints.hasFixedWidth] and
[fixed height][Constraints.hasFixedHeight]. This can be accomplished, for example, by having
[Modifier.size], or [Modifier.fillMaxSize], or other size modifier before
[recalculateWindowInsets]. If the [Constraints] sizes aren't fixed, [recalculateWindowInsets]
won't adjust the [WindowInsets] and won't have any affect on layout.

[recalculateWindowInsets] is useful when the parent does not call [consumeWindowInsets] when it
aligns a child. For example, a [Column] with two children should have different [WindowInsets]
for each child. The top item should exclude insets below its bottom and the bottom item should
exclude the top insets, but the Column can't assign different insets for different children.


Another use is when a parent doesn't properly [consumeWindowInsets] for all space that it
consumes. For example, a 3rd-party container has padding that doesn't properly use
[consumeWindowInsets].


In most cases you should not need to use this API, and the parent should instead use
[consumeWindowInsets] to provide the correct values"
type: "modifier"
---

<div class='type'>Compose Modifier</div>

<a id='references'></a>
<div class='sourceset sourceset-common'>Common</div>


```kotlin
fun Modifier.recalculateWindowInsets(): Modifier
```


This recalculates the `WindowInsets` based on the size and position. This only works when
`Constraints` have `fixed width` and
`fixed height`. This can be accomplished, for example, by having
`Modifier.size`, or `Modifier.fillMaxSize`, or other size modifier before
`recalculateWindowInsets`. If the `Constraints` sizes aren't fixed, `recalculateWindowInsets`
won't adjust the `WindowInsets` and won't have any affect on layout.

`recalculateWindowInsets` is useful when the parent does not call `consumeWindowInsets` when it
aligns a child. For example, a `Column` with two children should have different `WindowInsets`
for each child. The top item should exclude insets below its bottom and the bottom item should
exclude the top insets, but the Column can't assign different insets for different children.


Another use is when a parent doesn't properly `consumeWindowInsets` for all space that it
consumes. For example, a 3rd-party container has padding that doesn't properly use
`consumeWindowInsets`.


In most cases you should not need to use this API, and the parent should instead use
`consumeWindowInsets` to provide the correct values



## Code Examples
### consumeWindowInsetsWithPaddingSample
```kotlin
@Composable
fun consumeWindowInsetsWithPaddingSample() {
    // The outer Box uses padding and properly compensates for it by using consumeWindowInsets()
    Box(
        Modifier.fillMaxSize()
            .padding(10.dp)
            .consumeWindowInsets(WindowInsets(10.dp, 10.dp, 10.dp, 10.dp))
    ) {
        Box(Modifier.fillMaxSize().safeContentPadding().background(Color.Blue))
    }
}
```
### recalculateWindowInsetsSample
```kotlin
@Composable
fun recalculateWindowInsetsSample() {
    var hasFirstItem by remember { mutableStateOf(true) }
    var hasLastItem by remember { mutableStateOf(true) }
    Column(Modifier.fillMaxSize()) {
        if (hasFirstItem) {
            Box(Modifier.weight(1f).fillMaxWidth().background(Color.Magenta))
        }
        Box(
            Modifier.fillMaxWidth() // force a fixed size on the content
                .recalculateWindowInsets()
                .weight(1f)
                .background(Color.Yellow)
                .safeDrawingPadding()
        ) {
            Button(
                onClick = { hasFirstItem = !hasFirstItem },
                Modifier.align(Alignment.TopCenter),
            ) {
                val action = if (hasFirstItem) "Remove" else "Add"
                Text("$action First Item")
            }
            Button(
                onClick = { hasLastItem = !hasLastItem },
                Modifier.align(Alignment.BottomCenter),
            ) {
                val action = if (hasLastItem) "Remove" else "Add"
                Text("$action Last Item")
            }
        }
        if (hasLastItem) {
            Box(Modifier.weight(1f).fillMaxWidth().background(Color.Cyan))
        }
    }
}
```
### unconsumedWindowInsetsWithPaddingSample
```kotlin
@Composable
fun unconsumedWindowInsetsWithPaddingSample() {
    // This outer Box is representing a 3rd-party layout that you don't control. It has a
    // padding, but doesn't properly use consumeWindowInsets()
    Box(Modifier.padding(10.dp)) {
        // This is the content that you control. You can make sure that the WindowInsets are correct
        // so you can pad your content despite the fact that the parent did not
        // consumeWindowInsets()
        Box(
            Modifier.fillMaxSize() // Force a fixed size on the content
                .recalculateWindowInsets()
                .safeContentPadding()
                .background(Color.Blue)
        )
    }
}
```

