Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can an AnimatedContainer animate its height?

Tags:

flutter

dart

I'd like to animate a gap between two items in a list. I thought of using an AminatedContainer with a height initially at zero but I'm not familiar with how to make this work. My code at the moment is:

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

That does change the height of the Container but not in an animated way as I had hoped. Any help would be gratefully received!

like image 834
iBob101 Avatar asked Oct 07 '17 21:10

iBob101


People also ask

How do you use AnimatedBuilder?

AnimatedBuilder is useful for more complex widgets that wish to include an animation as part of a larger build function. To use AnimatedBuilder, simply construct the widget and pass it a builder function. For simple cases without additional state, consider using AnimatedWidget.

How do you animate widgets in flutter?

Flutter made it easy with its packages. To animate the widgets without much effort, we can wrap them inside different defined animated widgets in the animate_do package. In this article, we will see how with the animate_do package we can animate widgets easily and enhance the user experience. In the pubspec.


2 Answers

You can use an AnimatedSize for that purpose.

https://docs.flutter.io/flutter/widgets/AnimatedSize-class.html

like image 29
Rainer Wittmann Avatar answered Oct 14 '22 05:10

Rainer Wittmann


I am not sure if AnimatedSize is suitable for your use case, but I have added an example on how to make a simple animation with it:

The coloring is a bit off due to the recording but you should be able to test this yourself.

enter image description here

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;
                          })),
                ],
              )
            ],)
          ,)
    );
  }
}
like image 188
Shady Aziza Avatar answered Oct 14 '22 04:10

Shady Aziza