I have made simple hero animation following instructions from Flutter's website
It works as described in the instructions but in my case, I would like it to animate much more slowly from the first to the second screen. do anyone know how to change the speed of this animation?
The hero refers to the widget that flies between screens. Create a hero animation using Flutter's Hero widget. Fly the hero from one screen to another. Animate the transformation of a hero's shape from circular to rectangular while flying it from one screen to another.
What is vsync ? Vsync basically keeps the track of screen, so that Flutter does not renders the animation when the screen is not being displayed.
To modify the transition speed, you'll have to adjust the PageRoute transition duration (as already pointed out by @diegoveloper).
If you wanna keep the default transition, you can create a class implementing MaterialPageRoute. If you already have your own transition or want to create one you can use the PageRouteBuilder to easily build your own. Simply adjust the transitionDuration
.
Here's a small standalone example, using the PageRouteBuilder
:
import 'package:flutter/material.dart'; void main() => runApp(MyApp()); class MyApp extends StatelessWidget { @override Widget build(BuildContext context) { return MaterialApp( home: Page1(), ); } } class Page1 extends StatelessWidget { @override Widget build(BuildContext context) { return Scaffold( body: Center( child: Column( mainAxisAlignment: MainAxisAlignment.spaceAround, children: <Widget>[ RaisedButton( child: Text('Page2'), onPressed: () => Navigator.push( context, PageRouteBuilder( transitionDuration: Duration(seconds: 2), pageBuilder: (_, __, ___) => Page2())), ), Hero(tag: 'home', child: Icon(Icons.home)) ], ), ), ); } } class Page2 extends StatelessWidget { @override Widget build(BuildContext context) { return Scaffold( body: Center( child: Hero( tag: 'home', child: Icon( Icons.home, ), ), ), ); } }
I don't think the only way of achieving this is through changing the PageRoute
transition duration. I think you could also achieve the same effect by using an AnimationController
— this answer comes largely from lectures #149 and #150 of Angela Yu's The Complete 2019 Flutter Development Bootcamp with Dart.
StatefulWidget
.with SingleTickerProviderStateMixin
to your state class.controller
inside the initState
method. duration
, so you can change it to your liking.In the end, everything should look a bit like this:
class _NewScreenState extends State<HomeScreen> with SingleTickerProviderStateMixin{ AnimationController controller; @override void initState() { super.initState(); controller = AnimationController( duration: Duration(seconds: 1), vsync: this, ); controller.forward(); controller.addListener((){ setState(() { }); }); } @override Widget build(BuildContext context) { return ...
vsync
is a required (@required
) parameter that takes the state (instance) object itself (usually).addListener
and setState
are there if you wish to use the value of the controller
(controller.value
) at some point in the future — for example, changing the height of the icon with something like height: finalHeight * controller.value
.FlatButton
with Navigator.pushNamed
, nothing special.controller
will still be active even if you change screens later. So if you have a looping animation in the background, it's a good idea to dispose it when changing the screen, this way you don't waste phone resources with it anymore. This could be achieved with: @override void dispose() { controller.dispose(); super.dispose(); }
CurvedAnimation
. Animation animation;
right below your controller
.controller
, inside initState
, add: animation = CurvedAnimation( // the controller can't have upperBound > 1 parent: controller, // the controller you created curve: Curves.decelerate, );
Flutter
is by using TweenAnimations. For example, if you want to transition between colors, you could use ColorTween
(below your controller
, inside initState
): animation = ColorTween( begin: Colors.red, end: Colors.blue, ).animate(controller);
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