Flutter – Rotate Transition

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.

To build a simple application depicting the use of page_transition package for a Rotate transition follow the below steps:

  • Add page_transition to the dependencies in pubspec.yaml file
  • Import the same dependency in the main.dart file
  • Use a StatelessWidget to give a structure to the application
  • Design the homepage
  • Define the onGenerateRoute property in the MaterialApp widget to transition to from the home page

Now, let’s look in the steps in detail:

Adding the Dependency:

You can add the page_transition dependency to the pubspec.yaml file as follows:

dependency



Importing the Dependency:

To import the dependency to your main.dart file use the following:

import 'package:page_transition/page_transition.dart';

Desigining 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

filter_none

edit
close

play_arrow

link
brightness_4
code

class MyApp extends StatelessWidget {
  
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      debugShowCheckedModeBanner: false,
      title: 'GeeksForGeeks',
      theme: ThemeData(
        primarySwatch: Colors.blue,
      ),
}

chevron_right


Designing the Homepage:

A StatelessWidget can also be used to design the Hopepage 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

filter_none

edit
close

play_arrow

link
brightness_4
code

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: <Widget>[
            RaisedButton(
              child: Text('Rotate Transition Button'),
              onPressed: () {
                Navigator.push(
                  context,
                  PageTransition(
                    curve: Curves.bounceOut,
                    type: PageTransitionType.rotate,
                    alignment: Alignment.topCenter,
                    child: SecondPage(),
                  ),
                );
              },
            ),
          ],
        ),
      ),
    );
  }
}

chevron_right


Defining the OnRouteSetting property:

The onRoyeSettings 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

filter_none

edit
close

play_arrow

link
brightness_4
code

onGenerateRoute: (settings) {
        switch (settings.name) {
          case '/second':
            return PageTransition(
              child: SecondPage(
              ),
              type: PageTransitionType.fade,
              settings: settings,
            );

chevron_right


Complete Source Code:

Dart

filter_none

edit
close

play_arrow

link
brightness_4
code

import 'package:flutter/material.dart';
import 'package:page_transition/page_transition.dart';
  
void main() => runApp(MyApp());
  
class MyApp extends StatelessWidget {
  // root of 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(
              ),
              type: PageTransitionType.fade,
              settings: settings,
            );
            break;
          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,
      ),
      body: Center(
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          crossAxisAlignment: CrossAxisAlignment.center,
          children: <Widget>[
            RaisedButton(
              child: Text('Rotate Transition Button'),
              onPressed: () {
                Navigator.push(
                  context,
                  PageTransition(
                    curve: Curves.bounceOut,
                    type: PageTransitionType.rotate,
                    alignment: Alignment.topCenter,
                    child: SecondPage(),
                  ),
                );
              },
            ),
          ],
        ),
      ),
    );
  }
}
  
//defination of second page
class SecondPage extends StatelessWidget {
  
  final String title;
  
  /// constructor of the page
  const SecondPage({Key key, this.title}) : super(key: key);
  @override
  Widget build(BuildContext context) {
    final args = ModalRoute.of(context).settings.arguments;
    return Scaffold(
      appBar: AppBar(
        title: Text(args ?? "Page Transition Plugin"),
      ),
      body: Center(
        child: Text('Second Page'),
      ),
    );
  }
}

chevron_right


Output:




My Personal Notes arrow_drop_up


If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.


Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.