Home > Enterprise >  How can I convert a val to a fun when I use Jetpack Compose in Kotlin?
How can I convert a val to a fun when I use Jetpack Compose in Kotlin?

Time:10-19

The Code A is from the official sample project.

I think I use a function instead of the val background, but the Code B is wrong.

How can I convert a val to a fun when I use Jetpack Compose in Kotlin?

Code A

@Composable
fun NiaApp(
    windowSizeClass: WindowSizeClass,
    appState: NiaAppState = rememberNiaAppState(windowSizeClass)
) {
    NiaTheme {
        val background: @Composable (@Composable () -> Unit) -> Unit =
            when (appState.currentDestination?.route) {
                ForYouDestination.route -> { content -> NiaGradientBackground(content = content) }
                else -> { content -> NiaBackground(content = content) }
            }

        background {
            Scaffold(
                ...
            ) { padding ->
                Row(
                   ...
                ) {
                  ...
            }
        }
    }
}

Code B

@Composable
fun NiaApp(
    windowSizeClass: WindowSizeClass,
    appState: NiaAppState = rememberNiaAppState(windowSizeClass)
) {
    NiaTheme {
         @Composable
         fun background(aa: @Composable () -> Unit){
            when (appState.currentDestination?.route) {
                ForYouDestination.route -> { content -> NiaGradientBackground(content = content) }
                else -> { content -> NiaBackground(content = content) }
            }
        }

        background {
            Scaffold(
                ...
            ) { padding ->
                Row(
                   ...
                ) {
                  ...
            }
        }
    }
}

CodePudding user response:

just cut your Background composable function and paste it outside of the NiaApp composable function

@Composable
fun NiaApp(
    windowSizeClass: WindowSizeClass,
    appState: NiaAppState = rememberNiaAppState(windowSizeClass)
) {
    NiaTheme {
        background {
            Scaffold(
                ...
            ) { padding ->
                Row(
                   ...
                ) {
                  ...
            }
        }
    }    
}

@Composable
fun background(aa: @Composable () -> Unit){
    when (appState.currentDestination?.route) {
        ForYouDestination.route -> { content -> 
            NiaGradientBackground(content = content) }
            else -> { content -> NiaBackground(content = content) }
        }
}

CodePudding user response:

Well, something like this:

@Composable
fun NiaApp(
        windowSizeClass: WindowSizeClass,
        appState: NiaAppState = rememberNiaAppState(windowSizeClass) ) {
        NiaTheme {
            background(appState)() {
                Scaffold(
                    ...
                ) { padding ->
                  }
            }     
        }
}
    
@Composable
fun background(appState: NiaAppState): @Composable (@Composable () -> Unit) -> Unit =
        when (appState.currentDestination?.route) {
            ForYouDestination.route -> { content -> 
                NiaGradientBackground(content = content) }
                else -> { content -> NiaBackground(content = content) }
            } 
  • Related