首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Navigation.pop()和Navigation.push()表现得很奇怪

Navigation.pop()和Navigation.push()表现得很奇怪
EN

Stack Overflow用户
提问于 2019-10-20 08:47:20
回答 1查看 57关注 0票数 0

我在我的应用程序中创建了一个注册部分,并尝试执行以下操作:

  1. 用户单击“注册

”。

  1. 用户在注册

的任何阶段都按后退按钮。

  1. 然后弹出一个警告对话框,询问用户是否真的想离开注册进程

用户按Yes

然后将用户带回到应用程序的第一个(主要)页面.

这对我点击注册后的第一页有效,但是当我到达第二页时,步骤4将我保持在同一个页面上,然后当我再次尝试它时,它就能工作了。但情况不应该是这样。

经过一些调试,我发现了这个问题,但我不知道为什么会发生这种情况。

我在onBackPressed()函数末尾的第二个代码片段中作为注释编写了这个问题,这样就可以清楚地知道它在哪里中断:)。

下面是我从main.dart导航到注册过程页面的代码

代码语言:javascript
复制
Navigator.push(
    context,
    MaterialPageRoute(builder: (context) => SignUp())
)

然后,对于注册过程中的每一页,每当我进入下一页时,我都会执行Navigation.pop(context) to pop当前的注册页面从堆栈中移出,然后立即按下一页。

以下是每个注册页面中back按钮功能的代码:

代码语言:javascript
复制
bool backIsPressed = false;
Tools _tools = new Tools();

@override
void initState() {
  super.initState();
  BackButtonInterceptor.add(onBackPressed);
}

@override
void dispose() {
  BackButtonInterceptor.remove(onBackPressed);
  super.dispose();
}

bool onBackPressed(bool stopDefaultButtonEvent) {
  this.backIsPressed = !backIsPressed;

  if(backIsPressed) {
    _tools.yesNoAlert(
      context,
      "Going Back?",
      "Are you sure you want back? Changes made will not be saved.",
          () {
        this.backIsPressed = false;
        Navigator.pop(context, true);
      },
          () {
        this.backIsPressed = false;
        Navigator.pop(context, false);
      },
    ).then((res) {
      // ---------------- BREAKS HERE -----------------
      // "res" returns null the first time YES is pressed
      // But Navigation.pop(context, true) should return true according to Flutter's docs
      if(res) {
        Navigator.pop(context);
      }
    });

  }
  else {
    Navigator.pop(context);
  }

  return true;
}

最后,yesOrNoAlert()函数在Tools类中。

代码语言:javascript
复制
Future<bool> yesNoAlert(BuildContext context, String title,
    String description, Function yesFunction, Function noFunction) {
  this._isDialogOpen = true;
  return showDialog(
      context: context,
      barrierDismissible: false,
      builder: (BuildContext context) {
        return AlertDialog(
          title: new Text(title),
          content: new Text(description),
          actions: <Widget>[
            new FlatButton(
              child: new Text('Yes'),
              onPressed: () {
                _isDialogOpen = false;
                yesFunction();
              },
            ),
            new FlatButton(
              child: new Text('No'),
              onPressed: () {
                _isDialogOpen = false;
                noFunction();
              },
            )
          ],
        );
      });
}

希望我解释得很清楚。

EN

回答 1

Stack Overflow用户

发布于 2019-10-20 08:56:53

如果您想返回到第一页,而不是pop(),当用户选择“是”时,可以使用popUntil() .不需要通过“区域”

在这里了解更多信息:https://api.flutter.dev/flutter/widgets/Navigator/popUntil.html

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/58471351

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档