Repository navigation
|
Hello. I have a simple Compose Multiplatform app where I want to migrate from the NavHost to Voyager. In my current project I have: @Composable
fun MainApp() {
KoinContext {
AppTheme {
AppStructure()
}
}
}
@Composable
private fun AppStructure() {
val drawerState = rememberDrawerState(initialValue = DrawerValue.Closed)
//Scaffold structure
Scaffold(
topBar = { TopBar(drawerState = drawerState) },
) { innerPadding ->
MainScreenContent(
drawerState = drawerState,
modifier = Modifier
.padding(innerPadding)
.fillMaxSize()
)
}
}
@Composable
private fun MainScreenContent(
drawerState: DrawerState,
modifier: Modifier
) {
//Navigation Drawer Section
ModalNavigationDrawer(
modifier = modifier,
gesturesEnabled = false,
drawerState = drawerState,
drawerContent = {
DrawerMenu(
drawerState = drawerState
)
}
) {
//Here is my NavHost, where I want navigation to happen
NavHost {...}
}
}My idea was just to replace my NavHost with the Navigator and try to use it, but Im getting an exception from LocalNavigator.currentOrThrow and its because Im calling it outside the Navigator. So, I could not find a proper example about how to have the fixed Scaffold and make the navigation happen inside (like I had in my code). Because I could wrap everything inside a Navigator, but in the end, the placeholder where navigation should take place is deep inside my Composables According with the only example I found at docs: @Composable
override fun Content() {
Navigator(HomeScreen) { navigator ->
Scaffold(
topBar = { /* ... */ },
content = { CurrentScreen() },
bottomBar = { /* ... */ }
)
}
}I dont get exactly what are the HomeScreen and the CurrentScreen... Seems like things are out of order. Thanks for the help. |
Replies: 1 comment 1 reply
|
Move the val drawerState = rememberDrawerState(DrawerValue.Closed)
Navigator(HomeScreen) {
Scaffold(
topBar = { TopBar(drawerState = drawerState) },
) { innerPadding ->
ModalNavigationDrawer(
modifier = Modifier
.padding(innerPadding)
.fillMaxSize(),
drawerState = drawerState,
gesturesEnabled = false,
drawerContent = {
DrawerMenu(drawerState = drawerState)
},
) {
CurrentScreen()
}
}
}Here Now Use |
Navigator(HomeScreen)creates the navigation scope and initial stack.CurrentScreen()is the rendering slot for the top of that stack. They do not have to be adjacent: your drawer can sit between them.Move the
Navigatorabove the shared Scaffold, then putCurrentScreen()where yourNavHostwas: