Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Flutter - Animate change on height when child of container renders

I'm trying to recreate something like ExpansionTile but in a Card. When I click the card, its child renders and the card changes its height, so I want to animate that change.

I tried using AnimatedContainer and GlobalKey to know the final size of the card with its child rendered and then set the new height to AnimatedContainer but that didn't work.

like image 685
Diego Francisco Avatar asked Dec 13 '18 18:12

Diego Francisco


People also ask

How do you animate container height in Flutter?

Start the animation by rebuilding with new propertiesUse the setState() method. Add a button to the app. When the user taps the button, update the properties with a new width, height, background color and border radius inside a call to setState() .

How do you control animation in Flutter?

Work flow of the Flutter AnimationAnimationController(duration: const Duration(seconds: 2), vsync: this); animation = Tween<double>(begin: 0, end: 300). animate(controller); controller. forward(); Add animation based listener, addListener to change the state of the widget.


2 Answers

In the end I just had to use AnimatedSize. It replicates exactly the animation that I want.

AnimatedSize(   vsync: this,   duration: Duration(milliseconds: 150),   curve: Curves.fastOutSlowIn,   child: Container(     child: Container(       child: !_isExpanded           ? null           : FadeTransition(opacity: animationFade, child: widget.child),     ),   ), ); 
like image 142
Diego Francisco Avatar answered Sep 20 '22 05:09

Diego Francisco


You can use the AnimatedContainer for animations

class Animate extends StatefulWidget {   @override   _AnimateState createState() => _AnimateState(); }  class _AnimateState extends State<Animate> {   var height = 200.0;    @override   Widget build(BuildContext context) {     var size = MediaQuery.of(context).size;     return Scaffold(       body: Center(         child: AnimatedContainer(           color: Colors.amber,           duration: new Duration(milliseconds: 500),           height: height,         ),       ),       floatingActionButton: FloatingActionButton(         onPressed: () {           setState(() {             if (height == 200.0) {               height = 400.0;             } else {               height = 200.0;             }           });         },         child: Icon(Icons.settings),       ),     );   } } 
like image 21
Lakhwinder Singh Avatar answered Sep 24 '22 05:09

Lakhwinder Singh