Animate a Page Route Transition in Flutter

Flutter Dev Agency
2 min readAug 13, 2023

--

In Flutter, creating smooth and visually appealing page route transitions can greatly enhance the overall user experience. In this blog, we will explore how to animate a page route transition in Flutter using the built-in animation framework and provide an example code to illustrate the process.

Step 1: Setting up the Flutter project
To get started, make sure you have Flutter installed on your machine. Create a new Flutter project using the following command:

flutter create page_transition_demo

Step 2: Create the destination page
In this example, we will create a simple destination page called ‘SecondPage’. Open the ‘lib/main.dart’ file and replace the default ‘MyApp’ class with the following code:

import ‘package:flutter/material.dart’;

void main() => runApp(MyApp());

class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
title: ‘Page Transition Demo’,
home: FirstPage(),
);
}
}

class FirstPage extends StatelessWidget {
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(

title: Text(‘First Page’),
),
body: Center(
child: RaisedButton(
onPressed: () {
Navigator.push(
context,
MaterialPageRoute(builder: (context) => SecondPage()),
);
},
child: Text(‘Go to Second Page’),
),
),
);
}
}

class SecondPage extends StatelessWidget {
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text(‘Second Page’),
),
body: Center(
child: Text(‘Welcome to the Second Page!’),
),
);
}
}

Step 3: Add animation to the page transition
To animate the page route transition, we will use the `PageRouteBuilder` class and wrap the destination page with a `SlideTransition` widget. Modify the `onPressed` method in the `FirstPage` class as follows:

onPressed: () {
Navigator.push(
context,
PageRouteBuilder(
pageBuilder: (context, animation, secondaryAnimation) => SecondPage(),
transitionsBuilder: (context, animation, secondaryAnimation, child) {
var begin = Offset(1.0, 0.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,
);
},
),
);
},

Step 4: Run the app
Save the changes and run the app using the following command:

flutter run

You should now see the first page with a button. When you tap the button, the second page will slide in from the right, creating a smooth animation.

Conclusion:
Animating page route transitions in Flutter can greatly enhance the visual experience and make your app more engaging. By utilising the `PageRouteBuilder` and `SlideTransition` widgets, you can create custom and dynamic page transitions. Experiment with different animations and transitions to create stunning effects in your Flutter applications.

Remember to import the necessary packages and explore other animation options available in the Flutter framework. Happy coding!

Note: This example assumes you have a basic understanding of Flutter and Dart programming.

--

--

Flutter Dev Agency

We specialize in Flutter app development services. With our expertise in cross-platform development, UI/UX design, and backend integration.