簡體   English   中英

Jetpack Compose - BottomNavigationItem 交換圖標

[英]Jetpack Compose - BottomNavigationItem swapping Icons

我試圖找出是否有一種方法可以根據所選狀態更改我的可組合Icon使用的Image ,類似於selectedContentColor和 unselectedConstentColor 如何根據所選unselelectedConstentColor更改。

我有以下代碼:

@Composable
fun BottomNavigation(navController: NavController) {
       val items = listOf(
        BottomNavItem.Home,
        BottomNavItem.Track,
        BottomNavItem.Clothes,
        BottomNavItem.Logs,
        BottomNavItem.Ideas,
        BottomNavItem.Sessions
    )
    BottomNavigation(
        modifier = Modifier
            .height(90.dp)
            .padding(0.dp),
        backgroundColor = colorResource(id = R.color.Black),
        contentColor = colorResource(id = R.color.Black)

    ) {
        val navBackStackEntry by navController.currentBackStackEntryAsState()
        val currentRoute = navBackStackEntry?.destination?.route
        items.forEach { item ->
            BottomNavigationItem(
                modifier = Modifier
                    .scale(.7F)
                    .fillMaxHeight()
                    .requiredHeight(90.dp)
                    .padding(0.dp),
                selected = currentRoute == item.screen_route,
                icon = {
                    Image(painterResource(id = item.icon), contentDescription = "none")
                },
                selectedContentColor = Color.White,
                unselectedContentColor = Color.Black.copy(0.6f),
                alwaysShowLabel = false,
                onClick = {
                    navController.navigate(item.screen_route) {
                        navController.graph.startDestinationRoute?.let { screen_route ->
                            popUpTo(screen_route) {
                                saveState = true
                            }
                        }
                        launchSingleTop = true
                        restoreState = true
                    }
                }
            )
        }
    }
}

目標是根據所選狀態進行以下更改。 雖然這行不通,但本質上,

if (selected){ icon = { Image(painterResource(id = item.icon_on), contentDescription = "selected")} }else{
icon = { Image(painterResource(id = item.icon_off), contentDescription = "not selected")}
}

我嘗試使用此邏輯並在調用 BottomNavigationItem 可組合項 function 之前定義一個新變量,但這沒有用。 同樣,嘗試在參數中使用 if 也是行不通的。

感謝下面的評論,我想通了(盡管我認為我已經嘗試過了)。

icon = { if(currentRoute == item.screen_route){
 Image(painterResource(id = item.icon_on), contentDescription = "none")
}else{
 Image(painterResource(id = item.icon_off), contentDescription = "none")
}
},

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM