Skip to main content
Version: 12.10.0

:OCBottomNavigation

:How to use OCBottomNavigation

  • OCBottomNavigation allows to simplify the bottom navigation implementation with simple steps.

  • Create a list of bottom navigation items using type OCBottomNavigationItem and keep it immutable.

    val bottomNavigationItems: List<OCBottomNavigationItem> = listOf(
OCBottomNavigationItem("Home", "home", R.drawable.ic_home,R.drawable.ic_home),
OCBottomNavigationItem("Chat", "chat",R.drawable.ic_feature,R.drawable.ic_feature),
OCBottomNavigationItem("Settings", "settings", R.drawable.ic_settings,R.drawable.ic_settings),
OCBottomNavigationItem("Account", "settings", R.drawable.ic_settings,R.drawable.ic_settings),
OCBottomNavigationItem("Profile", "settings",R.drawable.ic_settings,R.drawable.ic_settings),
)
  • Also create a selected destination which will be updated on navigation.
    val currentDestination: NavDestination?
@Composable get() = navController
.currentBackStackEntryAsState().value?.destination

val selectedBottomNavigationItem: OCBottomNavigationItem?
@Composable get() = bottomNavigationItems.find { it.route == currentDestination?.route }

  • Now call the OCBottomNavigation constructor and assign it to scaffold's bottom bar.
    val appState = rememberAppState()
Scaffold(
bottomBar =
{
OCBottomNavigation(
items = appState.bottomNavigationItems,
selectedItem = appState.selectedBottomNavigationItem,
itemsWithBadge = appState.bottomNavigationItemsWithBadge,
onItemClick = {
appState.navigateToSelectedItem(it)
},
)
},
)
{ Navigation(appState = appState) }

  • If we click on an item now it will navigate to respective screen and update the state, which in turn will update the selected item and recompose the view
  • In a similar fashion state update need to be done for the items with badges according to the corresponding business logic