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
TextEditingControler text cant be null, you can do empty check
This is not going to work. The first
if
checks if both name.text and password.text arenull
, then the secondif
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
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 satisfiedYou have to check if the
TextEditingController
.isNotEmpty
instead ofname.text == null
Change your function to this
I think the issue is with the condition you have given,
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.
Try changing the condition to:
Correct me if i am worng.
Thank you.