Flutter 颤振中具有路径的持续底部导航杆

Flutter 颤振中具有路径的持续底部导航杆,flutter,dart,routes,navigation,Flutter,Dart,Routes,Navigation,我很难实现持续的颤振。我的目标是创建一个具有多个屏幕和多条路线的应用程序(最简单的示例): 我找到了一篇中级文章,在对实现进行了一点努力之后,我认为我找到了完美的解决方案但由于我想实现一个注销功能,将用户发送回LoginScreen,因此路由无法按预期工作… // Navigate back to the LoginScreen (this doesn't work as expected...) Navigator.of(context).pushAndRemoveUnt

我很难实现持续的颤振。我的目标是创建一个具有多个屏幕和多条路线的应用程序(最简单的示例):

我找到了一篇中级文章,在对实现进行了一点努力之后,我认为我找到了完美的解决方案但由于我想实现一个注销功能,将用户发送回LoginScreen,因此路由无法按预期工作…

// Navigate back to the LoginScreen (this doesn't work as expected...)
          Navigator.of(context).pushAndRemoveUntil(
              MaterialPageRoute(
                builder: (context) => LoginScreen(),
              ),
              (Route<dynamic> route) => false);

正如您在gif中所看到的,问题发生在单击注销按钮之后。而不是导航回LoginScreen,LoginScreen get被嵌入到带有底部导航栏的主屏幕中

class ContextKeeper {
  static BuildContext buildContext;

  void init(BuildContext context) {
    buildContext = context;
  }
}
如何改变这种行为?我想我会用
按钮删除所有路由,直到

// Navigate back to the LoginScreen (this doesn't work as expected...)
          Navigator.of(context).pushAndRemoveUntil(
              MaterialPageRoute(
                builder: (context) => LoginScreen(),
              ),
              (Route<dynamic> route) => false);
//导航回LoginScreen(这无法按预期工作…)
Navigator.of(context).pushandremoveintil(
材料路线(
生成器:(上下文)=>LoginScreen(),
),
(路由)=>false);

这里有一个最小的可复制示例:

经过几次尝试,我成功地解决了这个问题。我需要保存主屏幕的上下文(index.dart->保存底部导航栏)

lib/screens/main/index.dart:

@override
void initState() {
  super.initState();
  ContextKeeper().init(context); // Save the context
}
然后改变

Navigator.of(context).pushAndRemoveUntil(MaterialPageRoute(builder: (context) => LoginScreen(),),(Route<dynamic> route) => false);

而且很有效。

经过几次尝试,我终于解决了这个问题。我需要保存主屏幕的上下文(index.dart->保存底部导航栏)

lib/screens/main/index.dart:

@override
void initState() {
  super.initState();
  ContextKeeper().init(context); // Save the context
}
然后改变

Navigator.of(context).pushAndRemoveUntil(MaterialPageRoute(builder: (context) => LoginScreen(),),(Route<dynamic> route) => false);
而且很有效