0

当我单击按钮时,我想在进程完成时显示一个对话框,但是虽然我添加了对话框部分,但我在屏幕上看不到 alertdialog。我在哪里做错了?这是我的代码;

   child: InkWell(
                    onTap: () async {
                      final selectedLanguage =
                      await SharedPreferences.getInstance();
                      final setLanguage =
                      selectedLanguage.getString('selectedLanguage');
                      String language =
                      allTranslations.getLocaleKey(setLanguage);
                      _forgotPassword =
                      await createPasswordCode(_controller.text, language);
                     _dialog('Try');
                    },
                    child: Center(
                      child: Text(
                        'Send',
                        style: TextStyle(
                          fontSize: 16,
                          color: Colors.white,
                          fontWeight: FontWeight.bold,
                        ),
                      ),
                    ),

这是我的对话部分

  Widget _dialog(String title) {
    return AlertDialog(
      title: Text(
        title,
        style: TextStyle(fontWeight: FontWeight.bold),
      ),
      backgroundColor: Colors.white,
      shape: RoundedRectangleBorder(
        borderRadius:
        BorderRadius.all(Radius.circular(20)),
      ),
      actions: [
        FlatButton(
          child: Text('Approve'),
          onPressed: () {
            Navigator.of(context).pop();
          },
        ),
      ],
    );
  }
4

2 回答 2

1

你需要打电话给alertDialog里面showDialog

InkWell(
    onTap: () async {
  final selectedLanguage =
      await SharedPreferences.getInstance();
  final setLanguage =
  selectedLanguage.getString('selectedLanguage');
  String language =
  allTranslations.getLocaleKey(setLanguage);
  _forgotPassword =
      await createPasswordCode(_controller.text, language);
      showDialog<void>(
        context: context,
        barrierDismissible: false, // user must tap button!
        builder: (BuildContext context) {
          return _dialog("try");
        },
      );
    },
    child: Center(
      child: Text(
        'Send',
        style: TextStyle(
          fontSize: 16,
          color: Colors.white,
          fontWeight: FontWeight.bold,
        ),
      ),
    ))
于 2020-10-01T18:02:24.663 回答
0
Widget _dialog(String title) {
    return showDialog(
    barrierDismissible: false,
    context: context,
    child: AlertDialog(
      title: Text(
        title,
        style: TextStyle(fontWeight: FontWeight.bold),
      ),
      backgroundColor: Colors.white,
      shape: RoundedRectangleBorder(
        borderRadius:
        BorderRadius.all(Radius.circular(20)),
      ),
      actions: [
        FlatButton(
          child: Text('Approve'),
          onPressed: () {
            Navigator.of(context).pop();
          },
        ),
      ],
     )
    ),
  );
}

您需要showDialog显示对话框的方法

于 2020-10-01T18:02:15.867 回答