【Flutter】GoRouter使用问题集绵

  • WillPopScope失效:

在项目使用go_router插件后,WillPopScope这个组件被自动屏蔽掉了无法正常使用,官方给出的说明是这样的

GoRouter and other Router-based APIs are not compatible with the WillPopScope widget.

//GoRouter以及相关的其他基础api均不兼容WillPopScope组件

See issue #102408 for details on what such an API might look like in go_router.

  • 解决方案:

在gorouter配置类中使用onExit回调方法代替

GoRouter router = GoRouter(
  routes: [
    // 首页
    GoRoute(
        path: '/home',
        name: '/home',
        pageBuilder: (context, state) {
          return 你的页面组件;
        },
        onExit: (context, state) async {
          if(满足需要pop的条件){
              //通过 return true来允许pop
              return true;
          }
          ....//其他操作
          //不进行pop
          return false;
        }
    )
  ]
)
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。