As the
TopAppBar
is scrolled,
Scaffold's
content is recomposed (which makes sense since
innerPadding
is changing). I would expect
MyContent
to be skipped (since it takes a stable lambda and a parameter that is set to a
Boolean
literal), but it is called on every recompose.
However, if I hardcode
MyContent's
isVisible = true
instead of
isVisible = isVisible
then it does get skipped.
@Composable
fun Test() {
MyContentWrapper(
content = {},
isVisible = true
)
}
@OptIn(ExperimentalMaterial3Api::class)
@Composable
private fun MyContentWrapper(
content: @Composable () -> Unit,
isVisible: Boolean = true
) {
Scaffold(
topBar = {
TopAppBar(
title = { Text(text = "TopAppBar") },
scrollBehavior = TopAppBarDefaults.enterAlwaysScrollBehavior()
)
}
) { innerPadding ->
Box(
modifier = Modifier.padding(innerPadding)
) {
MyContent(
content = content,
isVisible = isVisible
)
}
}
}
@Composable
private fun MyContent(
content: @Composable () -> Unit,
isVisible: Boolean
) {
Log.e("TEST", "RECOMPOSE")
if(isVisible) {
content()
}
}