我试图将动画添加到@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")
}
}
我缺少什么?
尝试使用:
var visible by remember { mutableStateOf(false) }
AnimatedVisibility可对简单布尔值作出反应。