在 Jetpack Compose 中,
ScrollToTopButton
是从哪里来的?它在 Google 的文档中提到了,但遗憾的是他们没有提到该包。我已经导入了 foundation version 1.2.0-alpha08
,也尝试过使用 1.2.0-beta02
以及 ui 和 material(1.1.1),但都没找到。(是的,我已经在互联网上搜索了这个术语,但没有结果。)implementation "androidx.compose.foundation:foundation:${version}"
implementation "androidx.compose.foundation:foundation-layout:${version}"
implementation "androidx.compose.ui:ui:$compose_version"
implementation "androidx.compose.material:material:$compose_version"
@Composable
fun MessageList(messages: List<Message>) {
val listState = rememberLazyListState()
// Remember a CoroutineScope to be able to launch
val coroutineScope = rememberCoroutineScope()
LazyColumn(state = listState) {
// ...
}
ScrollToTopButton(
onClick = {
coroutineScope.launch {
// Animate scroll to the first item
listState.animateScrollToItem(index = 0)
}
}
)
}
编辑:如果这不是他们提供的功能,而是建议创建自己的功能,那么被撰写文档的人真是可耻,它明确建议是Compose提供的一个函数。
编辑2:事实证明它是一个自定义的函数(请查看答案)。为什么文档的作者要这样写呢?为什么不直接写Button呢?唉。