@Composable invocations can only happen from the context of a @Composable function
正如上面所说,您需要从一个带有@Composable
注释的函数中调用一个Composable
如果你判断LazyColumn函数签名
@Composable
fun LazyColumn(
// rest of the params
content: LazyListScope.() -> Unit
) {
}
你会发现内容不是可组合的.另一方面,items
函数使用itemContent
作为可组合函数
inline fun <T> LazyListScope.items(
items: List<T>,
noinline key: ((item: T) -> Any)? = null,
noinline contentType: (item: T) -> Any? = { null },
crossinline itemContent: @Composable LazyItemScope.(item: T) -> Unit
) = items(
count = items.size,
key = if (key != null) { index: Int -> key(items[index]) } else null,
contentType = { index: Int -> contentType(items[index]) }
) {
itemContent(items[it])
}
因此,您可以调用可组合函数,如Column inside
item函数.
items(myItems) { myItem: MyItem ->
Column{
}
}
Column
、Row
或Box
等函数的XScope个函数都标有
@Composable
inline fun Column(
modifier: Modifier = Modifier,
verticalArrangement: Arrangement.Vertical = Arrangement.Top,
horizontalAlignment: Alignment.Horizontal = Alignment.Start,
content: @Composable ColumnScope.() -> Unit
) {
val measurePolicy = columnMeasurePolicy(verticalArrangement, horizontalAlignment)
Layout(
content = { ColumnScopeInstance.content() },
measurePolicy = measurePolicy,
modifier = modifier
)
}
正因为如此,你可以在Column
、Row
或Box
中再拨打Composable
.