【发布时间】:2021-07-21 09:55:26
【问题描述】:
好的,所以,我有这个类 (IntroPageAnimation),它扩展了 PageRouteBuilder,我用它来动画我的页面转换。
我传递了 2 个 Widget,即当前页面 (exitPage) 和应用程序应该导航到的页面 (enterPage)。
class IntroPageAnimation extends PageRouteBuilder {
final Widget enterPage;
final Widget exitPage;
IntroPageAnimation({required this.enterPage, required this.exitPage})
: super(
transitionDuration: Duration(milliseconds: 500),
transitionsBuilder: (context, animation, secAnimation, child) {
animation = CurvedAnimation(parent: animation, curve: Curves.easeInOutCubic);
return Stack(
children: <Widget>[
SlideTransition(
position: Tween<Offset>(
begin: Offset(0, 0),
end: Offset(0, -1),
).animate(animation),
child: exitPage,
),
SlideTransition(
position: Tween<Offset>(
begin: Offset(0, 1),
end: Offset(0, 0),
).animate(animation),
child: child,
),
],
);
},
pageBuilder: (context, animation, secAnimation) {
return enterPage;
},
);
}
我这样使用它,在 onClick 内,在我主页的 build 函数内:
Navigator.of(context).push(IntroPageAnimation(
enterPage: VeryCoolNewPage(),
exitPage: this.widget,
));
但我的问题是:每次我导航到那个新页面时,initState() 都会在 exitPage 上调用。如何防止这种情况发生?
我正在关注this tutorial 如何创建页面过渡。
【问题讨论】: