fitInside

Functions summary

Modifier

Fits the contents within rulers.

Cmn

Functions

Modifier.fitInside

fun Modifier.fitInside(rulers: RectRulers): Modifier

Fits the contents within rulers. 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 fitInside. If the Constraints sizes aren't fixed, fitInside will size the child to the Constraints and try to center the content within rulers.

import androidx.compose.foundation.background
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.fitInside
import androidx.compose.foundation.layout.fitOutside
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.layout.WindowInsetsRulers.Companion.NavigationBars
import androidx.compose.ui.layout.WindowInsetsRulers.Companion.SafeContent
import androidx.compose.ui.layout.WindowInsetsRulers.Companion.StatusBars

Box(Modifier.fillMaxSize()) {
    // Drawn behind the status bar
    Box(Modifier.fillMaxSize().fitOutside(StatusBars.current).background(Color.Blue))
    // Drawn behind the navigation bar
    Box(Modifier.fillMaxSize().fitOutside(NavigationBars.current).background(Color.Red))
    // Body of the app
    Box(Modifier.fillMaxSize().fitInside(SafeContent.current).background(Color.Yellow))
}
See also
fitOutside