【问题标题】:Animated Container with dynamix height Flutter动态混合高度 Flutter 的动画容器
【发布时间】: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.轻松));我应该设置什么高度?正如我提到的,我无法手动设置高度。做任何事情来获得孩子的身高。

标签: flutter animatedcontainer


【解决方案1】:

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

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

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

您可以查看at 了解更多信息。

【讨论】:

    【解决方案2】:

    使用 Curves.fastLinearToSlowEaseIn 和 SizeBox.shrink 来实现您的要求

    @override
    Widget build(BuildContext context) {
      return SafeArea(
        bottom: true,
        top: false,
        child: Scaffold(
          appBar: AppBar(
            title: Text('Container'),
          ),
          body: Column(
            children: [
              Center(
                child: FlatButton(
                    onPressed: () {
                      setState(() {
                        _expanded = true;
                      });
                    },
                    child: Text('expand')),
              ),
              AnimatedContainer(
                duration: Duration(seconds: 2),
                curve: Curves.fastLinearToSlowEaseIn,
                height: _expanded ? 150 : 0,
                clipBehavior: Clip.antiAlias,
                decoration: BoxDecoration(
                    color: Colors.white
                ),
                child: _expanded ?  ListView.builder(
                    shrinkWrap: true,
                    scrollDirection: Axis.vertical,
                    physics: const NeverScrollableScrollPhysics(),
                    itemCount: 3,
                    itemBuilder: (ctx, i) {
                      return ListTile(
                        title: Text(i.toString()),
                      );
                    }) : SizedBox.shrink(),
              ),
            ],
          )
        ),
      );
    }
    

    【讨论】:

    • 兄弟,我让动画在它之前正常工作。但高度 150 不能明确设置。有什么办法可以摆脱height: _expanded ? 150 : 0,。像在普通容器中一样,高度将自动从其子容器中获取如果我们没有设置高度属性
    猜你喜欢
    • 2011-12-21
    • 2019-05-15
    • 2012-04-04
    • 1970-01-01
    • 2019-02-20
    • 1970-01-01
    • 2019-09-10
    • 1970-01-01
    • 2012-12-28
    相关资源
    最近更新 更多