Flutter:Couldn't Show the AlertDialog

576 views Asked by At

When I click the button, I want to show a dialog when the process is finished, but although I add the dialog part I cant see alertdialog in my screen. Where am I doing wrong? here's my code;

   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,
                        ),
                      ),
                    ),

And here's my dialog part

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

There are 2 answers

0
Salim Murshed On BEST ANSWER

You need to call the alertDialog inside 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,
        ),
      ),
    ))
0
Abhishek Ghaskata On
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();
          },
        ),
      ],
     )
    ),
  );
}

you need the showDialog method to show the dialog box