Google Advertisement

Simple Flutter Login UI with TextField and Button – Step-by-Step Guide

Google Advertisement
πŸ”₯ Read with Full Features on Our Website

Learn how to design a clean and functional login screen in Flutter using TextField and ElevatedButton. Follow this beginner-friendly guide with complete code and easy explanations.

Published on 04 May 2025
By Vandu

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:


πŸš€ Step 1: Create a New Flutter Project

First, open your terminal or IDE and run this command to create a new Flutter app:

flutter create login_ui

Now, navigate into the project folder:

cd login_ui

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:

import 'package:flutter/material.dart';

void main() {
  runApp(const MyApp());
}

class MyApp extends StatelessWidget {
  const MyApp({super.key});

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      debugShowCheckedModeBanner: false,
      title: 'Login UI',
      home: const LoginPage(),
    );
  }
}

βœ… Explanation:


🧱 Step 3: Build the LoginPage UI

Now let’s create the login UI. Replace or extend the code in the same file with this:

class LoginPage extends StatefulWidget {
  const LoginPage({super.key});

  @override
  State<LoginPage> createState() => _LoginPageState();
}

class _LoginPageState extends State<LoginPage> {
  final TextEditingController emailController = TextEditingController();
  final TextEditingController passwordController = TextEditingController();

  void handleLogin() {
    String email = emailController.text;
    String password = passwordController.text;

    // For now, just print the values
    print('Email: $email');
    print('Password: $password');
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: const Text('Login'),
        centerTitle: true,
      ),
      body: Padding(
        padding: const EdgeInsets.all(16.0),
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: [
            // Email Field
            TextField(
              controller: emailController,
              decoration: const InputDecoration(
                labelText: 'Email',
                border: OutlineInputBorder(),
              ),
              keyboardType: TextInputType.emailAddress,
            ),
            const SizedBox(height: 16),

            // Password Field
            TextField(
              controller: passwordController,
              decoration: const InputDecoration(
                labelText: 'Password',
                border: OutlineInputBorder(),
              ),
              obscureText: true,
            ),
            const SizedBox(height: 24),

            // Login Button
            SizedBox(
              width: double.infinity,
              child: ElevatedButton(
                onPressed: handleLogin,
                child: const Text('Login'),
              ),
            ),
          ],
        ),
      ),
    );
  }
}

πŸ“ Step-by-Step Code Explanation

πŸ”Ή 1. TextEditingController

final TextEditingController emailController = TextEditingController();
final TextEditingController passwordController = TextEditingController();

πŸ”Ή 2. TextField Widget

TextField(
  controller: emailController,
  decoration: InputDecoration(
    labelText: 'Email',
    border: OutlineInputBorder(),
  ),
)

πŸ”Ή 3. obscureText: true

πŸ”Ή 4. ElevatedButton

ElevatedButton(
  onPressed: handleLogin,
  child: Text('Login'),
)

πŸ”Ή 5. handleLogin()

void handleLogin() {
  String email = emailController.text;
  String password = passwordController.text;

  print('Email: $email');
  print('Password: $password');
}

You can later replace print() with authentication logic.


πŸ“± Final Output

When you run the app, you’ll see a clean login screen with:

On tapping the button, the email and password you enter will be printed in the console.


βœ… Bonus Tips


πŸ”š 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!

❀️ Like πŸ’¬ Comment πŸ”— Share
Google Advertisement
πŸ‘‰ View Full Version on Main Website β†—
Google Advertisement
πŸ‘‰ Read Full Article on Website