Jetpack compose TopAppBar 给出调用错误

Jetpack compose TopAppBar gives a call error

当我在 Jetpack compose TopAppBar 中编写以下内容时出现错误。 我认为这是一个基本的错误,但我不知道。 write error

TopAppBar( // ← error:None of the following functions can be called with the arguments supplied.
    title = Text(text = "hogrhoge"),
    actions = {IconButton(...)}

参数 title 的类型为 @Composable () -> Unit,这意味着您必须在那里传递可组合函数。您传递的是从 Text 函数返回的值,即 Unitactions 参数也一样。你应该做的是:

TopAppBar(
    title = { Text(...) },
    actions = { IconButton(...) },
    ...
)

如果检查脚手架函数定义

fun Scaffold(
  modifier: Modifier = Modifier,
  toastHostState: ToastHostState = remember { ToastHostState() },
  toastHost: @Composable (ToastHostState) -> Unit = { ToastHost(it) },
  statusBarMode: StatusBarMode = StatusBarMode.Red,
  topBar: @Composable () -> Unit = {}, // it's expecting a lambda
  bottomBar: @Composable () -> Unit = {},
  floatingActionButton: @Composable () -> Unit = {},
  backgroundColor: Color = NicolletTheme.colors2.backgroundPrimary,
  contentColor: Color = NicolletTheme.colors2.textPrimary,
  content: @Composable (PaddingValues) -> Unit
)

它需要一个 lambda,你需要将它包装在一个 {}

TopAppBar(
  title  = { Text(title = "aa") //your composable } 
)