Flutter Animation in Route Transition (original) (raw)
Last Updated : 09 Apr, 2025
**Routes are simply Pages in Flutter applications. An application often needs to move from one page to another. However, animations can be used to make these transitions smoother. 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.
Steps to Implement Animation in Route Transition
Step 1 : Adding the PageRouteBuilder
Using the PageRouteBuilder, 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 code below.
**main.dart:
Dart `
import 'package:flutter/material.dart';
void main() { runApp(const MaterialApp( home: Page1(), )); }
class Page1 extends StatelessWidget { const Page1({Key? key}) : super(key: key); @override Widget build(BuildContext context) { return Scaffold( appBar: AppBar(), body: Center( child: ElevatedButton( onPressed: () { Navigator.of(context).push(_createRoute()); }, child: const Text('Go to Page 2'), ) ), ); } }
Route _createRoute() { return PageRouteBuilder( pageBuilder: (context, animation, secondaryAnimation) => const Page2(), transitionsBuilder: (context, animation, secondaryAnimation, child) { return child; }, ); }
class Page2 extends StatelessWidget { const Page2({Key? key}) : super(key: key); @override Widget build(BuildContext context) { return Scaffold( appBar: AppBar(), body: const Center( child: Text('Page 2'), ), ); } }
`
Step 2 : Adding a tween
You can build a tween of an animation object by using the below code :
**main.dart:
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; },
`
Step 3 : 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, ); },
`
Step 4 : Creating a CurveTween
Use the below code to create a CurveTween:
Dart `
var curve = Curves.ease; var curveTween = CurveTween(curve: curve);
`
Step 5 : 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));
`
No, we 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
**main.dart:
Dart `
import 'package:flutter/material.dart';
void main() { runApp(const MaterialApp( debugShowCheckedModeBanner: false, home: Homepage(), )); }
class Homepage extends StatelessWidget { const Homepage({Key? key}) : super(key: key);
@override Widget build(BuildContext context) { return Scaffold( appBar: AppBar( title: const Text('GeeksForGeeks'), backgroundColor: Colors.green, foregroundColor: Colors.white, ), body: Center( child: ElevatedButton( style: ElevatedButton.styleFrom( backgroundColor: Colors.green, ), onPressed: () { // Navigate to Page 2 with a custom transition Navigator.of(context).push(_createRoute()); }, child: const Text( 'Go to Page 2', style: TextStyle( color: Colors.white, ), ), ), ), ); } }
// Function to create a custom route with a slide transition Route _createRoute() { return PageRouteBuilder( pageBuilder: (context, animation, secondaryAnimation) => const Page2(), transitionsBuilder: (context, animation, secondaryAnimation, child) { var begin = const 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 { const Page2({Key? key}) : super(key: key);
@override Widget build(BuildContext context) { return Scaffold( appBar: AppBar( backgroundColor: Colors.blue, foregroundColor: Colors.white, ), body: const Center( child: Text('Page 2'), ), ); } }
`
**Output: