flutter AnimatedContainer可以动画显示其高度吗?

zvms9eto  于 2023-02-13  发布在  Flutter
关注(0)|答案(2)|浏览(123)

我想在一个列表中的两个项目之间动画一个间隙。我想使用一个初始高度为零的AminatedContainer,但我不熟悉如何使其工作。目前我的代码是:

new AnimatedContainer(
      duration: const Duration(milliseconds: 200),
      height: App.itemSelected==id ? 50.0 : 0.0,
      curve: Curves.fastOutSlowIn,
    ),

这确实改变了容器的高度,但不是像我希望的那样以动画的方式。任何帮助都将感激不尽!

lzfw57am

lzfw57am1#

我不确定AnimatedSize是否适合您的用例,但我添加了一个如何使用它制作简单动画的示例:
着色是有点小康,由于录音,但你应该能够测试这一点自己。

class MyAppState extends State<MyApp> with TickerProviderStateMixin {
  double _height = 50.0;
  double _width = 20.0;
  var _color = Colors.blue;

  @override
  Widget build(BuildContext context) {
    return new Scaffold(
        body: new Center(
          child: new Column(
            mainAxisAlignment: MainAxisAlignment.center,
            children: <Widget>[
              new AnimatedSize(

                curve: Curves.fastOutSlowIn, child: new Container(
                width: _width,
                height: _height,
                color: _color,
              ), vsync: this, duration: new Duration(seconds: 2),),
              new Divider(height: 35.0,),
              new Row(
                mainAxisAlignment: MainAxisAlignment.center,
                children: <Widget>[
                  new IconButton(
                      icon: new Icon(Icons.arrow_upward, color: Colors.green,),
                      onPressed: () =>
                          setState(() {
                            _color = Colors.green;
                            _height = 95.0;
                          })),
                  new IconButton(
                      icon: new Icon(Icons.arrow_forward, color: Colors.red,),
                      onPressed: () =>
                          setState(() {
                            _color = Colors.red;
                            _width = 45.0;
                          })),
                ],
              )
            ],)
          ,)
    );
  }
}

相关问题