在 Jetpack Compose 中导航到没有先前脚手架的另一个屏幕

Navigate to another screen without the previous scaffold in Jetpack Compose

我的应用有一个带有脚手架和底部导航栏的主屏幕:

 override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContent {
            val navController = rememberNavController()
            MyApplicationTheme {
                Scaffold(
                    bottomBar = {
                        BottomBar(navController = navController)
                    }
                ) {
                    NavigationGraph(navController = navController)
                }
            }
        }
    }

...

@Composable
fun NavigationGraph(navController: NavHostController){
    NavHost(navController = navController, startDestination = BottomMenuOption.Home.route) {

        composable(route = BottomMenuOption.Home.route) {
            HomeScreen(navController = navController)
        }

        composable(route = BottomMenuOption.Settings.settings) {
            SettingsScreen()
        }
        composable(route = BottomMenuOption.Profile.route) {
            ProfileScreen()
        }

       composable(route = "feature") {
           FeatureScreen()
       }
    }

}

FeatureScreen 有自己的 Scaffold,带有 topBar 而不是 bottomBar,当我从 HomeScreen 导航到它时,我想从主屏幕,只看到一个顶部栏,但相反,我在屏幕上看到了两个栏。

@Composable
fun FeatureScreen() {
    Scaffold (
        topBar = {
            TopBar(" Feature Screen")
        }
            )  {

    }
}

这有可能实现吗?我认为这可以通过使用新的 Activity 来完成,但理想情况下,我想保留 Single Activity 方法。

我建议像这样创建一个新函数:

@Composable
fun MainScaffold(
    topBar: @Composable (() -> Unit) = {},
    bottomBar: @Composable (() -> Unit) = {},
    content: @Composable (PaddingValues) -> Unit){
    Scaffold(
        bottomBar = bottomBar,
        topBar = topBar,
        content = content
    )
}

然后,在您的屏幕中使用这个主支架:

@Composable
fun HomeScreen(navController: NavHostController) {
    MainScaffold(
        bottomBar = { BottomBar(navController = navController) },
        content = {
        // content
     })
}

并在您的功能屏幕中:

@Composable
fun FeatureScreen() {
    MainScaffold (
        topBar = {
            TopBar(" Feature Screen")
        }
    )  {
        //content
    }
}

并在 setContent

setContent {
       val navController = rememberNavController()
       VoiceAssistantJetpackComposeTheme {
           NavigationGraph(navController = navController)
       }}