簡體   English   中英

為什么 TopAppBar navigationIcon 不是可組合的 function?

[英]Why is TopAppBar navigationIcon's not a composable function?

我的 IDE 顯示 navigationIcon 不是可組合的 function。其他人也在做同樣的事情。 我收到這個錯誤

@composable invocations can only happen from the context of an @composable function
@Composable
fun AppBar(onClick: () -> Unit){
    TopAppBar(
        title = "Princess World", 
        navigationIcon = { 
            IconButton(onClick = onClick) {
                Icon(imageVector = Icons.Default.Menu, contentDescription = null)
            } 
        },
    ) {}
}

我無法在標題和導航圖標 {} 內使用可組合函數

@Composable
fun AppBar(onClick: () -> Unit){
   TopAppBar(title = { }, navigationIcon = { }) {

   }
}

看起來有 2 個 TopAppBar 可組合項,您必須使用它們的相應參數正確調用它們。

這個,

@Composable
fun AppBar(onClick: () -> Unit) {
    TopAppBar(
        title = { Text (text = "Princess World") },
        navigationIcon = {
            IconButton(onClick = onClick) {
                Icon(imageVector = Icons.Default.Menu, contentDescription = null)
            }
        }
    ) 
}

從 API 調用這個,

@Composable
    fun TopAppBar(
        title: @Composable () -> Unit,
        modifier: Modifier = Modifier,
        navigationIcon: @Composable (() -> Unit)? = null,
        actions: @Composable RowScope.() -> Unit = {},
        backgroundColor: Color = MaterialTheme.colors.primarySurface,
        contentColor: Color = contentColorFor(backgroundColor),
        elevation: Dp = AppBarDefaults.TopAppBarElevation
    ) { … }

或者這個,

TopAppBar {

}

從 API 調用這個

@Composable
fun TopAppBar(
    modifier: Modifier = Modifier,
    backgroundColor: Color = MaterialTheme.colors.primarySurface,
    contentColor: Color = contentColorFor(backgroundColor),
    elevation: Dp = AppBarDefaults.TopAppBarElevation,
    contentPadding: PaddingValues = AppBarDefaults.ContentPadding,
    content: @Composable RowScope.() -> Unit
) { … }

您必須刪除最后的{}

TopAppBar(
    title = { Text("Princess World") },
    navigationIcon = {
        IconButton(onClick = onClick) {
            Icon(imageVector = Icons.Default.Menu, contentDescription = null)
        }
    }
)

使用{}您正在嘗試使用具有屬性content: @Composable RowScope.() -> Unit沒有titlenavigationIcon屬性的單元。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM