Skip to main content

Flutter : Screen Animation tutorial

In this tutorial, we will learn how to perform animations while navigating from one screen to another. We will not require any other dependency to be installed. We will demonstrate the following example.



So let's start.

Approach

Instead of using the default MaterialPageRoute, we will use the PageRouteBuilder. PageRouteBuilder provides all the functionalities like duration, type of animation.

Syntax

The syntax for PageRouteBuilder is as follows.
PageRouteBuilder(
  transitionDuration: const Duration(seconds: 1),
  pageBuilder: (context, animation, secondaryAnimation) {
    return const SecondScreen();
  },
),
The parameters and fields are self-explanatory.

Code

Let us code our First Screen of the app.
class FirstScreen extends StatelessWidget {
  const FirstScreen({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: const Text("First Screen"),
      ),
      body: Center(
        child: Column(
          children: <Widget>[
            const Text("First Screen"),
            ElevatedButton(
              onPressed: () {
                Navigator.push(
                  context,
                  PageRouteBuilder(
                    transitionDuration: const Duration(seconds: 1),
                    transitionsBuilder:
                        (context, animation, secondaryAnimation, child) {
                      const begin = Offset(0, -1);
                      const end = Offset.zero;
                      final tween = Tween(begin: begin, end: end);
                      return SlideTransition(
                        position: animation.drive(tween),
                        child: child,
                      );
                    },
                    pageBuilder: (context, animation, secondaryAnimation) {
                      return const SecondScreen();
                    },
                  ),
                );
              },
              child: const Text("Second Screen"),
            )
          ],
        ),
      ),
    );
  }
}

We have used another field transitionBuilder. Here we will tween the animation will navigating from the current screen to the next screen and vice versa.
We have used the SlideTransition widget to give a sliding animation effect. There are many other animations presets. We can also create our own custom animations.
The Second Screen code is very simple.
class SecondScreen extends StatelessWidget {
  const SecondScreen({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: const Text("Second Screen"),
      ),
      body: const Center(
        child: Text("Second Screen"),
      ),
    );
  }
}

Run the code and see the result.




We can also use different curved animations. Replace the transitionBuilder code with the following.
transitionsBuilder:
    (context, animation, secondaryAnimation, child) {
  const begin = Offset(0, -1);
  const end = Offset.zero;
  final tween = Tween(begin: begin, end: end);
  final curvedAnimation = CurvedAnimation(
      curve: Curves.easeIn, parent: animation);
  return SlideTransition(
    position: tween.animate(curvedAnimation),
    child: child,
  );
},

Run the code again.




Thank you. If you have any doubt comment below. Learn more flutter tutorials from the below links.




Comments

  1. I generally want quality content and I found that in your post. The information you have shared about 3d animation advertisement is beneficial and significant for us. Keep sharing these kinds of articles here. Thank you.

    ReplyDelete

Post a Comment

If you have any problem or doubt please comment.

Popular posts from this blog

Flutter: DatePicker Tutorial both with Material and Cupertino Style

Introduction DatePicker is very important when you want the user to pick his / her date of birth or something else. In Flutter, implementing DatePicker is very easy and we will implement DatePicker in both Android or Material style and Cupertino or IOS style.  Table of contents Approach Project Setup Material Style DatePicker Cupertino Style DatePicker Conclusion Approach Flutter has widgets for everything and even for DatePicker. DatePicker widget is loaded with all needed animation and colours. So implementation is very easy.  Project Setup No extra plugins are required for this project and you can continue with your existing project. Here is the starting code. class DatePickerTutorial extends StatefulWidget { const DatePickerTutorial({Key? key}) : super (key: key); @ override _DatePickerTutorialState createState() => _DatePickerTutorialState(); } class _DatePickerTutorialState extends State...

Flutter: DraggableScrollableSheet widget Tutorial

Introduction In this tutorial, we are going to learn about the DraggableScrollableSheet widget. It is a widget that responds to user gestures. When the user drags from the bottom of the screen in the upward direction, the widget opens up and when scrolls down the widget close. The final app will look as follows  Final App DraggableScrollableSheet It is a widget that scrolls up and down of the viewport or screen according to the user's screen gestures. This widget becomes handy when you want to give some extra details which are lengthy content but don't want to navigate to a new screen. You might have come across many apps that implement the same thing. So let us learn to implement DraggableScrollableSheet in our own app. Approach The approach of our project is very simple. In our app, we are going to display some names of countries with the help of the  ListView.builder  and Li...

Flutter | Material Banner Tutorial

In this tutorial, we will create and display Material Banner in Flutter . Material Banners are displayed at the top of the screen . User interaction is required to dismiss the banner . Material Banner Material Banner alerts the user about action and p rovides some actions for the user to take . In brief, it alerts the user about an issue and the user address the issue . The user should dismiss the banner to remove it from the screen else it will be active. Syntax  ScaffoldMessenger.of(context).showMaterialBanner(MaterialBanner( content: content, actions: actions)) Code Here is our starting class. It is a stateless class and we will display a banner when the Button is pressed. class SnackBarTutorial extends StatelessWidget { const SnackBarTutorial({Key ? key}) : super ( key: key); @ override Widget build(BuildContext context) { return Scaffold( appBar: AppBar( title: const Text( "Material Banner"...