代码之家  ›  专栏  ›  技术社区  ›  N Sharma

在颤振中点击警报对话框时屏幕未关闭。

  •  0
  • N Sharma  · 技术社区  · 7 年前

    嗨,我设计了一个颤振屏幕。我有 AlertDialog 在此,我想关闭对话框并按屏幕。马上 对话框 按下时关闭,但屏幕不关闭。

    有人知道怎么做吗?

    class ForgotPasswordScreen extends StatefulWidget {
      @override
      State<StatefulWidget> createState() {
        return ForgotPasswordScreenState();
      }
    }
    
    class ForgotPasswordScreenState extends State<ForgotPasswordScreen> {
      var emailController = new TextEditingController();
      var authHandler = new Auth();
      bool isLoading = false;
    
      @override
      Widget build(BuildContext context) {
        return new Scaffold(
            body: Container(
                height: MediaQuery.of(context).size.height,
                decoration: BoxDecoration(
                  color: Colors.white,
                ),
                child: new Column(
                  crossAxisAlignment: CrossAxisAlignment.center,
                  mainAxisSize: MainAxisSize.max,
                  mainAxisAlignment: MainAxisAlignment.center,
                  children: <Widget>[
                    new Row(
                      children: <Widget>[
                        new Expanded(
                            child: isLoading
                                ? Center(child: CircularProgressIndicator())
                                : new Container()),
                      ],
                    ),
                    new Row(
                      children: <Widget>[
                        new Expanded(
                          child: new Padding(
                            padding: const EdgeInsets.only(left: 40.0),
                            child: new Text(
                              "EMAIL",
                              style: TextStyle(
                                fontWeight: FontWeight.bold,
                                color: Colors.redAccent,
                                fontSize: 15.0,
                              ),
                            ),
                          ),
                        ),
                      ],
                    ),
                    new Container(
                      width: MediaQuery.of(context).size.width,
                      margin:
                          const EdgeInsets.only(left: 40.0, right: 40.0, top: 10.0),
                      alignment: Alignment.center,
                      decoration: BoxDecoration(
                        border: Border(
                          bottom: BorderSide(
                              color: Colors.redAccent,
                              width: 0.5,
                              style: BorderStyle.solid),
                        ),
                      ),
                      padding: const EdgeInsets.only(left: 0.0, right: 10.0),
                      child: new Row(
                        crossAxisAlignment: CrossAxisAlignment.center,
                        mainAxisAlignment: MainAxisAlignment.start,
                        children: <Widget>[
                          new Expanded(
                            child: TextField(
                              controller: emailController,
                              textAlign: TextAlign.left,
                              decoration: InputDecoration(
                                border: InputBorder.none,
                                hintText: 'PLEASE ENTER YOUR EMAIL',
                                hintStyle: TextStyle(color: Colors.grey),
                              ),
                            ),
                          ),
                        ],
                      ),
                    ),
                    Divider(
                      height: 24.0,
                    ),
                    new Container(
                      width: MediaQuery.of(context).size.width,
                      margin:
                          const EdgeInsets.only(left: 30.0, right: 30.0, top: 20.0),
                      alignment: Alignment.center,
                      child: new Row(
                        children: <Widget>[
                          new Expanded(
                            child: new FlatButton(
                              shape: new RoundedRectangleBorder(
                                borderRadius: new BorderRadius.circular(30.0),
                              ),
                              color: Colors.redAccent,
                              onPressed: () {
                                setState(() {
                                  isLoading = true;
                                });
                                authHandler
                                    .sendPasswordResetEmail(emailController.text)
                                    .then((void nothing) {
                                  showDialog(
                                    context: context,
                                    builder: (BuildContext context) {
                                      // return object of type Dialog
                                      return AlertDialog(
                                        content: new Text(
                                            "Password reset email has been sent."),
                                        actions: <Widget>[
                                          // usually buttons at the bottom of the dialog
                                          new FlatButton(
                                            child: new Text("OK"),
                                            onPressed: () {
                                              Navigator.pop(context);
                                            },
                                          ),
                                        ],
                                      );
                                    },
                                  );
    
                                  setState(() {
                                    isLoading = false;
                                  });
                                }).catchError((e) => print(e));
                              },
                              child: new Container(
                                padding: const EdgeInsets.symmetric(
                                  vertical: 20.0,
                                  horizontal: 20.0,
                                ),
                                child: new Row(
                                  mainAxisAlignment: MainAxisAlignment.center,
                                  children: <Widget>[
                                    new Expanded(
                                      child: Text(
                                        "FORGOT PASSWORD",
                                        textAlign: TextAlign.center,
                                        style: TextStyle(
                                            color: Colors.white,
                                            fontWeight: FontWeight.bold),
                                      ),
                                    ),
                                  ],
                                ),
                              ),
                            ),
                          ),
                        ],
                      ),
                    ),
                  ],
                )));
      }
    }
    
    2 回复  |  直到 7 年前
        1
  •  1
  •   Rémi Rousselet    7 年前

    理想情况下,您需要打电话 pop 不止一次。一个用于模式,另一个用于实际路线。

    实现这一点有几种方法。但理想情况下你会想 await 在触发另一个关闭之前关闭对话框:

    foo() async {
      await showDialog(
        context: context,
        builder: (context) => AlertDialog(
              actions: [
                new FlatButton(
                  child: new Text("OK"),
                  onPressed: () => Navigator.pop(context),
                ),
              ],
            ),
      );
      Navigator.pop(context);
    }
    

    这样,路线和模式都可以处理它们的关闭,不管它们喜欢什么。

        2
  •  0
  •   Raj008    7 年前

    试试这个

    showPop() async {
        await showDialog(
          context:  context,
          barrierDismissible: true,
          builder: (context) => AlertDialog(
            actions: [
              new FlatButton(
                child: new Text("Close"),
                 onPressed: () => Navigator.pop(context),
              ),
            ],
          ),
        );
    
      }