首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Modifier.animateContentSize不影响内容的收缩。

Modifier.animateContentSize不影响内容的收缩。
EN

Stack Overflow用户
提问于 2022-09-26 22:40:48
回答 1查看 91关注 0票数 1

这个问题描述了与中解释的相同的问题,但是由于它没有提供任何代码,所以我要打开一个新的代码。

使用Modifier.animateContentSize(),我能够动画内容在卡内的扩展,然而,相对于我所知道的应该发生的事情,我无法动画的内容减少。

每当我点击按钮后,它已经增长,而不是发挥一个缩小的动画,它只是拍回原来的大小,立即,没有任何形式的过渡。

代码语言:javascript
复制
@OptIn(ExperimentalMaterial3Api::class)
@Composable
fun TheThing() {
    var expanded by remember { mutableStateOf(false) }

    // A surface container using the 'background' color from the theme
    Surface(
        modifier = Modifier.fillMaxSize(),
        color = MaterialTheme.colorScheme.background
    ) {
        Column {
            Card(
                onClick = { expanded = !expanded },
                modifier = Modifier
                    .padding(16.dp)
                    .fillMaxWidth()
                    .animateContentSize()
            ) {
                Text("Clickable", style = MaterialTheme.typography.displayMedium)

                if (expanded) {
                    Text("More text here... ")
                    Text("More text here... ")
                    Text("More text here... ")
                    Text("More text here... ")
                    Text("More text here... ")
                    Text("More text here... ")
                    Text("More text here... ")
                    Text("More text here... ")
                    Text("More text here... ")
                    Text("More text here... ")
                    Text("More text here... ")
                    Text("More text here... ")
                    Text("More text here... ")
                }
            }
        }
    }
}
EN

回答 1

Stack Overflow用户

发布于 2022-09-27 15:45:24

对于if语句中的内容,您应该使用AnimatedVisibility。这里发生的事情是,您的if (expanded)语句立即运行,因此它使您的内容立即消失,然后大小的变化是动画的,但内容已经从构图中消失了--使它看起来像没有动画的文本。

在下面的示例中,我正在定制AnimatedVisibility,使其速度更慢,这样您就可以看到实际的动画发生了。如果在它周围设置一个边框,您也会看到大小动画:

代码语言:javascript
复制
@OptIn(ExperimentalMaterialApi::class)
@Composable
fun TheThing() {
    var expanded by remember { mutableStateOf(false) }

    // A surface container using the 'background' color from the theme
    Surface(
        modifier = Modifier.fillMaxSize(),

        ) {
        Column {
            Card(
                onClick = { expanded = !expanded },
                modifier = Modifier
                    .padding(16.dp)
                    .fillMaxWidth()
            ) {
                Column {
                    Text("Clickable")
                    AnimatedVisibility(
                        expanded, exit = fadeOut(animationSpec = tween(1000)) +
                                shrinkVertically(animationSpec = tween(1000))
                    ) {
                        Column {
                            Text("More text here... ")
                            Text("More text here... ")
                            Text("More text here... ")
                            Text("More text here... ")
                            Text("More text here... ")
                            Text("More text here... ")
                            Text("More text here... ")
                            Text("More text here... ")
                            Text("More text here... ")
                            Text("More text here... ")
                            Text("More text here... ")
                            Text("More text here... ")
                            Text("More text here... ")
                        }
                    }
                }
            }
        }
    }
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/73860646

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档