传输没有动画的页面 | Navigator.of(上下文).pushNamed()

Transfer pages without animation | Navigator.of(context).pushNamed()

我正在为网络使用 flutter。 页面正在以Navigator.of(context).pushNamed(Home.route);过渡,但此时正在播放动画。 是否可以禁用此动画?

我在main.dartmain()函数中写了下面的代码。

MaterialApp(
initialRoute: '/home',
routes: {
  '/home': (context) => Home(),
  '/profile': (context) => Profile(),
},

@immutable
class Home extends StatelessWidget {
  static const String route = '/home';
  ....
}

@immutable
class Profile extends StatelessWidget {
  static const String route = '/profile';
....
}

正如我之前提到的,以下代码用于转换页面。

 Navigator.of(context).pushNamed(Home.route);

我可以使用 Navigator.push 转换没有动画的页面,但我想使用 Navigator.of(context).pushNamed(),因为 link 不会改变。

有谁知道如何在仍然使用 Navigator.of(context).pushNamed() 的同时禁用动画? 谢谢。

我解决了这个问题。

我没有使用 Navigator.of().pushNamed() 但是,可以显示 URL 页。

首先,我从 MaterialApp() 中删除了 routes:,然后像这样添加了 onGenerateRoute:

onGenerateRoute: (settings) {
      Widget wid = Home();
      switch (settings.name) {
        case "/home":
          wid = Home();
          break;
        case "/profile":
          wid = Profile();
          break;
      }

      if (wid != null) {
        return PageRouteBuilder(
            settings:
                settings, // Pass this to make popUntil(), pushNamedAndRemoveUntil(), works
            pageBuilder: (_, __, ___) => wid,
            transitionsBuilder: (_, a, __, c) =>
                FadeTransition(opacity: a, child: c));
      }
      // Unknown route
      return MaterialPageRoute(builder: (_) => Home());
    },

当你想移动页面时,调用这个即可;

Navigator.of(context).pushReplacementNamed('/home');