Flutter – Page Transition Animation
In Flutter, we can easily animate or handle the page transitions by using the page_transition package. The page_transition package is a valuable addition to the Flutter package, offering a variety of transition effects that can elevate the app’s UI. In this article, we are going to explore how to integrate and implement the page_transition package to animate page transitions in a Flutter application by using the page_transition package. A sample video is given below to get an idea about what we are going to do in this article.
Step By Step Implementation
Step 1: Create a New Project in Android Studio
To set up Flutter Development on Android Studio please refer to Android Studio Setup for Flutter Development, and then create a new project in Android Studio please refer to Creating a Simple Application in Flutter.
Step 2: Adding the Dependencies
Here we have to add the the following dependencies to our pubspec.yaml file.
dependencies:
page_transition: ^2.1.0
Or, Simply you can run the below command in your VS code Terminal.
flutter pub add page_transition
Step 3: Import the Package
First of all import material.dart package and the page_transition package.
import 'package:flutter/material.dart';
import 'package:page_transition/page_transition.dart';
Step 4: Execute the main Method
Here the execution of our app starts.
Dart
void main() => runApp(MyApp()); |
Step 5: Create MyApp Class
In this class we are going to implement the MaterialApp , here we are also set the Theme of our App and defines the Routes.
Dart
class MyApp extends StatelessWidget { @override Widget build(BuildContext context) { return MaterialApp( debugShowCheckedModeBanner: false , title: 'Page Transition Example' , theme: ThemeData( primarySwatch: Colors.green, ), home: MyHomePage(), // Custom route generator for named routes onGenerateRoute: (settings) { switch (settings.name) { case '/second' : return PageTransition( child: SecondPage(), type: PageTransitionType.theme, settings: settings, reverseDuration: Duration(seconds: 3), ); default : return null; } }, ); } } |
Step 6: Create MyHomePage Class
The MyHomePage class represents the main screen of the app. It extends the StatelessWidget class. The body of the screen is centered and contains a ListView widget with a series of ElevatedButton widgets. Each button is associated with a specific page transition effect, such as fade, left-to-right, right-to-left (with iOS-style swipe-back), left-to-right with fade, and a unique right-to-left pop transition. When users tap these buttons, the corresponding page transitions are triggered using the Navigator.push method, navigating to the SecondPage class or, in the case of the right-to-left pop transition, returning to the current home page. Comments are added for better understanding.
Different animation of page transition:
// Button for triggering Fade Transition
ElevatedButton(
child: Text('Fade Transition'),
onPressed: () {
Navigator.push(
context,
PageTransition(
type: PageTransitionType.fade,
child: SecondPage(),
),
);
},
),
SizedBox(height: 16),
// Button for triggering Left To Right Transition
ElevatedButton(
child: Text('Left To Right Transition'),
onPressed: () {
Navigator.push(
context,
PageTransition(
type: PageTransitionType.leftToRight,
child: SecondPage(),
),
);
},
),
SizedBox(height: 16),
// Button for triggering Right To Left Transition (iOS SwipeBack)
ElevatedButton(
child: Text('Right To Left Transition (iOS SwipeBack)'),
onPressed: () {
Navigator.push(
context,
PageTransition(
type: PageTransitionType.rightToLeft,
isIos: true,
child: SecondPage(),
),
);
},
),
SizedBox(height: 16),
// Button for triggering Left To Right with Fade Transition
ElevatedButton(
child: Text('Left To Right with Fade Transition'),
onPressed: () {
Navigator.push(
context,
PageTransition(
type: PageTransitionType.leftToRightWithFade,
alignment: Alignment.topCenter,
child: SecondPage(),
),
);
},
),
SizedBox(height: 16),
// Button for triggering Right To Left Pop Transition
ElevatedButton(
child: Text('Right To Left Pop Transition'),
onPressed: () {
Navigator.push(
context,
PageTransition(
alignment: Alignment.bottomCenter,
curve: Curves.easeInOut,
duration: Duration(milliseconds: 600),
reverseDuration: Duration(milliseconds: 600),
type: PageTransitionType.rightToLeftPop,
child: SecondPage(),
childCurrent: MyHomePage(),
),
);
},
),
Dart
class MyHomePage extends StatelessWidget { @override Widget build(BuildContext context) { return Scaffold( appBar: AppBar( title: Text( 'Page Transition Example' ), ), body: Center( child: ListView( children: <Widget>[ // Button for triggering Fade Transition ElevatedButton( child: Text( 'Fade Transition' ), onPressed: () { Navigator.push( context, PageTransition( type: PageTransitionType.fade, child: SecondPage(), ), ); }, ), SizedBox(height: 16), // Button for triggering Left To Right Transition ElevatedButton( child: Text( 'Left To Right Transition' ), onPressed: () { Navigator.push( context, PageTransition( type: PageTransitionType.leftToRight, child: SecondPage(), ), ); }, ), SizedBox(height: 16), // Button for triggering Right To Left Transition (iOS SwipeBack) ElevatedButton( child: Text( 'Right To Left Transition (iOS SwipeBack)' ), onPressed: () { Navigator.push( context, PageTransition( type: PageTransitionType.rightToLeft, isIos: true , child: SecondPage(), ), ); }, ), SizedBox(height: 16), // Button for triggering Left To Right with Fade Transition ElevatedButton( child: Text( 'Left To Right with Fade Transition' ), onPressed: () { Navigator.push( context, PageTransition( type: PageTransitionType.leftToRightWithFade, alignment: Alignment.topCenter, child: SecondPage(), ), ); }, ), SizedBox(height: 16), // Button for triggering Right To Left Pop Transition ElevatedButton( child: Text( 'Right To Left Pop Transition' ), onPressed: () { Navigator.push( context, PageTransition( alignment: Alignment.bottomCenter, curve: Curves.easeInOut, duration: Duration(milliseconds: 600), reverseDuration: Duration(milliseconds: 600), type: PageTransitionType.rightToLeftPop, child: SecondPage(), childCurrent: MyHomePage(), ), ); }, ), ], ), ), ); } } |
Step 7: Create SecondPage Class
It is the simple page in which the the user will come after clicking one of the button to start animated page transition. The page transition from the MyHomePage to SecondPage will done.
Dart
class SecondPage extends StatelessWidget { @override Widget build(BuildContext context) { return Scaffold( appBar: AppBar( title: Text( 'Second Page' ), ), body: Center( child: Column( mainAxisAlignment: MainAxisAlignment.center, crossAxisAlignment: CrossAxisAlignment.center, children: [ Text( 'This is the second page.' ), ], ), ), ); } } |
Here is the full Code of main.dart file
Dart
import 'package:flutter/material.dart' ; import 'package:page_transition/page_transition.dart' ; void main() => runApp(MyApp()); class MyApp extends StatelessWidget { @override Widget build(BuildContext context) { return MaterialApp( debugShowCheckedModeBanner: false , title: 'Page Transition Example' , theme: ThemeData( primarySwatch: Colors.green, ), home: MyHomePage(), // Custom route generator for named routes onGenerateRoute: (settings) { switch (settings.name) { case '/second' : return PageTransition( child: SecondPage(), type: PageTransitionType.theme, settings: settings, reverseDuration: Duration(seconds: 3), ); default : return null; } }, ); } } class MyHomePage extends StatelessWidget { @override Widget build(BuildContext context) { return Scaffold( appBar: AppBar( title: Text( 'Page Transition Example' ), ), body: Center( child: ListView( children: <Widget>[ // Button for triggering Fade Transition ElevatedButton( child: Text( 'Fade Transition' ), onPressed: () { Navigator.push( context, PageTransition( type: PageTransitionType.fade, child: SecondPage(), ), ); }, ), SizedBox(height: 16), // Button for triggering Left To Right Transition ElevatedButton( child: Text( 'Left To Right Transition' ), onPressed: () { Navigator.push( context, PageTransition( type: PageTransitionType.leftToRight, child: SecondPage(), ), ); }, ), SizedBox(height: 16), // Button for triggering Right To Left Transition (iOS SwipeBack) ElevatedButton( child: Text( 'Right To Left Transition (iOS SwipeBack)' ), onPressed: () { Navigator.push( context, PageTransition( type: PageTransitionType.rightToLeft, isIos: true , child: SecondPage(), ), ); }, ), SizedBox(height: 16), // Button for triggering Left To Right with Fade Transition ElevatedButton( child: Text( 'Left To Right with Fade Transition' ), onPressed: () { Navigator.push( context, PageTransition( type: PageTransitionType.leftToRightWithFade, alignment: Alignment.topCenter, child: SecondPage(), ), ); }, ), SizedBox(height: 16), // Button for triggering Right To Left Pop Transition ElevatedButton( child: Text( 'Right To Left Pop Transition' ), onPressed: () { Navigator.push( context, PageTransition( alignment: Alignment.bottomCenter, curve: Curves.easeInOut, duration: Duration(milliseconds: 600), reverseDuration: Duration(milliseconds: 600), type: PageTransitionType.rightToLeftPop, child: SecondPage(), childCurrent: MyHomePage(), ), ); }, ), ], ), ), ); } } class SecondPage extends StatelessWidget { @override Widget build(BuildContext context) { return Scaffold( appBar: AppBar( title: Text( 'Second Page' ), ), body: Center( child: Column( mainAxisAlignment: MainAxisAlignment.center, crossAxisAlignment: CrossAxisAlignment.center, children: [ Text( 'This is the second page.' ), ], ), ), ); } } |
Contact Us