Home > Software design >  I cant close the AlertDialog box
I cant close the AlertDialog box

Time:12-15

void logoutBack() {
    showDialog(
        context: context,
        builder: (_) => AlertDialog(
          title: new Text("Logout"),
          content: new Column(
            mainAxisAlignment: MainAxisAlignment.start,
            mainAxisSize: MainAxisSize.min,
            children: <Widget>[
              CustomDialogue(
                isSimpleDialogue: true,
                message:
                "Are you sure you want to logout ?",
              )
            ],
          ),
          actions: <Widget>[
            FlatButton(
              splashColor: primaryColor.withOpacity(0.5),
              child: Text(
                'CANCEL',
                style: TextStyle(color: primaryColor),
              ),
              onPressed: () {
                //Navigator.of(context).pop();
              },
            ),
            FlatButton(
              splashColor: primaryColor.withOpacity(0.5),
              child: Text(
                'OK',
                style: TextStyle(color: primaryColor),
              ),
              onPressed: () async {
                // userId = null;
                // SharedPreferences sharedPreferences =
                // await SharedPreferences.getInstance();
                // String log = await save_string_prefs(
                //     "loginuser", null);
                // save_string_prefs("signupuser", null);
                // save_string_prefs("loginuserId", null);
                // save_string_prefs("signUpUesrId", null);
                // save_string_prefs("loginToken", null);
                // save_string_prefs("signUpToken", null);
                // save_string_prefs("emailPref", null);
                // save_string_prefs("passwordPref", null);
                // save_string_prefs(
                //     "emailSignUpPref", null);
                // save_string_prefs(
                //     "passwordSignUpPref", null);
                // clearPrefs();
                //Navigator.of(context).pop();
                // Navigator.of(context).pushReplacement(
                //     new MaterialPageRoute(
                //         builder: (context) =>
                //         new LoginType()));
                RedirectToHomeScreenBasedOnUser(context, LoginUserMode.USER_AS_NONE);
              },
            ),
          ],
        ));
  }

Image

CodePudding user response:

remove the comment in cancel button and replace FlatButton with TextButton because replace button is deprecated and pass the context

logoutBack(BuildContext context){

TextButton(
              splashColor: primaryColor.withOpacity(0.5),
              child: Text(
                'CANCEL',
                style: TextStyle(color: primaryColor),
              ),
              onPressed: () {
                Navigator.of(context).pop();
              },
            ),

}

CodePudding user response:

You should do something like this.

class AlertDialogTest extends StatelessWidget {
  const AlertDialogTest({Key key}) : super(key: key);

  void logoutBack(BuildContext context) {
    showDialog(
        context: context,
        builder: (_) => AlertDialog(
              title: new Text("Logout"),
              content: new Column(
                mainAxisAlignment: MainAxisAlignment.start,
                mainAxisSize: MainAxisSize.min,
                children: <Widget>[
                  CustomDialogue(
                    isSimpleDialogue: true,
                    message: "Are you sure you want to logout ?",
                  )
                ],
              ),
              actions: <Widget>[
                FlatButton(
                  splashColor: primaryColor.withOpacity(0.5),
                  child: Text(
                    'CANCEL',
                    style: TextStyle(color: primaryColor),
                  ),
                  onPressed: () {
                    //Navigator.of(context).pop();
                  },
                ),
                FlatButton(
                  splashColor: primaryColor.withOpacity(0.5),
                  child: Text(
                    'OK',
                    style: TextStyle(color: primaryColor),
                  ),
                  onPressed: () async {
                    // userId = null;
                    // SharedPreferences sharedPreferences =
                    // await SharedPreferences.getInstance();
                    // String log = await save_string_prefs(
                    //     "loginuser", null);
                    // save_string_prefs("signupuser", null);
                    // save_string_prefs("loginuserId", null);
                    // save_string_prefs("signUpUesrId", null);
                    // save_string_prefs("loginToken", null);
                    // save_string_prefs("signUpToken", null);
                    // save_string_prefs("emailPref", null);
                    // save_string_prefs("passwordPref", null);
                    // save_string_prefs(
                    //     "emailSignUpPref", null);
                    // save_string_prefs(
                    //     "passwordSignUpPref", null);
                    // clearPrefs();
                    //Navigator.of(context).pop();
                    // Navigator.of(context).pushReplacement(
                    //     new MaterialPageRoute(
                    //         builder: (context) =>
                    //         new LoginType()));
                    RedirectToHomeScreenBasedOnUser(
                        context, LoginUserMode.USER_AS_NONE);
                  },
                ),
              ],
            ));
  }

  @override
  Widget build(BuildContext context) {
    return Container(
      child: TextButton(
        onPressed: () {
          logoutBack(context);
        },
        child: Text("Open dialog"),
      ),
    );
  }
}

Ps: I'll recommend you use TextButton instead of FlatButton since the later is deprecated

https://api.flutter.dev/flutter/material/TextButton-class.html

  • Related