Home > OS >  AlertDialog is not working on onPressed()
AlertDialog is not working on onPressed()

Time:02-02

trying to do simple login form and want to test the result.

I want to show a message on dialog box but when I am trying to use showDialog and AlertDialog but, it is not working. I have tried debugPrint/Print/log and other functions but nothing worked . it will not print it on console as well.

can anyone please help me?

I want to print message on dialog box.

import 'package:flutter/material.dart';

class Login extends StatefulWidget {
  const Login({super.key});

  @override
  State<Login> createState() => _LoginState();
}

class _LoginState extends State<Login> {
  late bool _autovalidate = false;

  // Message Stored
  late String message;

  // login credentials var
  late String userName = 'admin';
  late String passWord = '123';

  // TextEditingController initiated
  final name = TextEditingController();
  final password = TextEditingController();

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text(
          'Expense Manager',
          style: TextStyle(
            color: Colors.white,
            fontWeight: FontWeight.bold,
          ),
        ),
      ),
      body: SingleChildScrollView(
        child: Form(
          child: Card(
            shape: RoundedRectangleBorder(
              borderRadius: BorderRadius.circular(15.0),
            ),
            child: Column(
              children: <Widget>[
                Padding(
                  padding: EdgeInsets.all(10.0),
                ),
                Image(
                  image: AssetImage('images/logo.png'),
                  height: 100.0,
                  width: 100.0,
                ),
                ListTile(
                  leading: Icon(Icons.person),
                  title: TextFormField(
                    controller: name,
                    validator: (input) {
                      if (input!.isEmpty) {
                        return 'Enter Username';
                      }
                    },
                    decoration: InputDecoration(labelText: 'Username'),
                  ),
                ),
                ListTile(
                  leading: Icon(Icons.password),
                  title: TextFormField(
                    controller: password,
                    obscureText: true,
                    validator: (input) {
                      if (input!.isEmpty) {
                        return 'Enter Password';
                      }
                    },
                    decoration: InputDecoration(labelText: 'Password'),
                  ),
                ),
                Container(
                  padding: EdgeInsets.all(20.0),
                  child: ButtonTheme(
                    height: 40.0,
                    minWidth: 200.0,
                    child: ElevatedButton(
                      onPressed: () {
                        _showAppLogin();
                      },
                      child: Text(
                        'Login',
                        style: TextStyle(
                          color: Colors.white,
                        ),
                      ),
                      style: ElevatedButton.styleFrom(
                        shape: RoundedRectangleBorder(
                          borderRadius: BorderRadius.circular(10.0),
                        ),
                        backgroundColor: Colors.redAccent,
                        padding: EdgeInsets.zero,
                        tapTargetSize: MaterialTapTargetSize.shrinkWrap,
                      ),
                    ),
                  ),
                ),
              ],
            ),
          ),
        ),
      ),
    );
  }

  _showAppLogin() {
    if (name.text == null && password.text == null) {
      if (name.text == userName && password.text == passWord) {
        _showAlertDialog('Login Successful');
      } else {
        setState(() {
          _autovalidate = true;
        });
        _showAlertDialog('Invalid Credentials');
      }
    }
  }

  void _showAlertDialog(String message) {
    AlertDialog alertDialog = AlertDialog(
      icon: Icon(Icons.message_outlined),
      content: Text(message),
    );
    showDialog(context: context, builder: (_) => alertDialog);
  }
}

CodePudding user response:

_showAppLogin() {
    if (name.text == null && password.text == null) {
      if (name.text == userName && password.text == passWord) {

This is not going to work. The first if checks if both name.text and password.text are null, then the second if checks if they are equal to your desired values. It is impossible for both if conditions to pass.

I think you meant to check != null rather than == null

CodePudding user response:

TextEditingControler text cant be null, you can do empty check

  _showAppLogin() {
    if (name.text.isEmpty && password.text.isEmpty) {
      if (name.text == userName && password.text == passWord) {
        _showAlertDialog('Login Successful');
      } else {
        setState(() {
          _autovalidate = true;
        });
        _showAlertDialog('Invalid Credentials');
      }
    }
  }

CodePudding user response:

Remove the unnecessary null check from if (name.text == null && password.text == null) TextEditingController cannot be null because of Sound null safty.

And both of the if conditions Never satisfied

if (name.text == null && password.text == null) {
  if (name.text == userName && password.text == passWord) {
   ...
  }
}

You have to check if the TextEditingController .isNotEmpty instead of name.text == null

Change your function to this

if (name.text.isNotEmpty && password.text.isNotEmpty) {
    ....
}

CodePudding user response:

I think the issue is with the condition you have given,

if (name.text == null && password.text == null) {

The bottom condition will only be checked if the value is null; if name.text is null then it will not go inside and check if the value is equal to userName' and hence if it's null then there is no value to check.

  if (name.text == userName && password.text == passWord) {
  } else {
    ...
  }

Try changing the condition to:

if (name.text != null && password.text != null) {
  if (name.text == userName && password.text == passWord) {
  } else {
    ...
  }

Correct me if i am worng. Thank you.

  • Related