Flutter Rotate Transition (original) (raw)
Last Updated : 04 Mar, 2025
In Flutter, the **page_transition package is used to create beautiful page transitions. It provides a wide range of effects that can be used from moving from one route to another. It is very convenient to use. In this article, we will explore the same by building a simple application.
Steps to Implement rotate transition in Flutter
Step 1 : Adding the Dependency
You can add the _page_transition dependency to the pubspec.yaml file as follows:
dependencies:
page_transition: ^2.2.1
Now run the below command in terminal.
flutter pub get
Step 2 : Importing the Dependency
To import the dependency to your main.dart file use the following:
import 'package:page_transition/page_transition.dart';
Step 3 : Designing the App Structure
The **StatelessWidget can be used to give a simple structure to the application that contains an appbar and a body for the content as shown below:
Dart `
class MyApp extends StatelessWidget {
@override Widget build(BuildContext context) { return MaterialApp( debugShowCheckedModeBanner: false, title: 'GeeksForGeeks', theme: ThemeData( primarySwatch: Colors.blue, ), }
`
Step 4 : Designing the Homepage
A StatelessWidget can also be used to design the Homepage for the app. A button will also be added to the homepage which will have a transition action attached to it when pressed as shown below:
Dart `
class MyHomePage extends StatelessWidget { @override Widget build(BuildContext context) { return Scaffold( backgroundColor: Colors.greenAccent, appBar: AppBar( title: Text('GeeksForGeeks'), backgroundColor: Colors.green, ), body: Center( child: Column( mainAxisAlignment: MainAxisAlignment.center, crossAxisAlignment: CrossAxisAlignment.center, children: [ TextButton( child: Text('Rotate Transition Button'), onPressed: () { Navigator.push( context, PageTransition( curve: Curves.bounceOut, type: PageTransitionType.rotate, alignment: Alignment.topCenter, child: SecondPage(), ), ); }, ), ], ), ), ); } }
`
To know more about Navigator refer this article: Flutter – Navigate From One Screen to Another
Step 5 : Defining the OnRouteSetting property
The onRouteSettings property is used to extract information from one page and send it to another page (or, route). We will assign the same property to the button action that we added in the homepage that will transition it to the second page as shown below:
Dart `
onGenerateRoute: (settings) { switch (settings.name) { case '/second': return PageTransition( child: SecondPage( ), type: PageTransitionType.fade, settings: settings, );
`
**Complete Source Code (main.dart)
main.dart `
import 'package:flutter/material.dart'; import 'package:page_transition/page_transition.dart';
void main() => runApp(MyApp());
class MyApp extends StatelessWidget { // Root of the application @override Widget build(BuildContext context) { return MaterialApp( debugShowCheckedModeBanner: false, title: 'GeeksForGeeks', theme: ThemeData( primarySwatch: Colors.blue, ), home: MyHomePage(), onGenerateRoute: (settings) { switch (settings.name) { case '/second': return PageTransition( child: SecondPage( title: '', ), type: PageTransitionType.fade, settings: settings, ); default: return null; } }, ); } }
/// Homepage class MyHomePage extends StatelessWidget { @override Widget build(BuildContext context) { return Scaffold( backgroundColor: Colors.greenAccent, appBar: AppBar( title: Text('GeeksForGeeks'), backgroundColor: Colors.green, foregroundColor: Colors.white, ), body: Center( child: Column( mainAxisAlignment: MainAxisAlignment.center, crossAxisAlignment: CrossAxisAlignment.center, children: [ TextButton( child: Text('Rotate Transition Button'), onPressed: () { Navigator.push( context, PageTransition( curve: Curves.bounceOut, type: PageTransitionType.rotate, alignment: Alignment.topCenter, child: SecondPage( title: '', ), ), ); }, ), ], ), ), ); } }
// Definition of second page class SecondPage extends StatelessWidget { final String title;
/// Constructor of the page
const SecondPage({super.key, required this.title});
@override
Widget build(BuildContext context) {
final args = ModalRoute.of(context)!.settings.arguments;
return Scaffold(
appBar: AppBar(
title: Text(args as String? ?? "Page Transition Plugin"),
backgroundColor: Colors.blue,
foregroundColor: Colors.white,
),
body: Center(
child: Text('Second Page'),
),
);
}
}
`
**Output: