Home > Software engineering >  Will the system initialize the variable innerPadding in function Scaffold automatically?
Will the system initialize the variable innerPadding in function Scaffold automatically?

Time:11-25

I'm learning Compose, Code A is from the article.

I was told tthe following content:

The body content that is of type @Composable (InnerPadding) -> Unit: the lambda receives a padding as a parameter.

I'm very strange where the variable innerPadding is initialized, will the system initialize the variable innerPadding in function Scaffold automatically?

Code A

 Scaffold { innerPadding ->
        Text(text = "Hi there!", modifier = Modifier.padding(innerPadding))
 }

CodePudding user response:

Inspect the source code and you will see that Scaffold is implemented as a subcompose layout. The bottom bar is subcomposed:

val bottomBarPlaceables = subcompose(ScaffoldLayoutContent.BottomBar) {
    CompositionLocalProvider(
        LocalFabPlacement provides fabPlacement,
        content = bottomBar
    )
}.fastMap { it.measure(looseConstraints) }

innerPadding is then derived from the height of the bottom bar:

val bottomBarHeight = bottomBarPlaceables.fastMaxBy { it.height }?.height ?: 0

val innerPadding = PaddingValues(bottom = bottomBarHeight.toDp())
content(innerPadding)
  • Related