【发布时间】:2021-05-12 01:43:02
【问题描述】:
当按下按钮时,我正在使用 animatedContainer 来显示 listView.builder()。这是一个简单的子列表来显示,但问题是我不知道 ListView 构建器的高度传递给 animatedContainer 高度约束。有没有办法动态获取列表视图构建器的高度或任何其他方法来实现这一点?
我需要什么?
- 动画容器需要高度,但我不知道高度 列表视图生成器。
- 当我使用普通容器时,我没有设置
height:属性,所以 它会自动环绕孩子
类似这样的: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: 属性,以便它会自动获取孩子的高度,但我需要高度,因为我想以动画方式扩展子类别。
SubCategories 的每个元素具有不同的高度和 Subcategories 的数量也是未知的。所以用subcategories.length计算高度也是不可能的。
非常感谢任何建议,谢谢
【问题讨论】:
-
用普通的
Container包裹你的ListView.builder并给出稳定的高度值 -
好的,我得到了你想要的。创建 Animation
和 animationcontroller 在创建之后你需要创建 initState 方法并在这里实现你的 animationController,然后实现你的 Animation 并给出 Tween 的参数 -
所以你应该创建你的自定义动画容器
-
这样,我不想设置高度?容器是否了解孩子的高度并通过动画展开?
-
_heightAnimation = Tween
( begin: Size(double.infinity, 100), end: Size(double.infinity, 350)) .animate(CurvedAnimation(parent: _controller, curve: Curves.轻松));我应该设置什么高度?正如我提到的,我无法手动设置高度。做任何事情来获得孩子的身高。