在底部项目视图上更改底部导航图标颜色(或)图标(或)图标

发布于 2025-02-10 06:36:28 字数 131 浏览 1 评论 0原文

问题是关于撰写的底部导航,

  • 我已经使用navhost创建了底部导航视图,
  • 我需要在每次单击它时(启用/disable)将图标状态更改为(启用/disable),就像更改图标或图标颜色本身一样

The Question is regarding the Compose Bottom Navigation,

  • I have created the bottom navigation view with 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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

音盲 2025-02-17 06:36:29

正如您所说,您已经实施了导航。因此,现在您必须检查当前路线以及底部导航屏幕路线

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

将图标颜色acc给所选路线,如

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

以下是完整的底部导航栏实现

@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)

                    }

                }

            )
        }

    }

}

屏幕模型文件

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
    )

}

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
    )

}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文