admin管理员组文章数量:1122794
I'm making a Kotlin Multiplatform / Compose Multiplatform app. I want this sort of graph :
- SplashScreen
- AuthScreen
- MainGraph
- HomeScreen
- ...
- SettingsScreen
I want a navigation bar to navigate through my main graph, but obviously, I don't want to have this navigation bar when I'm on my SplashScreen or AuthScreen. I thought it would be easy but strangely I can't find any post asking this or anything in the documentation about that. And ChatGPT keeps giving me code that doesn't even work.
I've tried using another NavHost in a composable block but it won't work. Like that :
val navController = rememberNavController()
NavHost(
navController = navController,
startDestination = Destination.Splash
) {
composable<Destination.Splash> {
SplashScreenRoot()
}
composable<Destination.Auth> {
AuthScreenRoot()
}
composable<Destination.Main> {
// Add navigation here
NavHost(
navController = navController,
startDestination = Destination.Main.Home
) {
composable<Destination.Main.Home> {
HomeScreenRoot()
}
composable<Destination.Main.Settings> {
SettingsScreenRoot()
}
}
}
}
I've seen a way to do this by having a nav bar and showing it depending on the current route but it seems weird that there wouldn't be a cleaner way to do this seeing as it is a pretty basic situation.
I'm making a Kotlin Multiplatform / Compose Multiplatform app. I want this sort of graph :
- SplashScreen
- AuthScreen
- MainGraph
- HomeScreen
- ...
- SettingsScreen
I want a navigation bar to navigate through my main graph, but obviously, I don't want to have this navigation bar when I'm on my SplashScreen or AuthScreen. I thought it would be easy but strangely I can't find any post asking this or anything in the documentation about that. And ChatGPT keeps giving me code that doesn't even work.
I've tried using another NavHost in a composable block but it won't work. Like that :
val navController = rememberNavController()
NavHost(
navController = navController,
startDestination = Destination.Splash
) {
composable<Destination.Splash> {
SplashScreenRoot()
}
composable<Destination.Auth> {
AuthScreenRoot()
}
composable<Destination.Main> {
// Add navigation here
NavHost(
navController = navController,
startDestination = Destination.Main.Home
) {
composable<Destination.Main.Home> {
HomeScreenRoot()
}
composable<Destination.Main.Settings> {
SettingsScreenRoot()
}
}
}
}
I've seen a way to do this by having a nav bar and showing it depending on the current route but it seems weird that there wouldn't be a cleaner way to do this seeing as it is a pretty basic situation.
Share Improve this question asked Nov 21, 2024 at 22:55 DamonDamon 1498 bronze badges1 Answer
Reset to default 1You can do it by setting a nested NavHost that contains navigation bar with a Scaffold.
val navController = rememberNavController()
NavHost(
navController = navController,
startDestination = Destination.Splash
) {
composable<Destination.Splash> {
SplashScreenRoot()
}
composable<Destination.Auth> {
AuthScreenRoot()
}
composable<Destination.Main> {
MainContainer()
}
}
And MainContainer that contains nested NavHost with bottom navigation
@Composable
private fun MainContainer() {
val nestedNavController = rememberNavController()
Scaffold(
modifier = Modifier.fillMaxSize(),
bottomBar = {
NavigationBar {
}
}
) { paddingValues: PaddingValues ->
NavHost(
modifier = Modifier.padding(paddingValues),
navController = nestedNavController,
startDestination = Destination.Main.Home
) {
addBottomNavigationGraph()
}
}
}
You can check full sample in github repo.
本文标签:
版权声明:本文标题:navigationbar - With Compose, how can I show a navigation bar only on a sub navigation graph - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1736306695a1933051.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论