简体   繁体   English

更改底部导航图标颜色(或)图标,点击撰写中的底部项目视图

[英]Change Bottom Navigation Icon colour (or) icon , on tap of bottom Item view in compose

The Question is regarding the Compose Bottom Navigation,问题是关于撰写底部导航,

  • I have created the bottom navigation view with NavHost我用 NavHost 创建了底部导航视图
  • I need to change the Icon state as (Enable/Disable) while clicking on it each time like changing the Icon or Icon colour itself我需要在每次单击时将图标状态更改为(启用/禁用),例如更改图标或图标颜色本身

As you said you implement navigation already .正如你所说,你已经实现了导航。 so now you have to check current route and and bottom navigation screen route所以现在你必须检查当前路线和底部导航屏幕路线

val currentRoute = backStackEntry.value?.destination?.route; //check current route
        val selected = currentRoute == screens.route // if current route equal to screen route it return true

give icon color acc to selected route like this像这样为所选路线提供图标颜色

  Icon(
                        imageVector = screens.icon,
                        contentDescription = "",
                        tint = if (selected) Color.White else Color.Black
                    )

here is full Bottom Navigation Bar implementation这是完整的底部导航栏实现

@Composable

fun BottomBar(
    modifier: Modifier = Modifier,
    screens: List<ScreenModel.HomeScreens>,
    navController: NavController,

    ) {
    BottomNavigation {
        val backStackEntry = navController.currentBackStackEntryAsState()
        screens.forEach {


                screens ->
            val currentRoute = backStackEntry.value?.destination?.route;
            val selected = currentRoute == screens.route

            BottomNavigationItem(
                icon = {
                    Icon(
                        imageVector = screens.icon,
                        contentDescription = "",
                        tint = if (selected) Color.White else Color.Black
                    )
                },
                selected = selected,
                label = {
                    Text(
                        if (selected) screens.title else "", // Label
                        fontWeight = FontWeight.SemiBold,
                        color = Color.White
                    )
                },

                onClick = {
                    if (currentRoute != screens.route) {
                        navController.navigate(screens.route)

                    }

                }

            )
        }

    }

}

screen model file屏幕模型文件

class ScreenModel {

    sealed class HomeScreens(
        val route: String,
        val title: String,
        val icon: ImageVector
    ) {
        object Home : HomeScreens("home", "Home", Icons.Filled.Home)
        object Search : HomeScreens("search", "Search", Icons.Filled.Search)
        object Profile : HomeScreens("profile", "MyNetwork", Icons.Filled.Person)

    }

    val screensInHomeFromBottomNav = listOf(
        HomeScreens.Home, HomeScreens.Search, HomeScreens.Profile
    )

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM