Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Shared Group Composables #343

Open
Eschryn opened this issue Jun 25, 2024 · 1 comment
Open

Shared Group Composables #343

Eschryn opened this issue Jun 25, 2024 · 1 comment

Comments

@Eschryn
Copy link

Eschryn commented Jun 25, 2024

I would like to be able to define a shared Composable that will wrap the content of a group.
for example:

val navigator = rememberNavigator()
NavHost(
    navigator = navigator
) {
    group("navigationBar", "home") {
        wrapper = { content ->
            val items = listOf("home", "library", "settings")
 
            Scaffhold(
                bottomBar = {
                    NavigationBar {
                        items.forEachIndexed { index, item ->
                            NavigationBarItem(
                                icon = { Icon(Icons.Filled.Favorite, contentDescription = item) },
                                label = { Text(item) },
                                selected = selectedItem == index,
                                onClick = { 
                                    selectedItem = index,
                                    navigator = navigator.navigate("/navigationBar/${items[selectedIndex]}")
                                }
                            )
                        }
                    }
                }
            ) { innerPadding ->
                content()
            }
        }
    
        scene("home") { HomeScreen() }
        scene("library") { LibraryScreen() }
        scene("setting") { SettingsScreen() }
    }
}
@Tlaster
Copy link
Owner

Tlaster commented Jun 26, 2024

Currently you can have a composable that does the same thing and wrap your screen like this:

@Composable
fun YourScaffhold(
    navigator: Navigator,
    content: @Composable () -> Unit,
) {
    val items = listOf("home", "library", "settings")
    Scaffhold(
        bottomBar = {
            NavigationBar {
                items.forEachIndexed { index, item ->
                    NavigationBarItem(
                        icon = { Icon(Icons.Filled.Favorite, contentDescription = item) },
                        label = { Text(item) },
                        selected = selectedItem == index,
                        onClick = { 
                            selectedItem = index,
                            navigator = navigator.navigate("/navigationBar/${items[selectedIndex]}")
                        }
                    )
                }
            }
        }
    ) { innerPadding ->
        content()
    }
}
    // in your NavHost's scene
    scene("home") { YourScaffhold(navigator = navigator) { HomeScreen() } }

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants