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();
}
)
);
The basic way — push and pop The Navigator behaves like a stack, so the most simple way is to push a new page onto it when you want to navigate to it and to pop a page if you want to go back.
In the Flutter SlideTransition is a widget that animates the position of a widget relative to its normal position and slides required a little bit more interaction by using tween, in this case, offset, and flutter widget. The translation is expressed as an Offset scaled to the child's size.
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(),
),
);
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
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