How to slide to new page on the right instead of the bottom in flutter?

13,584

Solution 1

Check out the CupertinoPageRoute:

A modal route that replaces the entire screen with an iOS transition.

The page slides in from the right and exits in reverse. The page also shifts to the left in parallax when another page enters to cover it.

The page slides in from the bottom and exits in reverse with no parallax effect for fullscreen dialogs.

There's a demo of it in the flutter gallery example app:

Navigator.of(context, rootNavigator: true).push(
  new CupertinoPageRoute<bool>(
    fullscreenDialog: true,
    builder: (BuildContext context) => new Tab3Dialog(),
  ),
);

Solution 2

You can create a function with your desirable animation

Route createRoute(Widget page) {
  return PageRouteBuilder(
    pageBuilder: (context, animation, secondaryAnimation) => page,
    transitionsBuilder: (context, animation, secondaryAnimation, child) {
      const begin = Offset(1.0, 0.0);
      const end = Offset.zero;
      const curve = Curves.ease;

      var tween = Tween(begin: begin, end: end).chain(CurveTween(curve: curve));

      return SlideTransition(
        position: animation.drive(tween),
        child: child,
      );
    },
  );
}

And call it each time you push to a new screen

Navigator.of(context).push(createRoute(SearchView()))

If you want to change the direction you need to change the offset of begin If you want to change the effect you need to change SlideTransition

Share:
13,584
jia chen
Author by

jia chen

Updated on December 05, 2022

Comments

  • jia chen
    jia chen over 1 year

    The default flutter animation to transit a new page into the focus is to slide it up from the bottom. How do i change this behaviors and slide the new page in from the right or left instead?

          Navigator.push(
            context,
              new PageRouteBuilder(
              pageBuilder: (BuildContext context, _, __) {
                return new SearchView();
              }
              )
          );