androidkotlinandroid-jetpack-composeandroid-jetpack-compose-material3

Target state parameter `it` is not used when applying animation to NavigationSuite


When apply the Crossfade animation to the NavigationSuiteScaffold the following error is returned. Any ideas on where the word it is supposed to be added?

Target state parameter it is not used

class MainActivity : ComponentActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        enableEdgeToEdge()
        setContent {
            MyAdaptiveAppTheme {
                var currentDestination by remember { mutableStateOf(AppDestinations.HOME) }

                val adaptiveInfo = currentWindowAdaptiveInfo()
                val layoutType = with(adaptiveInfo) {
                    if (windowSizeClass.windowWidthSizeClass == WindowWidthSizeClass.EXPANDED) { NavigationSuiteType.NavigationDrawer } else { NavigationSuiteScaffoldDefaults.calculateFromAdaptiveInfo(adaptiveInfo) }
                }

                NavigationSuiteScaffold(
                    layoutType = layoutType,
                    navigationSuiteItems = {
                        AppDestinations.entries.forEach {
                            item(
                                selected = currentDestination == it,
                                onClick = { currentDestination = it },
                                icon = {
                                    Icon(
                                        imageVector = it.icon,
                                        contentDescription = it.contentDescription.toString()
                                    )
                                },
                                label = { Text(text = stringResource(id = it.label)) }
                            )
                        }
                    },
                ) {
                    Crossfade(
                        targetState = currentDestination,
                        label = "CurrentPage"
                    ) {
                    when(currentDestination) {
                        AppDestinations.HOME -> DestinationHome()
                        AppDestinations.PROFILE -> DestinationProfile()
                    }
                    }
                }
            }
        }
    }
}

Solution

  • Please try to update your Crossfade like this:

    Crossfade(
        targetState = currentDestination,
        label = "CurrentPage"
    ) { targetDestination ->
        when(targetDestination) {
            AppDestinations.HOME -> DestinationHome()
            AppDestinations.PROFILE -> DestinationProfile()
        }
    }
    

    The currently set targetState is passed into the lamdba as a parameter and should be used inside of the lamdba.