【问题标题】:Transfer pages without animation | Navigator.of(context).pushNamed()无动画转移页面 | Navigator.of(context).pushNamed()
【发布时间】:2021-11-13 14:00:51
【问题描述】:

我正在为 web 使用颤振。 页面正在以Navigator.of(context).pushNamed(Home.route); 转换,但此时播放动画。 是否可以禁用此动画?

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

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(),因为链接不会改变。

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

【问题讨论】:

    标签: flutter dart flutter-web


    【解决方案1】:

    我解决了这个问题。

    我没有使用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');
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-01-19
      • 2019-08-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-07-21
      • 2021-03-14
      相关资源
      最近更新 更多