【问题标题】:AppBar flickers when using Navigator.of(context).pushNamedAndRemoveUntil()使用 Navigator.of(context).pushNamedAndRemoveUntil() 时 AppBar 闪烁
【发布时间】:2020-11-27 09:48:55
【问题描述】:

在用户登录我的应用程序后,我想清除所有以前的路线。但是,我的 Flutter 应用程序中的以下代码存在问题:

void clearRoutes()
  {
    //createLinks();
    Navigator.of(context).pushNamedAndRemoveUntil('/my_home', (Route<dynamic> route) => false);
  }

  Widget build(BuildContext context)
  {
    WidgetsBinding.instance.addPostFrameCallback((_) => clearRoutes());

    return Scaffold(
          appBar: AppBar(
            centerTitle: true,
            title: Text("iWhiz Home"),
          ),
          body: ListView.builder(
              itemCount: links.length,
              itemBuilder: (context, index) {
                return ListTile(
                  title: Text(links[index]),
                );
              }
          ),
        );
  }

上面的代码导致了这个问题:

给我这个问题的代码行是Navigator.of(context).pushNamedAndRemoveUntil('/my_home', (Route&lt;dynamic&gt; route) =&gt; false);

这个问题可以解决吗?

【问题讨论】:

    标签: flutter dart


    【解决方案1】:

    我找到了解决方案。这是给我带来问题的代码:

    class Login extends StatelessWidget
    {
      void homePage(BuildContext context)
      {
        Navigator.pushNamed(context, "/my_home");
      }
    
      Widget build(BuildContext context)
      {
        return Scaffold(
          appBar: AppBar(
            centerTitle: true,
            title: Text("Login"),
          ),
          body: Center(
              child:
              Column(
                children: [
                  Text("Welcome to the Login Page"),
                  ElevatedButton(
                    onPressed: ()
                    {
                      homePage(context);
                    },
                    child: Text("Login"),
                  ),
                ],
              )
          ),
        );
      }
    }
    
    class MyHome extends StatelessWidget
    {
      void removePreviousRoutes(BuildContext context)
      {
        //The problem code.
        Navigator.of(context).pushNamedAndRemoveUntil('/my_home', (Route<dynamic> route) => false);
      }
    
      Widget build(BuildContext context)
      {
        WidgetsBinding.instance.addPostFrameCallback((_) => removePreviousRoutes(context));
    
        return Scaffold(
            appBar: AppBar(
              centerTitle: true,
              title: Text("My Home"),
            ),
            body: Center(
              child:
              Text("Welcome to the Home Page"),
            )
        );
      }
    }
    

    将显示的初始页面是登录。在用户点击“欢迎来到登录页面”后,他们将被引导至 MyHome 页面。问题来自 MyHome 页面上的 Navigator.of(context).pushNamedAndRemoveUntil('/my_home', (Route&lt;dynamic&gt; route) =&gt; false);

    解决方案:

    class Login extends StatelessWidget
    {
      void homePage(BuildContext context)
      {
        //The solution.
        Navigator.pushNamed(context, "/my_home");
    
        Navigator.of(context).pushNamedAndRemoveUntil('/my_home', (Route<dynamic> route) => false);
      }
    
      Widget build(BuildContext context)
      {
        return Scaffold(
          appBar: AppBar(
            centerTitle: true,
            title: Text("Login"),
          ),
          body: Center(
              child:
              Column(
                children: [
                  Text("Welcome to the Login Page"),
                  ElevatedButton(
                    onPressed: ()
                    {
                      homePage(context);
                    },
                    child: Text("Login"),
                  ),
                ],
              )
          ),
        );
      }
    }
    
    class MyHome extends StatelessWidget
    {
      Widget build(BuildContext context)
      {
        return Scaffold(
            appBar: AppBar(
              centerTitle: true,
              title: Text("My Home"),
            ),
            body: Center(
              child:
              Text("Welcome to the Home Page"),
            )
        );
      }
    }
    

    解决方案是推送新路由登录中删除所有以前的路由,INSTEAD OF中推送新路由登录并从注册中删除以前的路线。完成此操作后,我不再收到闪烁的问题。

    【讨论】:

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