Search code examples
flutterdartflutter-alertdialog

Flutter:Couldn't Show the AlertDialog


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

Solution

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