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.
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() .
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.
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), ), ), );
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), ), ); } }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With