Compose - AnimatedVisibility 停止工作
我试图向 @Composable
添加动画,突然它停止工作,我想做的动画是在滚动 LazyColumn
时,所以我有这个:
Box(
modifier = Modifier
.fillMaxWidth()
.fillMaxHeight()
.background(White)
) {
val visible = remember {
MutableTransitionState(false).apply {
targetState = true
}
}
val fabHeight = 64.dp
val fabHeightPx = with(
LocalDensity.current
) {
fabHeight.roundToPx().toFloat()
}
val fabOffsetHeightPx = remember { mutableStateOf(0f) }
val nestedScrollConnection = remember {
object : NestedScrollConnection {
override fun onPreScroll(
available: Offset,
source: NestedScrollSource
): Offset {
//Some checks and get the anyValue
visible.targetState = anyValue < 0
return Offset.Zero
}
override fun onPostScroll(
consumed: Offset,
available: Offset,
source: NestedScrollSource
): Offset {
//Some checks but I assign the value as this
visible.targetState = true
return super.onPostScroll(consumed, available, source)
}
}
}
LazyColumn(
modifier = Modifier
.padding(start = 16.dp, end = 16.dp, top = 16.dp)
.nestedScroll(nestedScrollConnection)
.align(Alignment.TopCenter),
verticalArrangement = Arrangement.spacedBy(16.dp),
) {
....
这是AnimatedVisibility
AnimatedVisibility(
visible = visible.currentState,
enter = fadeIn(),
exit = fadeOut(),
modifier = Modifier
.align(Alignment.BottomCenter)
.padding(bottom = 16.dp),
) {
Text(text = "Hello")
}
}
我缺少什么?
I was trying to add an animation to a @Composable
and suddenly it stop worked, the animation I want to do is when scrolling a LazyColumn
so I have this :
Box(
modifier = Modifier
.fillMaxWidth()
.fillMaxHeight()
.background(White)
) {
val visible = remember {
MutableTransitionState(false).apply {
targetState = true
}
}
val fabHeight = 64.dp
val fabHeightPx = with(
LocalDensity.current
) {
fabHeight.roundToPx().toFloat()
}
val fabOffsetHeightPx = remember { mutableStateOf(0f) }
val nestedScrollConnection = remember {
object : NestedScrollConnection {
override fun onPreScroll(
available: Offset,
source: NestedScrollSource
): Offset {
//Some checks and get the anyValue
visible.targetState = anyValue < 0
return Offset.Zero
}
override fun onPostScroll(
consumed: Offset,
available: Offset,
source: NestedScrollSource
): Offset {
//Some checks but I assign the value as this
visible.targetState = true
return super.onPostScroll(consumed, available, source)
}
}
}
LazyColumn(
modifier = Modifier
.padding(start = 16.dp, end = 16.dp, top = 16.dp)
.nestedScroll(nestedScrollConnection)
.align(Alignment.TopCenter),
verticalArrangement = Arrangement.spacedBy(16.dp),
) {
....
And this is the AnimatedVisibility
AnimatedVisibility(
visible = visible.currentState,
enter = fadeIn(),
exit = fadeOut(),
modifier = Modifier
.align(Alignment.BottomCenter)
.padding(bottom = 16.dp),
) {
Text(text = "Hello")
}
}
What I'm missing?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
尝试使用:
AnimatedVisibility 可对简单的布尔值做出反应。
Try using:
The AnimatedVisibility works reacting to a simple boolean value.