Build a Login Page UI using TextField and ElevatedButton in Flutter
Creating a login screen is one of the first things you do when building a mobile app. In this tutorial, we’ll walk step-by-step through building a clean and responsive login UI using Flutter widgets like TextField
and ElevatedButton
.
π― What You Will Learn:
-
How to use
TextField
to accept user input (email and password) -
How to use
ElevatedButton
to create a login button -
How to structure your login screen using Flutter layout widgets
π Step 1: Create a New Flutter Project
First, open your terminal or IDE and run this command to create a new Flutter app:
Now, navigate into the project folder:
Open the project in your favorite code editor (like VS Code or Android Studio).
π Step 2: Clean Up the main.dart
File
Replace everything in the lib/main.dart
file with the following code:
β Explanation:
-
Google Advertisement
runApp()
starts your app. -
MyApp
is the root widget of the application. -
MaterialApp
provides the basic design style and routing. -
LoginPage
will be the screen we design next.
π§± Step 3: Build the LoginPage UI
Now let’s create the login UI. Replace or extend the code in the same file with this:
π Step-by-Step Code Explanation
πΉ 1. TextEditingController
-
These are used to control and get the input from
TextField
.
πΉ 2. TextField Widget
-
Used to take input from the user.
-
One
TextField
for email, another for password.
-
controller
: links the field to a variable. -
Google Advertisement
labelText
: label shown inside the field. -
border
: gives the field a border outline. -
keyboardType
: helps suggest a suitable keyboard (e.g., email).
πΉ 3. obscureText: true
-
Used in the password field to hide the input like ****.
πΉ 4. ElevatedButton
-
A material-style raised button.
-
Executes the
handleLogin()
function when clicked.
πΉ 5. handleLogin()
-
Fetches and prints the user input.
You can later replace print()
with authentication logic.
π± Final Output
When you run the app, you’ll see a clean login screen with:
-
Google Advertisement
Email field
-
Password field
-
Login button
On tapping the button, the email and password you enter will be printed in the console.
β Bonus Tips
-
You can add input validation to ensure fields are not empty.
-
You can navigate to another screen on successful login.
-
Wrap the
TextField
s andElevatedButton
inside aForm
widget for advanced validation.
π Conclusion
You’ve just built a simple login page UI in Flutter using TextField
and ElevatedButton
. This structure is a great starting point for adding real user authentication and improving your app’s UI.
Keep practicing with more Flutter widgets to create amazing mobile apps!