gpt4 book ai didi

android - 如何将一个 Composable 作为其参数传递给另一个 Composable 并在 Jetpack Compose 中显示/运行它

转载 作者:行者123 更新时间:2023-12-05 04:19:53 45 4
gpt4 key购买 nike

我在 youtube 上学会了这个抽屉

https://www.youtube.com/watch?v=JLICaBEiJS0&list=PLQkwcJG4YTCSpJ2NLhDTHhi6XBNfk9WiC&index=31菲利普·拉克纳

SwipeScreen enter image description here

我想将抽屉添加到我的应用程序中,该应用程序具有多个屏幕,其中一些不需要抽屉,因此我使用屏幕实现了导航,并且一些屏幕还需要将抽屉放在它们上面。

这是抽屉的代码


val scaffoldState = rememberScaffoldState()
val scope = rememberCoroutineScope()
Scaffold(
drawerGesturesEnabled = scaffoldState.drawerState.isOpen,
scaffoldState = scaffoldState, topBar = {
AppBar(onNavigationIconClick = {
scope.launch {
scaffoldState.drawerState.open()
}

})
}, drawerContent = {
DrawerHeader()
DrawerBody(items = listOf(
MenuItem(
id = "home",
title = "Home",
contentDescription = "Go to home screen",
icon = Icons.Default.Home
),
MenuItem(
id = "settings",
title = "Settings",
contentDescription = "Go to Settings screen",
icon = Icons.Default.Settings
),
MenuItem(
id = "help",
title = "Help",
contentDescription = "Go to help screen",
icon = Icons.Default.Info
),
), onItemClick = {
println("Clicked on ${it.title}")

when (it.id) {
"home" -> {
println("Clicked on ${it.title}")
}
"settings" -> {
println("Clicked on ${it.title}")
}
"help" -> {
println("Clicked on ${it.title}")
}
}
})

}) {

Text(text = "Hello World")

}

Text = Hellow world 是我想传递屏幕参数的地方,但我不知道该怎么做。我想添加一个参数,该参数采用可组合函数并在内部运行它

我关注了这个关于如何在 kotlin 中导航的导航视频

https://www.youtube.com/watch?v=4gUeyNkGE3g&list=PLQkwcJG4YTCSpJ2NLhDTHhi6XBNfk9WiC&index=18

它有 3 个大文件,所以如果您要求我将它们发布在这里,但我会尝试更具体地说明需要什么

composable(route = Screen.RegisterScreen.route) {
RegisterScreen(navController = navCotroller)
}

如果我把代码放在抽屉里,效果很好,但我想把代码拆分得更干净,因为我在更多地方使用抽屉

代码像下面的例子一样工作

composable(route = Screen.PreferenceScreen.route) {
val scaffoldState = rememberScaffoldState()
val scope = rememberCoroutineScope()
Scaffold(

drawerGesturesEnabled = scaffoldState.drawerState.isOpen,
scaffoldState = scaffoldState,
topBar = {
AppBar(onNavigationIconClick = {
scope.launch {
scaffoldState.drawerState.open()
}

})
},
drawerContent = {
DrawerHeader()
DrawerBody(items = listOf(
MenuItem(
id = "swipe",
title = "Swipe",
contentDescription = "Go to Swipe screen",
icon = Icons.Default.Home
),
MenuItem(
id = "settings",
title = "Settings",
contentDescription = "Go to Settings screen",
icon = Icons.Default.Settings
),
MenuItem(
id = "profile",
title = "Profile",
contentDescription = "Go to profile screen",
icon = Icons.Default.Info
),
), onItemClick = {
when (it.id) {
"swipe" -> {
navCotroller.navigate(Screen.SwipeScreen.route)
}
"settings" -> {
navCotroller.navigate(Screen.PreferenceScreen.route)
}
"profile" -> {
navCotroller.navigate(Screen.CelebProfileScreen.route)
}
}
})

}) {

-----> PreferenceScreen(navController = navCotroller)

}

}

但这不是干净的代码!!我怎样才能使用函数指针来完成这项工作??

enter image description here

最佳答案

我无法编译你的代码,我不确定这是否能解决你的问题,但基于此

… I want to add a parameter that takes a composable function and runsit inside

您可以将其用作引用。考虑这个可组合项,它接受任何可组合项并在内部运行它

@Composable
fun AnyComposable(
anyComposable: @Composable () -> Unit
) {
anyComposable()
}

另一个可组合项使用上面的可组合项并像这样将任何其他可组合项传递给它

@Composable
fun MyScreen() {

// big red Box composable
val composable1 = @Composable {
Box (
modifier = Modifier
.size(200.dp)
.background(Color.Red)
)
}

// small blue Box composable
val composable2 = @Composable {
Box(
modifier = Modifier
.size(80.dp)
.background(Color.Blue)
)
}

// small green rectangular Box composable
val composable3 = @Composable {
Box(
modifier = Modifier
.size(width = 100.dp, height = 30.dp)
.background(Color.Green)
)
}

var dynamicComposable by remember { mutableStateOf<@Composable () -> Unit> (@Composable { composable1() }) }

Column(
modifier = Modifier.fillMaxSize(),
horizontalAlignment = Alignment.CenterHorizontally
) {

Row(
modifier = Modifier.fillMaxWidth(),
verticalAlignment = Alignment.CenterVertically,
horizontalArrangement = Arrangement.SpaceBetween
) {
Button(onClick = {
dynamicComposable = composable1
}) {
Text("Comp 1")
}

Button(onClick = {
dynamicComposable = composable2
}) {
Text("Comp 2")
}

Button(onClick = {
dynamicComposable = composable3
}) {
Text("Comp 3")
}
}

AnyComposable {
dynamicComposable()
}
}
}

和输出:

enter image description here

关于android - 如何将一个 Composable 作为其参数传递给另一个 Composable 并在 Jetpack Compose 中显示/运行它,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/74665777/

45 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com