gpt4 book ai didi

flutter - 具有动态高度的动画容器 Flutter

转载 作者:行者123 更新时间:2023-12-05 08:37:13 31 4
gpt4 key购买 nike

我正在使用 animatedContainer 在按下按钮时显示 listView.builder()。这是一个要显示的简单子(monad)列表,但问题是我不知道 ListView 构建器的高度会传递给 animatedContainer 高度约束。有什么方法可以动态获取 ListView 生成器的高度或任何其他方法来实现这一点?

我需要什么?

  • 动画容器需要高度,但我不知道高度 ListView 生成器。
  • 当我使用 Normal Container 时,我没有设置 height: 属性,所以它会自动环绕 child

是这样的:https://getbootstrap.com/docs/4.0/components/collapse/

AnimatedContainer(
duration: Duration(milliseconds: 200),
curve: Curves.easeIn,
height: _expanded ? 150 : 0, // This height I cannot set here explicitly. I need to set dynamically like, get child's height
child: ListView.builder(
key: listViewKey,
shrinkWrap: true,
scrollDirection: Axis.vertical,
physics: const NeverScrollableScrollPhysics(),
itemCount: loadedSubCategories.length,
itemBuilder: (ctx, i) => ChangeNotifierProvider.value(
value: loadedSubCategories[i],
child: SubCategoryItem(loadedSubCategories[i].categoryId,
loadedSubCategories[i].subCategoryId)),
),
),

当我使用普通的 Container() 时,我不会设置 height: 属性,这样它会自动获取 child 的高度,但我需要高度,因为我想扩展子类别一种动画方式。

SubCategories 的每个元素具有不同的高度和 Subcategories 的数量也是未知的。所以用 subcategories.length 计算高度也是不可能的。

非常感谢任何建议,谢谢

最佳答案

您可以使用 AnimatedSize 小部件实现您想要的。

它会自动创建一个小部件,该小部件会自行设置动画以匹配其子级大小,您无需为其提供任何高度或宽度。你只需要用它包装你的小部件

AnimatedSize(
curve: Curves.fastOutSlowIn,
duration: Duration(milliseconds: 300),
vsync: this,
child: Container()
)

你可以看看at获取更多信息。

关于flutter - 具有动态高度的动画容器 Flutter,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66101838/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com