Skip to content
Related Articles

Related Articles

Improve Article
Flutter – Animation in Route Transition
  • Last Updated : 16 Oct, 2020

Routes are simply Pages in Flutter applications. An application often needs to move from one page to another. But to make these transition smoother, Animations can be used. These animations can be used to curve or tween the Animation object of the PageRouteBuilder class to alter the transition animation. We will discuss them in detail here.

Let’s just build a simple app to better understand the concept. Follow the below steps to do so:

  • Add the PageRouteBuilder.
  • Add a tween.
  • Create an AnimatedWidget
  • Create a CurveTween.
  • Combine both tweens

Let’s discuss each step in detail:

Adding the PageRouteBuilder:

By using the PageRouteBuild create two routes with the first route as the “Homepage” with a button “Goto page 2” and the second route with just an empty page named “Page 2”, using the below code:

Dart






import 'package:flutter/material.dart';
  
main() {
  runApp(MaterialApp(
    home: Page1(),
  ));
}
  
class Page1 extends StatelessWidget {
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(),
      body: Center(
        child: RaisedButton(
          child: Text('Go to Page 2'),
          onPressed: () {
            Navigator.of(context).push(_createRoute());
          },
        ),
      ),
    );
  }
}
  
Route _createRoute() {
  return PageRouteBuilder(
    pageBuilder: (context, animation, secondaryAnimation) => Page2(),
    transitionsBuilder: (context, animation, secondaryAnimation, child) {
      return child;
    },
  );
}
  
class Page2 extends StatelessWidget {
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(),
      body: Center(
        child: Text('Page 2'),
      ),
    );
  }
}

Adding a tween:

You can build a tween of an animation object by using the below code :

Dart




transitionsBuilder: (context, animation, secondaryAnimation, child) {
  var begin = Offset(0.0, 1.0);
  var end = Offset.zero;
  var tween = Tween(begin: begin, end: end);
  var offsetAnimation = animation.drive(tween);
  return child;
},

Creating an AnimatedWidget:

The AnimatedWidget has the property of its state once the animation value changes. You can create one like below:

Dart




transitionsBuilder: (context, animation, secondaryAnimation, child) {
  var begin = Offset(0.0, 1.0);
  var end = Offset.zero;
  var tween = Tween(begin: begin, end: end);
  var offsetAnimation = animation.drive(tween);
  
  return SlideTransition(
    position: offsetAnimation,
    child: child,
  );
},

Creating a CurveTween:

Use the below code to create a CurveTween:

Dart




var curve = Curves.ease;
  
var curveTween = CurveTween(curve: curve);

Combining both Tweens:

Use the chain() method to combine two tweens as shown below:

Dart




var begin = Offset(0.0, 1.0);
var end = Offset.zero;
var curve = Curves.ease;
  
var tween = Tween(begin: begin, end: end).chain(CurveTween(curve: curve));

Now use the animation.drive() method to create an Animation object of the combined tweens as shown below:

Dart




return SlideTransition(
  position: animation.drive(tween),
  child: child,
);

Complete Source Code:

Dart




import 'package:flutter/material.dart';
  
main() {
  runApp(MaterialApp(
    home: Homepage(),
  ));
}
  
class Homepage extends StatelessWidget {
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title : Text('GeeksForGeeks'),
        backgroundColor: Colors.green,
      ),
  
      body: Center(
        child: RaisedButton(
          child: Text('Go to Page 2'),
          onPressed: () {
            Navigator.of(context).push(_createRoute());
          },
        ),
      ),
    );
  }
}
  
Route _createRoute() {
  return PageRouteBuilder(
    pageBuilder: (context, animation, secondaryAnimation) => Page2(),
    transitionsBuilder: (context, animation, secondaryAnimation, child) {
      var begin = Offset(0.0, 1.0);
      var end = Offset.zero;
      var curve = Curves.ease;
  
      var tween = Tween(begin: begin, end: end).chain(CurveTween(curve: curve));
  
      return SlideTransition(
        position: animation.drive(tween),
        child: child,
      );
    },
  );
}
  
class Page2 extends StatelessWidget {
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(),
      body: Center(
        child: Text('Page 2'),
      ),
    );
  }
}

Output:

Want a more fast-paced & competitive environment to learn the fundamentals of Android?
Click here to head to a guide uniquely curated by our experts with the aim to make you industry ready in no time!



My Personal Notes arrow_drop_up
Recommended Articles
Page :