Skip to content
Snippets Groups Projects
Commit b68d0e22 authored by Fodor Patrik's avatar Fodor Patrik :ghost:
Browse files

Create bottom navigation bar

parent b6c97372
Branches
No related tags found
No related merge requests found
package hu.bme.kszk.szobatarsch.composable
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.filled.*
import androidx.compose.material3.*
import androidx.compose.runtime.Composable
import androidx.compose.runtime.getValue
import androidx.compose.ui.graphics.vector.ImageVector
import androidx.navigation.NavGraph.Companion.findStartDestination
import androidx.navigation.NavHostController
import androidx.navigation.compose.currentBackStackEntryAsState
sealed class NavRoutes(val route: String) {
object Find : NavRoutes("find")
object Matches : NavRoutes("matches")
object Profile : NavRoutes("profile")
object Settings : NavRoutes("settings")
}
data class BarItem(
val title: String,
val image: ImageVector,
val route: String
)
object NavBarItems {
val BarItems = listOf(
BarItem(
title = "Find",
image = Icons.Filled.Search,
route = "find"
),
BarItem(
title = "Matches",
image = Icons.Filled.ThumbUp,
route = "matches"
),
BarItem(
title = "Profile",
image = Icons.Filled.Face,
route = "profile"
),
BarItem(
title = "Settings",
image = Icons.Filled.Settings,
route = "settings"
)
)
}
@Composable
fun BottomNavigationBar(navController: NavHostController) {
NavigationBar {
val backStackEntry by navController.currentBackStackEntryAsState()
val currentRoute = backStackEntry?.destination?.route
NavBarItems.BarItems.forEach { navItem ->
NavigationBarItem(
selected = currentRoute == navItem.route,
onClick = {
navController.navigate(navItem.route) {
popUpTo(navController.graph.findStartDestination().id) {
saveState = true
}
launchSingleTop = true
restoreState = true
}
},
icon = {
Icon(imageVector = navItem.image,
contentDescription = navItem.title)
},
label = {
Text(text = navItem.title)
},
)
}
}
}
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment