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?
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.
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.
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
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With