简体   繁体   English

如何更改 Jetpack Compose 抽屉内的 Scaffold 内的内容?

[英]How to change the content inside a Scaffold that exists inside drawer in Jetpack Compose?

I have this drawer:我有这个抽屉:

val drawerState = rememberDrawerState(DrawerValue.Closed)
val scope = rememberCoroutineScope()
val items = listOf(Icons.Default.Favorite, Icons.Default.Face, Icons.Default.Email)
val selectedItem = remember { mutableStateOf(items[0]) }

ModalNavigationDrawer(
    drawerState = drawerState,
    drawerContent = {
        items.forEach { item ->
            NavigationDrawerItem(
                icon = { Icon(item, contentDescription = null) },
                label = { Text(item.name) },
                selected = item == selectedItem.value,
                onClick = {
                    scope.launch { drawerState.close() }
                    selectedItem.value = item
                    //How to change the content of Scaffold's content section?
                },
                modifier = Modifier.padding(NavigationDrawerItemDefaults.ItemPadding)
            )
        }
    },
    content = {
        Scaffold(
            topBar = {
                //
            },
            content = { padding ->
                Text(
                    modifier = Modifier.padding(padding),
                    text = "Favorites"
                )
            }
        )
    }
)

Now, when I run the app, I get the correct TopBar and right under it a Text that holds the value of "Favorites".现在,当我运行应用程序时,我得到了正确的 TopBar,并且在它的正下方有一个包含“Favorites”值的文本。 The problem is that I always want to have the same TopBar, and only change the Scaffold's content.问题是我总是希望拥有相同的 TopBar,并且只更改 Scaffold 的内容。 So instead of "Favorites" I want to display "Face", when the second item is clicked.因此,当单击第二个项目时,我想显示“Face”而不是“Favorites”。

I though to create some composable functions, to display the data, but I cannot call them from inside the onClick.我想创建一些可组合的函数来显示数据,但我不能从 onClick 内部调用它们。 Android Studio is complaining with: Android Studio 抱怨:

@Composable invocations can only happen from the context of a @Composable function @Composable 调用只能在 @Composable function 的上下文中发生

I also thought on creating states and load that data according to that state.我还考虑根据 state 创建状态并加载该数据。 But isn't it so complicated to only display a single text?但是只显示一个文本不是很复杂吗?

In also thought on navigating to other screens but this means that each screen should have it own TopBar, which in my opinion requires adding more complexity to the app for no reason.还考虑过导航到其他屏幕,但这意味着每个屏幕都应该有自己的 TopBar,在我看来,这需要无缘无故地增加应用程序的复杂性。 I'm completely lost.我完全迷路了。 How to solve this issue?如何解决这个问题?

Creating a State to change content is not that complicated same goes for puting a NavHost composable inside your content block.创建 State 来更改内容并不复杂,将 NavHost 可组合放入内容块中也是如此。

object Routes {
    const val Favorites = "Favorites"
    const val Faces = "Faces"
}

var route by remember {mutableStateOf(Routes.Favorites)}

ModalNavigationDrawer(
    drawerState = drawerState,
    drawerContent = {
        items.forEach { item ->
            NavigationDrawerItem(
                icon = { Icon(item, contentDescription = null) },
                label = { Text(item.name) },
                selected = item == selectedItem.value,
                onClick = {
                    scope.launch { drawerState.close() }
                    selectedItem.value = item
                    // Change route
                    route = some new route...
                },
                modifier = Modifier.padding(NavigationDrawerItemDefaults.ItemPadding)
            )
        }
    },
    content = {
        Scaffold(
            topBar = {
                //
            },
            content = { padding ->
               if(route == Routes.Favorites) {
                  Text(
                    modifier = Modifier.padding(padding),
                    text = "Favorites"
                )
              }else {
                // other routes
              }
            }
        )
    }
)

With NavHost使用 NavHost

    NavHost(
        navController = navController,
        startDestination = Routes.Favorites
    ) {
        composable(Routes.Favorites) {
            Favorites()
        }

        composable(Routes. Face) {
            Faces()
        }
    }

You should just simply update a viewmodel state value in onclick and listen to that value inside your composable.您只需简单地更新 onclick 中的视图模型 state 值,然后在您的可组合物中收听该值。 When the value changes you can simply call other composable.当值发生变化时,您可以简单地调用其他可组合。 It isn't that complex.它没有那么复杂。

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

相关问题 在 Scaffold Jetpack Compose 内的特定屏幕上隐藏顶部和底部导航器 - hide Top and Bottom Navigator on a specific screen inside Scaffold Jetpack Compose 如何在 ModalDrawer (Jetpack compose) 中更改抽屉尺寸? - How to change the drawer size in ModalDrawer (Jetpack compose)? Jetpack Compose,如何在 Scaffold 中的 .showSnackbar() 中更改零食栏的动作颜色? - Jetpack Compose, how to change the color of the action of the snackbar in .showSnackbar() in a Scaffold? Jetpack Compose - 更改 Compsable 中的变量 - Jetpack Compose - change variables inside Compsable Jetpack Compose 将文本内容与 Scaffold 的中心对齐 - Jetpack Compose align Text content to center of the Scaffold Jetpack Compose:拖动 MapView 时打开脚手架抽屉 - Jetpack Compose: Scaffold drawer opens when dragging MapView 如何在 Jetpack Compose 中更改 ModalNavigationDrawer 的抽屉容器颜色? - How to change the drawer container color of ModalNavigationDrawer in Jetpack Compose? 如何在 Jetpack Compose 中将列内的元素居中 - How to center elements inside a column in Jetpack Compose 如何在 Jetpack Compose 中将 Surface 中的 Item 居中 - How to center Item inside Surface in jetpack compose 在 Jetpack Compose 中打开抽屉时推送内容 - Push content when drawer opens in Jetpack Compose
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM