How to Create a Simple Login Screen in Flutter - Step by Step
By Techmithu
Introduction
Almost every app you use starts with a login screen. WhatsApp, Facebook, Gmail, your bank app, and your university portal all ask you to sign in before you can use them. The login screen is the front door of an app. It asks for an email address and a password, checks that both are correct, and then opens the home screen.
Building a login screen is one of the best second projects for a Flutter beginner. Your first app shows text on a screen. A login screen teaches you the real skills that every app needs: text fields that take input from the user, buttons that run code when you tap them, checks that stop empty or wrong input, and moving from one screen to another. Once you can build a login screen, you can build a sign up screen, a feedback form, and a settings page, because they all use the same pieces.
This guide shows you how to create a simple login screen in Flutter step by step. You will build a clean screen with an email field, a password field with a show and hide button, a login button, error messages for wrong input, and a simple home screen that opens after a successful login. This is a learning project, so the login check uses a simple test email and password that you set yourself. It does not connect to a real server and it does not store real user accounts. That step comes later, after you learn Firebase Authentication. By the end of this guide, you will have a working login screen running on the emulator, and you will understand every line of code in it.
What You Will Learn
- What a login screen is and what parts it needs
- How to create a new Flutter project for the login screen
- How to add an email field and a password field with TextField
- How to hide the password with dots and add a show password button
- How to check the input with a Form and show error messages
- How to add a login button that checks the email and password
- How to show a message with a SnackBar when login fails
- How to open a home screen with Navigator after a successful login
- How to test the login screen on the emulator
- Common beginner mistakes and how to fix them
What Is a Login Screen and Why Build One
A login screen is the first screen of an app that asks the user to prove who they are. It has two main parts: a place to type an email address or username, and a place to type a password. When the user taps the login button, the app checks the two values. If they match a real account, the app opens the home screen. If they do not match, the app shows an error message and stays on the login screen.
A login screen matters for two reasons. First, it protects the user's data. Without it, anyone who picks up the phone could open the app and read private messages, photos, or bank details. Second, it is a skill that every app developer needs. Almost every app a client asks for has a login screen, so it is one of the first things you will build in a real job or a freelance project.
In this guide, the login check is simple on purpose. You will set one test email and one test password in the code, for example test@techmithu.com and 123456. When the user types those exact values, the app opens the home screen. Any other values show an error. This is not how a real app checks users. A real app sends the email and password to a server, or to a service such as Firebase Authentication, and the server checks them against a safe database. Never store real user passwords in your app code. This guide keeps the check simple so you can learn the screen first, and the Firebase step comes in a later guide.
What You Need Before You Start
You need three things before you start this project. First, Flutter and Android Studio must be installed and working on your laptop. If you have not installed them yet, read the guide "How to Install Flutter and Android Studio on Windows 11" on this blog first, and then come back here. Second, you need an emulator set up in Android Studio, or a real Android phone connected with USB debugging turned on. Third, you need the basic Flutter knowledge from the earlier guides: how to create a project, how to run it, and what a widget is. If you have already built the calculator app or the to-do app from this blog, you have everything you need.
The project in this guide uses only the widgets that come with Flutter. You do not need to add any package from pub.dev. That keeps the project simple and means nothing can break because of a package version.
Step 1: Create a New Flutter Project
Open Android Studio and create a new Flutter project. Click New Flutter Project, choose Flutter, and name the project login_screen_app. The project name must be in small letters with underscores, because Dart package names use this style. Choose a folder on your laptop where you want to save the project, and click Create. Android Studio will create the project and open the main file, lib/main.dart.
You can also create the project from the terminal with one command:
flutter create login_screen_app
Then open the folder in Android Studio or VS Code with this command:
cd login_screen_app
Run the project once before you change anything, so you know the setup works. Start the emulator, then run this command in the terminal:
flutter run
You should see the default Flutter counter app on the emulator. If it runs, your setup is correct. Now you can delete the counter code and build the login screen.
Step 2: Add an Email Field and a Password Field
Open lib/main.dart and remove the counter app code. The login screen is built with a StatefulWidget, because the screen changes while the user types. The text fields need to remember what the user typed, the password field needs to switch between hidden and visible, and the error messages need to appear and disappear. A StatelessWidget cannot remember these changes, so StatefulWidget is the right choice.
The two most important widgets in this step are TextField and TextEditingController. A TextField is the box the user types into. A TextEditingController is a small object that holds the text from the field, so your code can read it later when the login button is tapped. You need one controller for the email field and one for the password field.
Here is the basic structure of the screen. The full code for each step is explained in simple words below it, so you can type it yourself and understand it.
First, create the two controllers at the top of the state class:
final TextEditingController emailController = TextEditingController(); final TextEditingController passwordController = TextEditingController();
Then add the email field inside the build method:
TextField(
controller: emailController,
keyboardType: TextInputType.emailAddress,
decoration: InputDecoration(
labelText: 'Email',
hintText: 'Enter your email',
prefixIcon: Icon(Icons.email),
border: OutlineInputBorder(),
),
)
Each line has a clear job. The controller line connects the field to the controller, so you can read the email later. The keyboardType line tells the phone to show the email keyboard, which has the @ sign easy to reach. The labelText is the small title above the field. The hintText is the grey example text inside the field before the user types. The prefixIcon adds a small email icon at the left side. The border line draws a clean outline around the field.
The password field looks almost the same, with two differences. First, obscureText is set to true, which shows dots instead of letters, so nobody standing behind the user can read the password. Second, the keyboard type is normal text, because a password has no special keyboard. The password field also gets a small eye icon at the right side, which the next step turns into a show password button.
TextField(
controller: passwordController,
obscureText: true,
decoration: InputDecoration(
labelText: 'Password',
hintText: 'Enter your password',
prefixIcon: Icon(Icons.lock),
border: OutlineInputBorder(),
),
)
Put the two fields inside a Column widget, with a SizedBox of 16 pixels of space between them, so the screen looks clean and the fields do not touch each other. Add some padding around the whole Column, about 24 pixels, so the fields do not touch the edges of the screen. A login screen with space around the fields looks calm and professional. A screen where the fields touch the edges looks unfinished.
Step 3: Add a Show Password Button
Beginners often forget the show password button, but users expect it in every login screen. A user who types a long password wants to check it once before tapping login, and the eye icon lets them do that. Without it, one small typing mistake means a failed login and an annoyed user.
To add the button, you need a boolean variable that remembers whether the password is visible or hidden. Add this line near the controllers:
bool isPasswordVisible = false;
Then change the password field in two places. First, change obscureText from a fixed true to the opposite of the variable:
obscureText: !isPasswordVisible,
When isPasswordVisible is false, obscureText is true, so the password shows as dots. When the user taps the eye icon, isPasswordVisible becomes true, obscureText becomes false, and the password shows as normal letters.
Second, add a suffixIcon to the password field. A suffix icon sits at the right side of the field, and here it is a button with an eye icon:
suffixIcon: IconButton(
icon: Icon(
isPasswordVisible ? Icons.visibility : Icons.visibility_off,
),
onPressed: () {
setState(() {
isPasswordVisible = !isPasswordVisible;
});
},
),
The icon changes with the variable: an open eye when the password is visible, and a crossed eye when it is hidden. The setState call tells Flutter to rebuild the screen with the new value, so the change appears at once. This is the same setState you used in the calculator app to update the result on the screen. If you skip setState, the variable changes but the screen stays the same, and the button looks broken.
Step 4: Check the Input with a Form and Show Error Messages
A login screen must never accept empty fields. If the user taps login with an empty email box, the app should show a clear message under the field, such as "Please enter your email". This check is called validation, and Flutter has a built-in way to do it with a Form widget.
Wrap your Column of fields in a Form widget, and give the Form a key. The key is a GlobalKey of type FormState, and it lets your code ask the Form to check all the fields at once. Add this line near the controllers:
final GlobalKey<FormState> formKey = GlobalKey<FormState>();
Then change the two TextField widgets to TextFormField widgets. A TextFormField is a TextField with a validator built in. The validator is a small function that looks at the value the user typed and returns an error message if the value is wrong, or null if the value is fine. Null means no error.
For the email field, the validator checks two things: the field is not empty, and the value contains an @ sign. A real email always has an @ sign, so this simple check catches the most common mistake:
validator: (value) {
if (value == null || value.isEmpty) {
return 'Please enter your email';
}
if (!value.contains('@')) {
return 'Please enter a valid email';
}
return null;
},
For the password field, the validator checks that the field is not empty, and that the password is at least 6 characters long. A password shorter than 6 characters is too weak for any real app:
validator: (value) {
if (value == null || value.isEmpty) {
return 'Please enter your password';
}
if (value.length < 6) {
return 'Password must be at least 6 characters';
}
return null;
},
When the login button is tapped, your code calls formKey.currentState!.validate(). Flutter runs every validator, and if any validator returns a message, that message appears in red under its field. If all validators return null, validate returns true, and your code moves on to check the email and password. This is the same pattern the official Flutter docs show in the "Validate input with a Form" guide, so it is the correct way to do it.
Step 5: Add the Login Button and Check the Email and Password
Now add the login button below the password field. Use an ElevatedButton, which is the filled button with a background colour that Flutter recommends for the main action on a screen. Make the button wide, so it stretches across the screen like the text fields, and give it a clear label:
ElevatedButton(
onPressed: () {
if (formKey.currentState!.validate()) {
checkLogin();
}
},
child: Text('Login'),
)
The onPressed code runs when the user taps the button. First it runs the Form check from Step 4. If the fields are empty or wrong, the error messages appear and nothing else happens. If the fields pass, it calls a function named checkLogin, which you will write next.
The checkLogin function reads the text from the two controllers, trims the extra spaces from the email, and compares both values with the test email and password. Remember, this is a learning project, so the test values sit in the code. In a real app, this check would go to a server instead.
void checkLogin() {
String email = emailController.text.trim();
String password = passwordController.text;
if (email == 'test@techmithu.com' && password == '123456') {
Navigator.push(
context,
MaterialPageRoute(builder: (context) => HomeScreen()),
);
} else {
ScaffoldMessenger.of(context).showSnackBar(
SnackBar(
content: Text('Wrong email or password. Please try again.'),
),
);
}
}
If the values match, Navigator.push opens the home screen. Navigator is Flutter's way of moving between screens, and MaterialPageRoute is the standard slide animation that moves to the new screen. If the values do not match, a SnackBar appears at the bottom of the screen with a short message. A SnackBar is the small dark bar that appears for a few seconds and then disappears on its own. It is the right widget for a short message, because it does not block the screen and the user can try again at once.
The trim method on the email removes spaces at the start and end of the text. Users often type a space after their email by accident, because the phone keyboard adds one after a word. Without trim, test@techmithu.com with a space at the end would not match, and the user would see a wrong password message for a correct email. This is one of the most common beginner mistakes in a login screen, and one line of code fixes it.
Step 6: Create a Simple Home Screen
The home screen is a second screen that proves the login worked. It can be simple: a welcome message and a logout button that takes the user back to the login screen. Create a new StatelessWidget named HomeScreen in the same file, below the login screen code:
class HomeScreen extends StatelessWidget {
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('Home'),
),
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
Text(
'Welcome! You are logged in.',
style: TextStyle(fontSize: 20),
),
SizedBox(height: 20),
ElevatedButton(
onPressed: () {
Navigator.pop(context);
},
child: Text('Logout'),
),
],
),
),
);
}
}
The AppBar at the top shows the title Home and a back arrow that Flutter adds on its own. The body shows a welcome message in the middle of the screen and a logout button below it. Navigator.pop closes the home screen and takes the user back to the login screen, which is still open underneath. That is how Flutter navigation works: each new screen sits on top of the last one, and pop removes the top screen to reveal the one below.
In a real app, the home screen would show the user's name, their content, and a menu. For this learning project, the welcome message is enough, because the goal is to prove that the login check and the navigation both work.
Step 7: Test the Login Screen on the Emulator
Start the emulator and run the app with flutter run. First, test the empty fields. Tap the login button without typing anything. You should see "Please enter your email" under the email field and "Please enter your password" under the password field. Both messages should appear in red.
Next, test a wrong email. Type hello in the email field and 123456 in the password field, then tap login. You should see "Please enter a valid email" under the email field, because hello has no @ sign.
Next, test a short password. Type test@techmithu.com in the email field and 123 in the password field, then tap login. You should see "Password must be at least 6 characters" under the password field.
Next, test the show password button. Type a password, tap the eye icon, and check that the letters appear. Tap the eye icon again and check that the dots come back.
Next, test a wrong login. Type wrong@test.com and 123456, then tap login. The form check passes, because the email looks valid and the password is long enough, but the values do not match the test account. A SnackBar should appear at the bottom with the message "Wrong email or password. Please try again."
Finally, test a correct login. Type test@techmithu.com and 123456, then tap login. The home screen should open with the welcome message. Tap logout and check that you return to the login screen.
If all six tests pass, your login screen works. If one test fails, read the error in the terminal or in the Android Studio Run window. The most common cause is a missing comma or bracket in the code. Dart needs every bracket closed, and one missing bracket can stop the whole app. The terminal shows the line number of the problem, so open that line and check the brackets around it.
Common Beginner Mistakes
Using a StatelessWidget for the login screen. The login screen changes while the user types, so it must be a StatefulWidget. A StatelessWidget cannot hold the text controllers or the password visibility variable, and the screen will not update when the user taps the eye icon.
Forgetting setState in the eye icon button. The variable changes, but the screen does not rebuild without setState, so the eye icon and the password dots stay the same. Every change that must appear on the screen needs setState around it.
Using TextField instead of TextFormField. A plain TextField has no validator, so the Form cannot check it. If you use TextField, the empty field check never runs, and the login button tries to check an empty email. Use TextFormField inside a Form for every input that needs checking.
Forgetting the Form key. The Form needs a GlobalKey so the button can call validate. Without the key, there is no way to ask the Form to check the fields, and the validators never run.
Not trimming the email. A space at the end of the email makes a correct email look wrong. Always use .trim() on the email before you compare it. This one line saves a lot of confusion.
Storing real passwords in the code. The test email and password in this guide are only for learning. Never put a real user's password in your app code, and never send a password to a server without a safe connection. A real app uses Firebase Authentication or a backend server, and the password is checked on the server, not in the app.
Making the login button too small. A small button in the middle of the screen is hard to tap and looks unfinished. Make the button as wide as the text fields, with clear padding, so it is easy to tap and easy to find.
Skipping the error message. When login fails and nothing happens, the user does not know what went wrong. Always show a message, either under the field or in a SnackBar, so the user knows what to fix.
Try It Yourself Checklist
- Create a new Flutter project named login_screen_app and run it once
- Add an email field with a controller and a password field with obscureText set to true
- Add the eye icon button that shows and hides the password
- Wrap the fields in a Form with a GlobalKey and add a validator to each field
- Add a login button that runs the Form check and then checks the test email and password
- Show a SnackBar when the login fails and open a home screen when it succeeds
- Test all six cases on the emulator: empty fields, wrong email, short password, show password, wrong login, and correct login
- Change the test email and password to your own values and test again
Conclusion
A login screen looks simple, but it teaches you the core skills of app development. You learned how to take input with TextField and TextEditingController, how to hide a password and add a show password button, how to check input with a Form and validators, how to show error messages and a SnackBar, and how to move between screens with Navigator. These are the same pieces you will use in a sign up screen, a feedback form, a profile page, and almost every screen a client asks for.
The next step is to make the login real. Learn Firebase Authentication, which checks users safely on a server and stores no passwords in your app code. After that, you can add a sign up screen, a forgot password link, and a remember me option that keeps the user logged in. Each of these builds on the screen you made in this guide.
If you found this guide useful, read the related articles on this blog next. The calculator app guide shows you how to build calculation logic with setState, and the to-do app guide shows you how to save data on the phone so it stays after the app closes. Together with this login screen, those three projects cover the main skills of a beginner Flutter developer.
Related articles:
- How to Create a Simple Calculator App in Flutter - Step by Step
- How to Create a Simple To-Do App in Flutter - Step by Step
- What is Flutter? Simple Explanation for Beginners
- How to Create Your First Flutter App - Hello World in 10 Minutes
- How to Install Flutter and Android Studio on Windows 11 - Complete Guide for Beginners
Frequently Asked Questions (FAQs)
What is a login screen in Flutter?
A login screen is the first screen of an app that asks the user for an email address and a password. It checks the two values, and if they are correct, it opens the home screen. In Flutter, it is built with TextField widgets, a Form for checking the input, and Navigator for moving to the next screen.
How do I hide the password in a Flutter login screen?
Set obscureText to true on the password TextField. This shows dots instead of letters. Add an eye icon button that changes a boolean variable with setState, so the user can tap the eye to show the password and tap again to hide it.
How do I check if the email and password are correct in Flutter?
Use a Form widget with a GlobalKey and a validator on each TextFormField. The validator checks that the fields are not empty and that the email has an @ sign. When the login button is tapped, call formKey.currentState!.validate(). If it returns true, compare the values with the test email and password, or send them to a server in a real app.
How do I show an error message when login fails in Flutter?
Use a SnackBar for a short message at the bottom of the screen, with ScaffoldMessenger.of(context).showSnackBar. For a wrong value in one field, return an error message from that field's validator, and Flutter shows it in red under the field.
How do I open a new screen after login in Flutter?
Use Navigator.push with a MaterialPageRoute. This opens the home screen with a slide animation. To go back, use Navigator.pop, which closes the top screen and returns to the login screen.
Can I use a login screen without Firebase?
Yes, for learning you can check a test email and password in the code, as this guide shows. For a real app with real users, you need a server or a service such as Firebase Authentication, because real passwords must never sit in the app code.
What is the difference between TextField and TextFormField in Flutter?
A TextField is a simple input box. A TextFormField is a TextField with a validator built in, so it works inside a Form. Use TextFormField when you need to check the input, such as in a login screen, and TextField when you only need to read the text.
How do I add a forgot password link to a login screen?
Add a TextButton under the login button with the label "Forgot password?". When the user taps it, open a new screen with an email field and a button that sends a reset link. In a real app, the reset link is sent by Firebase Authentication or by your server.




Comments
Post a Comment