【问题标题】:initState() gets called when navigating to a new page导航到新页面时调用 initState()
【发布时间】: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 如何创建页面过渡。

【问题讨论】:

    标签: flutter flutter-animation


    【解决方案1】:

    调用是因为 this.widget 替换

      Navigator.of(context).push(IntroPageAnimation(
                enterPage: VeryCoolNewPage(),
                exitPage: this.widget,
              ));
    

      Navigator.of(context).push(IntroPageAnimation(
                enterPage: VeryCoolNewPage(),
                exitPage: EmptyWidget(),
              ));
    

    【讨论】:

    • 我也想为exitPage 制作动画。使用空的小部件,这是行不通的。 (对吧?)
    • 如果你调用exitPage,意味着它将从头开始构建:),然后initState将被调用
    • 是的,但我确实想要重建exitPage。我只想在当前状态下为当前页面以及正在导航到的页面设置动画。
    猜你喜欢
    • 2019-06-09
    • 2021-11-15
    • 2020-09-30
    • 1970-01-01
    • 1970-01-01
    • 2018-12-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多