Login screen in flutter

 

Mastering Login Screens in Flutter:

A Comprehensive Guide

Introduction:

Welcome, fellow programmers, to the exciting realm of Flutter development, where innovation meets creativity to shape the digital experiences of tomorrow. Today, we embark on a journey to unravel the mysteries of crafting seamless login screens - the cornerstone of user engagement in mobile applications. Whether you're a seasoned developer honing your skills or a curious enthusiast venturing into the world of Flutter, the art of building captivating login screens is a skill worth mastering. Join us as we explore the fundamental principles and practical techniques that will empower you to create login screens that not only captivate users but also instill confidence and trust in your app.

Throughout this guide, we'll navigate through the intricacies of Flutter's widget library, unlocking its potential to design visually stunning and intuitive user interfaces. We'll delve into the intricacies of authentication logic, ensuring that your app's login process is not only secure but also seamless and user-friendly.

1. Setting Up Your Project: Before diving into the code, let's ensure our development environment is set up correctly. We'll cover the initial steps of creating a new Flutter project and opening it in your preferred IDE. With everything in place, we'll be ready to start building our login screen.

2. Designing the UI: A well-designed UI is the cornerstone of any successful login screen. We'll explore the essential UI components needed for a login screen, such as text fields for username and password, along with a login button. Leveraging Flutter's rich widget library, we'll demonstrate how to style these components effectively to create a visually appealing interface.

3. Implementing Authentication Logic: With the UI in place, it's time to add the functionality that makes our login screen work. We'll delve into the implementation of authentication logic, including verifying user credentials and handling authentication errors. Using Flutter's state management capabilities, we'll ensure a seamless user experience throughout the authentication process.

4. Enhancing User Experience: To elevate our login screen to the next level, we'll explore various ways to enhance the user experience. This includes implementing features like password visibility toggling, input validation, and feedback mechanisms. By incorporating these enhancements, we'll create a more intuitive and user-friendly login experience for our app users.

5. Testing and Debugging: No development process is complete without thorough testing and debugging. We'll discuss the importance of testing our login screen on different devices and screen sizes to ensure compatibility and responsiveness. Additionally, we'll address common debugging techniques to troubleshoot any issues that may arise during development.


Functionality
   - The main functionality of this code is to create a simple login page where users can input their email and password.
   - Upon pressing the "Login" button, the `_signInWithEmailAndPassword` function is called to validate the entered email and password.
   - If the entered email and password match the hardcoded values ("kanushan@gmail.com" and "password" respectively), it navigates to the "Ball" page.
   - If the login fails, an error dialog is shown indicating that the username or password is incorrect.

Design
   - The design of the login page includes two text fields for entering email and password, along with appropriate hint text and icons.
   - The password field has a visibility toggle button to show or hide the entered password.
   - Error handling is implemented using an AlertDialog, which pops up when the login fails.
   - The design follows a simple layout with padding and spacing to make the UI clean and user-friendly.



//main.dart

import 'package:flutter/material.dart';


void main() {

  runApp(Sport());

}


class Sport extends StatelessWidget {

  @override

  Widget build(BuildContext context) {

    return MaterialApp(

      home: LoginPage(),

    );

  }

}


class LoginPage extends StatefulWidget {

  @override

  _LoginPageState createState() => _LoginPageState();

}


class _LoginPageState extends State<LoginPage> {

  bool textVisible = true;

  TextEditingController _emailController = TextEditingController();

  TextEditingController _passwordController = TextEditingController();


  void _signInWithEmailAndPassword(BuildContext context) {

    

    if (_emailController.text == "kanushan@gmail.com" &&

        _passwordController.text == "password") {

      // If login succeeds, navigate to the next page

      Navigator.push(

        context,

        MaterialPageRoute(builder: (context) => Ball()),

      );

    } else {

      // If login fails, show an error message

      showDialog(

        context: context,

        builder: (BuildContext context) {

          return AlertDialog(

            title: Text('Error'),

            content: Text('Failed to sign in: you are inputed wrong username or password'),

            actions: [

              TextButton(

                onPressed: () {

                  Navigator.of(context).pop();

                },

                child: Text('OK'),

              ),

            ],

          );

        },

      );

    }

  }


  @override

  Widget build(BuildContext context) {

    return Scaffold(

      appBar: AppBar(

        backgroundColor: Colors.orange,

        title: Text('Committee'),

      ),

      body: Padding(

        padding: EdgeInsets.all(20.0),

        child: Column(

          mainAxisAlignment: MainAxisAlignment.center,

          children: [

            TextField(

              controller: _emailController,

              decoration: InputDecoration(

                labelText: "User Name",

                labelStyle: TextStyle(color: Colors.blue),

                hintText: "Enter Your Email Address",

                hintStyle: TextStyle(color: Colors.grey),

                prefixIcon: Icon(Icons.person, color: Colors.grey),

                enabledBorder: OutlineInputBorder(

                  borderRadius: BorderRadius.circular(20.0),

                  borderSide: BorderSide(color: Colors.grey),

                ),

                focusedBorder: OutlineInputBorder(

                  borderRadius: BorderRadius.circular(20.0),

                  borderSide: BorderSide(color: Colors.grey),

                ),

                border: OutlineInputBorder(

                  borderRadius: BorderRadius.circular(20.0),

                  borderSide: BorderSide(color: Colors.grey),

                ),

              ),

              keyboardType: TextInputType.emailAddress,

            ),

            SizedBox(height: 20.0),

            TextField(

              controller: _passwordController,

              obscureText: textVisible,

              decoration: InputDecoration(

                labelText: "Password",

                labelStyle: TextStyle(color: Colors.blue),

                hintText: "Enter Your Password",

                hintStyle: TextStyle(color: Colors.grey),

                prefixIcon: Icon(Icons.password, color: Colors.grey),

                suffixIcon: IconButton(

                  onPressed: () {

                    setState(() {

                      textVisible = !textVisible;

                    });

                  },

                  icon: textVisible

                      ? Icon(Icons.visibility, color: Colors.red)

                      : Icon(Icons.visibility_off, color: Colors.red),

                ),

                enabledBorder: OutlineInputBorder(

                  borderRadius: BorderRadius.circular(20.0),

                  borderSide: BorderSide(color: Colors.grey),

                ),

                focusedBorder: OutlineInputBorder(

                  borderRadius: BorderRadius.circular(20.0),

                  borderSide: BorderSide(color: Colors.grey),

                ),

                border: OutlineInputBorder(

                  borderRadius: BorderRadius.circular(20.0),

                  borderSide: BorderSide(color: Colors.grey),

                ),

              ),

            ),

            SizedBox(height: 20.0),

            ElevatedButton(

              style: ElevatedButton.styleFrom(primary: Colors.orange),

              onPressed: () => _signInWithEmailAndPassword(context),

              child: Text(

                'Login',

                style: TextStyle(color: Colors.black),

              ),

            ),

          ],

        ),

      ),

    );

  }

}


// Define Ball widget or replace it with the appropriate widget

class Ball extends StatelessWidget {

  @override

  Widget build(BuildContext context) {

    return Scaffold(

      appBar: AppBar(

        title: Text('Ball Page'),

      ),

      body: Center(

        child: Text('Ball Page'),

      ),

    );

  }

}


Comments

Popular posts from this blog

Scanning