Facebook Twitter Instagram
    DeepCrazyWorld
    Facebook Twitter Instagram Pinterest YouTube
    • FLUTTER
      • FLUTTER APP
        • QRCode
        • Quiz App
        • Chat GPT
        • PDF App
        • News App
        • Fitness App
        • Weather App
        • BMI Calculator
        • GAME APP
        • Ecommerce App
        • wallpaper App
        • Finance app
        • Chat App
        • Wallet App
        • Taxi App
        • Quran app
        • Music player app
      • FLUTTER UI
        • Splash Screen
        • Onboarding Screen
        • Login Screen
        • Card Design
        • Drawer
    • PROJECT
      • Android Projects
      • College Projects
      • FLUTTER APP
      • Project Ideas
      • PHP Projects
      • Python Projects
    • SOURCE CODE
    • ANDROID
      • ANDROID APP
      • GAME APP
      • ANDROID STUDIO
    • MCQ
      • AKTU MCQ
        • RPA MCQ
        • COA MCQ
        • HPC MCQ
        • SPM MCQ
        • Renewable Energy All MCQ
        • Data Compression MCQ
        • Data Structure MCQ
        • Digital Image Processing MCQ
        • Software Engineering MCQ
        • Machine Learning MCQ
        • Artificial Intelligence MCQ
      • D PHARMA MCQ
        • Pharmaceutics – I MCQ
        • Pharmacognosy MCQ
        • Pharmaceutical Chemistry MCQ
        • Biochemistry and Clinical Pathology MCQ
        • Human Anatomy and Physiology MCQ
        • Heath Education and Community Pharmacy MCQ
    • INTERVIEW QUESTIONS
      • Flutter Interview Questions
      • INTERVIEW QUESTIONS
      • Python Interview Questions
      • Coding ninjas solution
    • MORE
      • WORDPRESS
        • SEO
        • TOP 10 WORDPRESS THEME
      • PRODUCTIVITY
      • Program
      • QUOTES
    DeepCrazyWorld
    Home»FLUTTER UI»How to make Login Screen with flutter with validation
    FLUTTER UI

    How to make Login Screen with flutter with validation

    DeepikaBy DeepikaAugust 24, 2024Updated:August 24, 2024No Comments5 Mins Read

    Creating a Login Screen with flutter with validation – Here’s a step-by-step guide to creating a login screen in Flutter with validation, using the provided code:

    Table of Contents

    Toggle
    • 1. Set Up Your Flutter Project
    • 2. Create the Login Screen
    • login_screen.dart
    • Output
    • 3. Understanding the Code
      • Imports and Setup
      • State Management
      • Password Visibility
      • UI Elements
      • Form Submission
      • Resource Cleanup
    • 4. Testing the Login Screen
    • 5. Additional Enhancements
    • Related Articles

    1. Set Up Your Flutter Project

    Make sure you have a Flutter project set up. If not, you can create a new Flutter project using:

    flutter create my_login_app
    cd my_login_app

    In lib/main.dart, update the home property of your MaterialApp to use the LoginScreen:

    import 'package:flutter/material.dart';
    import 'login_screen.dart'; // Import the login screen file

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

    class MyApp extends StatelessWidget {
    @override
    Widget build(BuildContext context) {
    return MaterialApp(
    title: 'Flutter Demo',
    theme: ThemeData(
    primarySwatch: Colors.blue,
    ),
    home: LoginScreen(), // Set LoginScreen as the home widget
    );
    }
    }

    2. Create the Login Screen

    Replace the content of lib/main.dart with the provided code or create a new Dart file (e.g., login_screen.dart) and add the following code to it:

    Here’s the updated code with the confirm password functionality:

    login_screen.dart

    import 'package:flutter/material.dart';
    
    class LoginScreen extends StatefulWidget {
      @override
      _LoginScreenState createState() => _LoginScreenState();
    }
    
    class _LoginScreenState extends State<LoginScreen> {
      final _formKey = GlobalKey<FormState>(); // Form key for validation
      final TextEditingController _firstNameController = TextEditingController();
      final TextEditingController _lastNameController = TextEditingController();
      final TextEditingController _passwordController = TextEditingController();
      final TextEditingController _confirmPasswordController = TextEditingController(); // New controller for confirm password
    
      bool _passwordVisible = false; // Password visibility toggle
      bool _confirmPasswordVisible = false; // Confirm password visibility toggle
    
      @override
      void initState() {
        super.initState();
        _passwordVisible = false;
        _confirmPasswordVisible = false;
      }
    
      @override
      Widget build(BuildContext context) {
        return Scaffold(
          appBar: AppBar(
            title: Text('Login Screen'),
          ),
          body: Padding(
            padding: const EdgeInsets.all(16.0),
            child: Form(
              key: _formKey,
              child: Column(
                children: <Widget>[
                  TextFormField(
                    controller: _firstNameController,
                    decoration: InputDecoration(labelText: 'First Name'),
                    validator: (value) {
                      if (value == null || value.isEmpty) {
                        return 'Please enter your first name';
                      }
                      return null;
                    },
                  ),
                  TextFormField(
                    controller: _lastNameController,
                    decoration: InputDecoration(labelText: 'Last Name'),
                    validator: (value) {
                      if (value == null || value.isEmpty) {
                        return 'Please enter your last name';
                      }
                      return null;
                    },
                  ),
                  TextFormField(
                    controller: _passwordController,
                    decoration: InputDecoration(
                      labelText: 'Password',
                      suffixIcon: IconButton(
                        icon: Icon(
                          _passwordVisible ? Icons.visibility : Icons.visibility_off,
                        ),
                        onPressed: () {
                          setState(() {
                            _passwordVisible = !_passwordVisible;
                          });
                        },
                      ),
                    ),
                    obscureText: !_passwordVisible,
                    validator: (value) {
                      if (value == null || value.isEmpty) {
                        return 'Please enter your password';
                      } else if (value.length < 6) {
                        return 'Password must be at least 6 characters long';
                      }
                      return null;
                    },
                  ),
                  TextFormField(
                    controller: _confirmPasswordController,
                    decoration: InputDecoration(
                      labelText: 'Confirm Password',
                      suffixIcon: IconButton(
                        icon: Icon(
                          _confirmPasswordVisible ? Icons.visibility : Icons.visibility_off,
                        ),
                        onPressed: () {
                          setState(() {
                            _confirmPasswordVisible = !_confirmPasswordVisible;
                          });
                        },
                      ),
                    ),
                    obscureText: !_confirmPasswordVisible,
                    validator: (value) {
                      if (value == null || value.isEmpty) {
                        return 'Please confirm your password';
                      } else if (value != _passwordController.text) {
                        return 'Passwords do not match';
                      }
                      return null;
                    },
                  ),
                  SizedBox(height: 20),
                  ElevatedButton(
                    onPressed: () {
                      if (_formKey.currentState!.validate()) {
                        // If the form is valid, display a snackbar or handle submission
                        ScaffoldMessenger.of(context).showSnackBar(
                          SnackBar(content: Text('Processing Data')),
                        );
                      }
                    },
                    child: Text('Submit'),
                  ),
                ],
              ),
            ),
          ),
        );
      }
    
      @override
      void dispose() {
        _firstNameController.dispose();
        _lastNameController.dispose();
        _passwordController.dispose();
        _confirmPasswordController.dispose();
        super.dispose();
      }
    }

    Output

    3. Understanding the Code

    Imports and Setup

    • import 'package:flutter/material.dart';: Imports Flutter’s material design package.

    State Management

    • final _formKey = GlobalKey<FormState>();: Creates a key to uniquely identify the form and manage validation.
    • TextEditingController: Controllers for managing text input and validation.

    Password Visibility

    • bool _passwordVisible and _confirmPasswordVisible: Booleans to toggle password visibility.

    UI Elements

    • TextFormField: Input fields for the first name, last name, password, and confirm password.
      • validator: Function to validate input and show error messages.
      • suffixIcon: An icon button to toggle the visibility of the password.

    Form Submission

    • ElevatedButton: A button to submit the form. It validates the form and shows a snackbar if successful.

    Resource Cleanup

    • dispose(): Dispose of controllers when they are no longer needed to free up resources.

    4. Testing the Login Screen

    • Run the App: Execute flutter run to see the login screen in action.
    • Validation: Try submitting the form with empty fields and incorrect passwords to test the validation logic.

    5. Additional Enhancements

    • Styling: Customize the appearance of input fields and buttons.
    • Error Handling: Implement more sophisticated error handling and feedback mechanisms.
    • Navigation: Add navigation to different screens based on successful login.

    This setup provides a solid foundation for a login screen in Flutter with basic validation and UI features.m password, and visibility toggling for both password fields.

    Related Articles

    • How to make Ludo app in Flutter with Source Code Step by step
    • How to make PDF Reader app in Flutter with Source Code Step by step
    • How to make QR Scanner app in Flutter with Source Code Step by step
    • How to Make a ToDo App with Flutter with source Code StepWise in 2024
    • What is package in Flutter (Dart) with example in 2024
    • What is class in Flutter(Dart) with example step by step
    • Advantage of Flutter with examples in 2024
    • Top 15 Amazing Applications Built with Flutter Framework
    • Specialized PDF reader designed specifically for music sheets
    • Christmas Quote Generator app built with flutter source code
    • How to make review and rating ui with flutter stepwise
    • Create custom social login button Google, Facebook and Apple ui

    Share. Facebook Twitter LinkedIn WhatsApp Telegram Pinterest Reddit Email
    Previous ArticleCreate custom social login button Google, Facebook and Apple ui in Flutter
    Next Article How to make ChatGpt App in flutter with source code Stepwise

    Related Posts

    Implementing a Dynamic FAQ Screen UI in Flutter Using ExpansionTile

    FLUTTER 5 Mins Read

    Creating an Instruction UI Screen in Flutter Application

    FLUTTER UI 7 Mins Read

    How to make ListView Builder Ui in flutter with Source Code

    FLUTTER UI 5 Mins Read

    Create a TabBar View in flutter with fully functional stepwise

    FLUTTER UI 6 Mins Read

    Leave A Reply Cancel Reply

    Recent Posts
    • Implementing a Dynamic FAQ Screen UI in Flutter Using ExpansionTile March 29, 2025
    • Creating an Instruction UI Screen in Flutter Application March 29, 2025
    • Animated Backgrounds in Flutter: A Complete Guide March 15, 2025
    • How to make Diary App using flutter stepwise using getx August 31, 2024
    • How to Create Music Player UI screen with fully functional in flutter August 30, 2024
    • How to make ListView Builder Ui in flutter with Source Code August 29, 2024
    • Create a TabBar View in flutter with fully functional stepwise August 28, 2024
    • How to create TabBar view in flutter with source code step wise August 27, 2024
    • How to make Heart rate measure app with Flutter stepwise August 26, 2024
    • How to make ChatGpt App in flutter with source code Stepwise August 25, 2024
    Facebook Twitter Instagram Pinterest YouTube
    • About
    • Contact
    • Disclaimer
    • Privacy Policy
    Copyright by DeepCrazyWorld © 2025

    Type above and press Enter to search. Press Esc to cancel.