【问题标题】:How to prevent navigation from Navigator.pushNamed... and display bottom modal?如何防止从 Navigator.pushNamed... 导航并显示底部模式?
【发布时间】:2020-09-25 21:47:16
【问题描述】:

如果我有一个定义了routes 的 MaterialApp,则在导航时会触发一个函数。

对于某些路线(受保护的路线),我想阻止导航并打开模态底部表,showModalBottomSheet

我试着这样做:

class App extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'My Flutter App',
      routes: <String, WidgetBuilder>{
        '/profile': (context) {
          if (signedIn) {
            return PopScreen();
          } else {
            showModalBottomSheet<void>(
              context: context,
              builder: (BuildContext context) {
                return Container(
                    color: Color(0xFF737373),
                    child: FractionallySizedBox(
                        heightFactor: 1,
                        child: Container(
                          decoration: BoxDecoration(
                              color: Colors.amber,
                              borderRadius: BorderRadius.only(
                                  topRight: Radius.circular(radius),
                                  topLeft: Radius.circular(radius))),
                          child: Center(
                            child: Column(
                              mainAxisAlignment: MainAxisAlignment.center,
                              mainAxisSize: MainAxisSize.min,
                              children: <Widget>[
                                const Text('Modal BottomSheet'),
                                RaisedButton(
                                  child: const Text('Close BottomSheet'),
                                  onPressed: () => Navigator.pop(context),
                                ),
                              ],
                            ),
                          ),
                        )));
              },
            );
          }
        },
      },
      home: Home(),
    );
  }
}

通过上述方法,路由器无法到达/profile 路由,这很好,但随后出现错误The builder for route returned null. The builders must never return null

如何阻止导航?

有没有更好的方法呢?我发现的教程似乎建议在按钮上添加if logged in 并从那里重定向,但似乎这部分应该位于路由器内部。

【问题讨论】:

  • 显示更多代码 showModalBottomSheet(...);
  • @griffins 我添加了更多描述。我还找到了 ModalRoute,根据描述,这正是我想做的,但我找不到如何使用它的示例:(api.flutter.dev/flutter/widgets/ModalRoute-class.html
  • 我认为您需要在 showModalBottomSheet 之前添加 return 或返回简单的脚手架然后在 onInit 函数中显示对话框或操作表

标签: flutter


【解决方案1】:

首先我认为您需要在 showModalBottomSheet 之前添加 return 或返回简单的脚手架,然后在 onInit 函数中显示对话框或操作表

如果这不起作用,请尝试

创建一个服务或帮助函数来导航到配置文件并在整个应用程序中使用它

class NavigationService {

  static navigateToProfile(Context context) {...}


}

检查 isLogged,如果为 true,则导航到路由。如果为 false ShowDialog() 或为此打开操作表

【讨论】:

  • 添加助手会有所帮助,但如果我忘记使用它,界面会变得不稳定。我希望它发生在路由器级别。在 React native 中,我曾经使用 reactnavigation.org/docs/4.x/custom-routers,从导航返回 null,然后打开一个 modal
  • 当从函数返回任何内容时,仍然会触发导航。我想阻止它。
猜你喜欢
  • 2022-09-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-04-04
  • 2022-01-18
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多