我想隐藏 navigationIcon 但是当我传递 null
或 Unit
时,图标空间仍然可见,如图所示下面。
@Composable
fun DefaultScaffold(
title: String? = null,
icon: ImageVector? = null,
content: @Composable() () -> Unit
) {
Scaffold(
topBar = {
TopAppBar(
title = {
if (title != null) {
Text(text = "My Title")
}
},
navigationIcon = {
if (icon != null) {
IconButton(
onClick = {}
) {
Icon(imageVector = Icons.Filled.Close, contentDescription = null)
}
} else Unit
}
)
}
) {
content()
}
}
最佳答案
移除条件语句周围的括号,然后在您不想显示图标空间时返回 null
而不是 Unit
。
所以,会变成
navigationIcon = if (icon != null) {
{
IconButton(
onClick = {}
) {
Icon(imageVector = Icons.Filled.Close, contentDescription = null)
}
}
} else null
关于android - 如何在 Jetpack Compose 中传递条件参数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/68931199/