Member-only story
Flutter: Basic Login Screen
How to show/hide password in TextFormField?
2 min readApr 7, 2020
A Basic login involves either username, Email sometimes both with a password. Here, We going to learn the following:
- Validate Email
- Show Error, warning, hint to TextField.
- Obscure Password.
- Update the state based on user interaction.
- Complete Code Gist.
Validate Email: To achieve this, We can simply use regex “^[a-zA-Z0–9.a-zA-Z0–9.!#$%&’*+-/=?^_`{|}~]+@[a-zA-Z0–9]+\.[a-zA-Z]+”. There are many other regexes for email validation and any of them can be used.
//
bool isValidEmail() {
if ((_email == null) || (_email.length == 0)) {
//if user did not typed aything in email box we will not show error
return true;
} return RegExp(r"^[a-zA-Z0-9.a-zA-Z0-9.!#$%&'*+-/=?^_`{|}~]+@[a-zA-Z0-9]+\.[a-zA-Z]+").hasMatch(_email);}
Show Error, warning, hint to TextField: To do this we can simply use InputDecoration of TextField like following:
decoration: InputDecoration(
labelText: "Email",
hintText: 'Enter your password',
errorText: isValidEmail() ? null : "Invalid Email Address"
)