本文介绍了Navigation.pop()和Navigation.push()行为异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Flutter在我的应用程序中创建一个Sign-Up部分,我正在尝试执行以下操作:

I'm creating a Sign-Up section in my application using Flutter and I'm trying to do the following:

  1. 用户单击注册"

  1. The user clicks Sign-Up

用户在注册过程中的任何阶段都按下后退按钮

The user presses the back button at any stage during sign-up

然后弹出一个警告对话框,询问用户是否真的要退出注册过程

Then an alert dialog pops up asking if the user really wants to leave sign-up process

用户按下

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

The user is then taken back to the first (main) page of the app.

单击注册后此功能适用于第一页,但是当我进入第二页时,步骤4 使我停留在同一页上,然后再次尝试时可以使用.但这不是事实.

This works for the first page after I click sign-up, but when I get to the second page, step 4 keeps me on the same page, then when I try it again, it works. But this shouldn't be the case.

经过调试后,我发现了问题,但不知道为什么会发生.

After some debugging, I found the issue, but I don't know why it's happening.

我在onBackPressed()函数末尾的第二个代码段中以注释形式写了这个问题,以便清楚地知道它的中断位置:).

I wrote the issue as a comment in my second code snippet, at the end of the onBackPressed() function so it's clear as to where it's breaking :).

这是我的代码,用于从我的 main.dart 导航至注册过程页面:

Here is my code for navigating to the sign-up process page from my main.dart:

Navigator.push(
    context,
    MaterialPageRoute(builder: (context) => SignUp())
)

然后在注册过程中的每一页上,每当我进入下一页时,我都会执行Navigation.pop(context)弹出从堆栈中弹出当前的注册页,然后执行紧接着推下一页.

Then for each page during the sign-up process, whenever I go onto the next page I do a Navigation.pop(context) to pop the current sign-up page off the stack the do a Navigation.push() right after to push the next page on.

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

Here is the code for the back button functionality in each sign-up page:

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;
}

最后,是Tools类中的yesOrNoAlert()函数.

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();
              },
            )
          ],
        );
      });
}

希望我解释得很好.

推荐答案

如果要返回第一页,而不是pop(),则当用户选择是"时可以使用popUntil()...不需要通过"res"

if you want go back to the first page, instead of pop(), you can use popUntil() when the user selects yes... no need to pass "res"

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

Learn more about it here: https://api.flutter.dev/flutter/widgets/Navigator/popUntil.html

这篇关于Navigation.pop()和Navigation.push()行为异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-03 07:04