mirror of
https://github.com/msfjarvis/compose-lobsters
synced 2025-08-15 07:37:03 +05:30
android: use navigation destination to signal selected state
This commit is contained in:
parent
2bfdab7a1a
commit
f8e7043a42
1 changed files with 2 additions and 9 deletions
|
@ -11,10 +11,6 @@ import androidx.compose.material3.NavigationBar
|
||||||
import androidx.compose.material3.NavigationBarItem
|
import androidx.compose.material3.NavigationBarItem
|
||||||
import androidx.compose.material3.Text
|
import androidx.compose.material3.Text
|
||||||
import androidx.compose.runtime.Composable
|
import androidx.compose.runtime.Composable
|
||||||
import androidx.compose.runtime.getValue
|
|
||||||
import androidx.compose.runtime.mutableStateOf
|
|
||||||
import androidx.compose.runtime.remember
|
|
||||||
import androidx.compose.runtime.setValue
|
|
||||||
import androidx.compose.ui.Modifier
|
import androidx.compose.ui.Modifier
|
||||||
import androidx.compose.ui.graphics.painter.Painter
|
import androidx.compose.ui.graphics.painter.Painter
|
||||||
import androidx.navigation.NavController
|
import androidx.navigation.NavController
|
||||||
|
@ -28,8 +24,6 @@ fun ClawNavigationBar(
|
||||||
isVisible: Boolean,
|
isVisible: Boolean,
|
||||||
modifier: Modifier = Modifier,
|
modifier: Modifier = Modifier,
|
||||||
) {
|
) {
|
||||||
var selectedIndex by remember { mutableStateOf(0) }
|
|
||||||
|
|
||||||
AnimatedVisibility(
|
AnimatedVisibility(
|
||||||
visible = isVisible,
|
visible = isVisible,
|
||||||
enter =
|
enter =
|
||||||
|
@ -47,13 +41,12 @@ fun ClawNavigationBar(
|
||||||
modifier = Modifier,
|
modifier = Modifier,
|
||||||
) {
|
) {
|
||||||
NavigationBar(modifier = modifier) {
|
NavigationBar(modifier = modifier) {
|
||||||
items.forEachIndexed { index, navItem ->
|
items.forEach { navItem ->
|
||||||
NavigationBarItem(
|
NavigationBarItem(
|
||||||
icon = { Icon(painter = navItem.icon, contentDescription = navItem.label) },
|
icon = { Icon(painter = navItem.icon, contentDescription = navItem.label) },
|
||||||
label = { Text(text = navItem.label) },
|
label = { Text(text = navItem.label) },
|
||||||
selected = selectedIndex == index,
|
selected = navController.currentDestination?.route == navItem.route,
|
||||||
onClick = {
|
onClick = {
|
||||||
selectedIndex = index
|
|
||||||
if (navController.currentDestination?.route == navItem.route) return@NavigationBarItem
|
if (navController.currentDestination?.route == navItem.route) return@NavigationBarItem
|
||||||
navController.popBackStack(navController.graph.startDestinationRoute!!, false)
|
navController.popBackStack(navController.graph.startDestinationRoute!!, false)
|
||||||
if (navItem.route != Destinations.startDestination.getRoute()) {
|
if (navItem.route != Destinations.startDestination.getRoute()) {
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue