按下按钮时未显示警报对话框

时间:2020-06-13 13:57:43

标签: flutter dart

警报对话框的类

class AlertWindow extends StatelessWidget {
  final String title;

  const AlertWindow({Key key, this.title}) : super(key: key);


  @override
  Widget build(BuildContext context) {

    return Builder(
        builder:(BuildContext context) {
          return AlertDialog(
            title: Text(this.title),
            actions: <Widget>[
              new FlatButton(
                  onPressed: (){
                    Navigator.of(context).pop();
                  },
                  child: new Text(
                      "OK"
                  )
              ),
            ],
          );
        }
    );
  }
}

在这样的aysnc函数中被调用

  Future<ParseUser> SignUP(username, pass, email) async {
    var user = ParseUser(username, pass, email);   // You can add columns to user object adding "..set(key,value)"
    var result = await user.create();
    if (result.success) {
      setState(() {
        _parseUser = user;     // Keep the user
      });
      print(user.objectId);
      new AlertWindow(
        title: "Signup success " + user.objectId,
      );
    } else {
      print(result.error.message);
      new AlertWindow(
        title: "Signup error " + result.error.message,
      );
    }
  }

运行此命令后,我可以在控制台中看到print语句,但是AlertWindow没有出现。

我有种预感,它可能与创建时未传递给父BuildContext的父AlertDialog有关。

2 个答案:

答案 0 :(得分:1)

您需要调用函数PosixFileAttributes以使showDialog出现:

AlertDialog

工作示例:

https://dartpad.dev/051f787e1737de84609a390d31c36ee0

https://api.flutter.dev/flutter/material/showDialog.html

答案 1 :(得分:1)

尝试使用功能而不是使用小部件 创建一个返回未来的新功能

      Future<dynamic> _showDialog(BuildContext context){
             return showDialog(
                context: context,
                builder: (BuildContext context) {
                     return AlertDialog(
                          title: Text(this.title),
                         actions: <Widget>[
                             new FlatButton(
                                 onPressed: (){
                                  Navigator.of(context).pop();
                                 },
                           child: new Text(
                                     "OK"
                            )
                         ),
                      ],
                  );
              }
          );
       }