Use PageRouteBuilder for a custom page transition. You can achieve various types of page transition effects. Below are some commented custom page transition you can uncomment one of them and run to see how it works on different transitions.
PageRouteBuilder _pageRouteBuilder() {
return PageRouteBuilder(
pageBuilder: (context, animation, secondaryAnimation) {
return Demo2();
},
transitionsBuilder: (context, animation, secondaryAnimation, child) {
// return SlideTransition(
// position: animation.drive(
// Tween<Offset>(begin: Offset(-1.0, 0.0), end: Offset.zero),
// ),
// child: SlideTransition(
// position: Tween<Offset>(
// begin: Offset.zero,
// end: Offset(0.0, 1.0),
// ).animate(secondaryAnimation),
// child: child,
// ),
// );
// return ScaleTransition(
// scale: animation.drive(
// Tween<double>(begin: 0.0, end: 1.0).chain(
// CurveTween(
// curve: Interval(0.0, 0.5, curve: Curves.elasticIn),
// ),
// ),
// ),
// child: ScaleTransition(
// scale: Tween<double>(begin: 1.5, end: 1.0).animate(
// CurvedAnimation(
// parent: animation,
// curve: Interval(0.5, 1.0, curve: Curves.elasticInOut),
// ),
// ),
// child: child,
// ),
// );
return ScaleTransition(
scale: CurvedAnimation(parent: animation, curve: Curves.elasticInOut),
child: child,
);
},
transitionDuration: const Duration(seconds: 1),
);
}
And now suppose I have to open a new route on a click of a button:
Widget build(BuildContext context) {
double width = MediaQuery.of(context).size.width;
double height = MediaQuery.of(context).size.height;
return Scaffold(
body: SafeArea(
child: Center(
child: RaisedButton(
onPressed: () {
Navigator.push(context, _pageRouteBuilder());
},
child: Text('Go!'),
),
),
),
);
}