Home > Software design >  How to pass a composable function without a lamda?
How to pass a composable function without a lamda?

Time:08-06

Scaffold(
           bottomBar = { SootheBottomNavigation() },
           content = { padding ->
           HomeScreen(Modifier.padding(padding))
       }
       ) 

   @Composable
    fun HomeScreen(modifier: Modifier = Modifier) {
    
         
    }

How do you pass content composable without the lambda into Scaffold? Below is the parameter for Scaffold

content: @Composable (PaddingValues) -> Unit

CodePudding user response:

I'm not sure if I understood your question...

I think you want to do this:

@Composable
fun ScreenContent(paddingValues: PaddingValues) {
    Text("Hello")
}

@Composable
fun MyScreen() {
    Scaffold(
        content = ::ScreenContent // ERROR here
    )
}

But seems like this is not supported, since this error is displayed:

"Function References of @Composable functions are not currently supported"

An alternative would be:

val ScreenContent: @Composable (PaddingValues) -> Unit = { it ->
    Text("Hello")
}

@Composable
fun MyScreen() {
    Scaffold(
        content = ScreenContent
    )
}

But to be honest, I would go with the regular lambda calls...

@Composable 
fun ScreenContent(paddingValues: PaddingValues) { 
    Text("Hello")
}

@Composable
fun MyScreen() {
    Scaffold(
        content = { ScreenContent(it) }
    )
}
  • Related