Animation 制作一张足够大的卡片,以适合子文本

Animation 制作一张足够大的卡片,以适合子文本,animation,flutter,dart,expand,Animation,Flutter,Dart,Expand,我正在创建一个包含长描述的简单卡片 我想将卡的正常高度设置为50px,当用户点击它时,我想扩展卡,以便能够容纳里面的所有内容 到目前为止,为了满足我的需求,我写了以下内容: String aLotOfText = "When Gradle resolves the compile classpath, it first resolves the runtime classpath and uses the result to determine what versions of dep

我正在创建一个包含长描述的简单卡片

我想将卡的正常高度设置为50px,当用户点击它时,我想扩展卡,以便能够容纳里面的所有内容

到目前为止,为了满足我的需求,我写了以下内容:

String aLotOfText = "When Gradle resolves the compile classpath, it first resolves the runtime classpath and uses the result to determine what versions of dependencies should be added to the compile classpath. In other words, the runtime classpath determines the required version numbers for identical dependencies on downstream classpaths. Your app's runtime classpath also determines the version numbers that Gradle requires for matching dependencies in the runtime classpath for the app's test APK. The hierarchy of classpaths is described in figure 1.";

bool cardExpanded = false;

正如您在
height:cardExpanded?null:50,
cardExpanded
true
时,我没有指定高度,因此通过这种方式,我可以将卡扩展到正确的高度以包含文本

但是有了这个小技巧,我已经完全失去了动画效果,只要这张卡与20张或更多其他卡一起进入listview,开始和结束就会使列表上下跳跃

我确信有一种方法可以将卡扩展到正确的高度,同时保持动画


我还想说明的是,上面的一个只是一个例子,在我的卡,将有图像和按钮以及。 此外,当用按钮打开卡片时,我可以添加一些文本,因此打开卡片时,卡片也必须展开。 这就是我无法指定高度的原因


编辑 高度
时:是否展开?null:50
我得到了我想要的,因此,卡片变得足够大以适合内容,但正如您所看到的,那里没有动画


相反,如果我提供一个值,比如
height:cardExpanded?100:50
,我得到了动画,但很明显,卡会一直增长到100像素,并且不会显示所有内容

我想要得到的结果是第一个例子中的结果加上动画


谢谢

您可以设置两个变量:

height=MediaQuery.of(context).size.height*0.8//您希望卡增长的百分比
宽度=MediaQuery.of(上下文).size.width*0.8;
因此,当expanded为true时,容器将增长

高度:是否展开?身高:50,
宽度:扩大?宽度:50,
此外,若要指定动画的执行方式,可以使用一个属性使动画看起来更平滑

curve:Curves.bounceInOut,

或者您可以尝试使用类似expanded to adjust的小部件。

您可以设置两个变量:

height=MediaQuery.of(context).size.height*0.8//您希望卡增长的百分比
宽度=MediaQuery.of(上下文).size.width*0.8;
因此,当expanded为true时,容器将增长

高度:是否展开?身高:50,
宽度:扩大?宽度:50,
此外,若要指定动画的执行方式,可以使用一个属性使动画看起来更平滑

curve:Curves.bounceInOut,

或者您可以尝试使用类似expanded这样的小部件进行调整。

您可以添加一些屏幕截图视频或gif来显示您的问题,以便更好地理解。请尝试。@OMiShah Hi,很抱歉回复太晚,但我已经离开家过圣诞节了。无论如何,我附上了2个截图。希望您理解,谢谢您尝试过在发布模式下运行。它应该工作顺利。运行
flatter Run--release
并检查输出。@OMiShah我已经尝试了
flatter Run--release
但我得到了相同的结果。为了更好地理解您的问题,请添加一些屏幕截图视频或gif?试试。@OMiShah嗨,很抱歉回复晚,但我已经离开圣诞节了。无论如何,我附上了2个截图。希望您理解,谢谢您尝试过在发布模式下运行。它应该工作顺利。运行
flatterrun--release
并检查输出。@OMiShah我尝试了
flatterrun--release
,但我得到了同样的结果。谢谢你的回复,但这不是我想要的。我附上了2个截图,也许现在更容易理解了。谢谢你好,谢谢你的回复,但这不是我想要的。我附上了2个截图,也许现在更容易理解了。谢谢你
InkWell(
      onTap: () {
        expandCard();
      },
        child: AnimatedContainer(
          height: cardExpanded ? null : 50,
          duration: Duration(milliseconds: 200),
          child: Card(
            child: Text(aLotOfText),
          ),
        )
)
expandCard() {
    setState(() {
      cardExpanded = !cardExpanded;
    });
  }