skip to Main Content

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);
  }
}

4

Answers


  1. TextEditingControler text cant be null, you can do empty check

      _showAppLogin() {
        if (name.text.isNotEmpty && password.text.isNotEmpty) {
          if (name.text == userName && password.text == passWord) {
            _showAlertDialog('Login Successful');
          } else {
            setState(() {
              _autovalidate = true;
            });
            _showAlertDialog('Invalid Credentials');
          }
        }
      }
    
    
    Login or Signup to reply.
  2. _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

    Login or Signup to reply.
  3. 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) {
        ....
    }
    
    Login or Signup to reply.
  4. 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.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search