使用PageRouteBuilder 进行自定义页面转换。可以实现各种类型的页面过渡效果。下面是一些带注释的自定义页面过渡,您可以取消注释其中一个并运行以查看它在不同过渡上的工作方式。
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),
);
}
现在假设我必须通过单击按钮打开一条新路线:
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!'),
),
),
),
);
}