Fix home selected item.
This commit is contained in:
parent
288a79b9f6
commit
944180882e
1 changed files with 11 additions and 9 deletions
|
|
@ -14,7 +14,7 @@ import androidx.compose.material3.TopAppBar
|
||||||
import androidx.compose.runtime.Composable
|
import androidx.compose.runtime.Composable
|
||||||
import androidx.compose.runtime.CompositionLocalProvider
|
import androidx.compose.runtime.CompositionLocalProvider
|
||||||
import androidx.compose.runtime.Stable
|
import androidx.compose.runtime.Stable
|
||||||
import androidx.compose.runtime.State
|
import androidx.compose.runtime.derivedStateOf
|
||||||
import androidx.compose.runtime.mutableStateOf
|
import androidx.compose.runtime.mutableStateOf
|
||||||
import androidx.compose.runtime.remember
|
import androidx.compose.runtime.remember
|
||||||
import androidx.compose.runtime.staticCompositionLocalOf
|
import androidx.compose.runtime.staticCompositionLocalOf
|
||||||
|
|
@ -53,20 +53,25 @@ fun HomeNavHost(
|
||||||
)
|
)
|
||||||
},
|
},
|
||||||
bottomBar = {
|
bottomBar = {
|
||||||
|
val selectedIndex = remember { mutableStateOf(0) }
|
||||||
BottomAppBar(
|
BottomAppBar(
|
||||||
containerColor = MaterialTheme.colorScheme.surface,
|
containerColor = MaterialTheme.colorScheme.surface,
|
||||||
tonalElevation = 0.dp,
|
tonalElevation = 0.dp,
|
||||||
) {
|
) {
|
||||||
bottomBarItems.forEach {
|
bottomBarItems.forEachIndexed { index, item ->
|
||||||
|
val selectedItem = remember { derivedStateOf { selectedIndex.value == index } }
|
||||||
NavigationBarItem(
|
NavigationBarItem(
|
||||||
selected = it.selected.value,
|
selected = selectedItem.value,
|
||||||
onClick = it.onClick,
|
onClick = {
|
||||||
|
selectedIndex.value = index
|
||||||
|
item.onClick.invoke()
|
||||||
|
},
|
||||||
label = {
|
label = {
|
||||||
Text(text = stringResource(id = it.label))
|
Text(text = stringResource(id = item.label))
|
||||||
},
|
},
|
||||||
icon = {
|
icon = {
|
||||||
Icon(
|
Icon(
|
||||||
painter = painterResource(id = it.icon),
|
painter = painterResource(id = item.icon),
|
||||||
contentDescription = "",
|
contentDescription = "",
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
@ -93,7 +98,6 @@ fun HomeNavHost(
|
||||||
|
|
||||||
@Stable
|
@Stable
|
||||||
class BottomBarItem(
|
class BottomBarItem(
|
||||||
val selected: State<Boolean>,
|
|
||||||
val icon: Int,
|
val icon: Int,
|
||||||
val label: Int,
|
val label: Int,
|
||||||
val onClick: () -> Unit,
|
val onClick: () -> Unit,
|
||||||
|
|
@ -107,13 +111,11 @@ private fun rememberBottomBarItems(
|
||||||
return remember(navHostController) {
|
return remember(navHostController) {
|
||||||
listOf(
|
listOf(
|
||||||
BottomBarItem(
|
BottomBarItem(
|
||||||
selected = mutableStateOf(false),
|
|
||||||
icon = R.drawable.ic_outline_account_circle_24,
|
icon = R.drawable.ic_outline_account_circle_24,
|
||||||
label = R.string.home_lexicon,
|
label = R.string.home_lexicon,
|
||||||
onClick = { navHostController.navigateToLexicon(navHostController.pageOption()) }
|
onClick = { navHostController.navigateToLexicon(navHostController.pageOption()) }
|
||||||
),
|
),
|
||||||
BottomBarItem(
|
BottomBarItem(
|
||||||
selected = mutableStateOf(false),
|
|
||||||
icon = R.drawable.ic_outline_map_24,
|
icon = R.drawable.ic_outline_map_24,
|
||||||
label = R.string.home_quest_log,
|
label = R.string.home_quest_log,
|
||||||
onClick = { navHostController.navigateToQuestList(navHostController.pageOption()) }
|
onClick = { navHostController.navigateToQuestList(navHostController.pageOption()) }
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue