Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to pop screen without animation in Flutter

Tags:

flutter

Basically I want to Navigator.of(context).pop(); but without the animation.

From reading the docs I see that you can override this animations only when pushing a route. In my case, I don't know in advance if I want to show the pop animation or not when I remove the route, so I need a solution that allows me to specify if I want this animation by the time I call pop()

Is this possbile?

like image 493
tudorprodan Avatar asked Aug 08 '19 08:08

tudorprodan


People also ask

How do I navigate without animation in Flutter?

With Navigator 2.0, there are two ways: Rebuild the Navigator with a new pages list that has the last item replaced. If the previous Page and replacement Page both have no key or the same key, then Flutter will treat them as the same page and will not animate.

What is MaterialPageRoute?

MaterialPageRoute<T> class Null safety. A modal route that replaces the entire screen with a platform-adaptive transition. For Android, the entrance transition for the page zooms in and fades in while the exiting page zooms out and fades out. The exit transition is similar, but in reverse.


1 Answers

To create/pop a page without animation, you can create a custom page like this.

class NoAnimationPage extends Page<dynamic> {
  const NoAnimationPage({
    LocalKey? key,
    required this.child,
  }) : super(key: key);

  final Widget child;

  @override
  Route<dynamic> createRoute(BuildContext context) => PageRouteBuilder<dynamic>(
        settings: this,
        pageBuilder: (_, __, ___) => child, 
        // don't wrap in an animation to create a page without animation.
  );
}

Then use NoAnimationPage instead of MaterialPage or CupertinoPage.

like image 103
Omatt Avatar answered Oct 19 '22 14:10

Omatt