6

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?

tudorprodan
  • 950
  • 8
  • 18
  • https://stackoverflow.com/questions/49874272/how-to-navigate-to-other-page-without-animation-flutter – Sharad Paghadal Aug 08 '19 at 08:19
  • @SharadPaghadal this does not work since depending on user input I may need to show or not show the pop animation. Overriding `buildTransitions` will predefine if the animation is played or not before pushing the route. – tudorprodan Aug 08 '19 at 10:01

1 Answers1

1

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.

Omatt
  • 8,564
  • 2
  • 42
  • 144