Skip to main content

Creating a SignUp and Login UI in Flutter with Firebase authentication.

 Creating a SignUp and Login UI in Flutter with Firebase authentication involves several steps.


I'll provide a basic outline of the process, but please note that this is a simplified overview. You'll need to adjust and customize the code based on your specific requirements.

Step 1: Set up Flutter and Firebase

Install Flutter: Follow the instructions on the official Flutter website to install Flutter on your computer.

  1. Set up Firebase: Create a new project on the Firebase console (https://console.firebase.google.com/). Enable Firebase Authentication and follow the instructions to integrate Firebase with your Flutter project.
  2. Step 2: Design the UI

    1. Create a new Flutter project.
    2. Design the SignUp and Login screens using Flutter widgets like Scaffold, TextFormField, RaisedButton, etc.
  3. Step 3: Implement Firebase Authentication

    1. Initialize Firebase in your Flutter app by adding the necessary configurations in the main.dart file.
    2. Set up methods to handle user registration and login using Firebase Authentication.
    3. Use FirebaseAuth.instance methods to perform user registration and login with email and password.

    Here's an example of how you can implement SignUp and Login functionality using Firebase Authentication:

Dart file

import 'package:flutter/material.dart';
import 'package:firebase_auth/firebase_auth.dart';

void main() => runApp(MyApp());

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: AuthScreen(),
    );
  }
}

class AuthScreen extends StatefulWidget {
  @override
  _AuthScreenState createState() => _AuthScreenState();
}

class _AuthScreenState extends State<AuthScreen> {
  final FirebaseAuth _auth = FirebaseAuth.instance;
  final TextEditingController _emailController = TextEditingController();
  final TextEditingController _passwordController = TextEditingController();

  void _signUp() async {
    try {
      final UserCredential userCredential =
          await _auth.createUserWithEmailAndPassword(
        email: _emailController.text.trim(),
        password: _passwordController.text,
      );

      // You can do something with the userCredential here
    } catch (e) {
      print("Error during registration: $e");
    }
  }

  void _login() async {
    try {
      final UserCredential userCredential =
          await _auth.signInWithEmailAndPassword(
        email: _emailController.text.trim(),
        password: _passwordController.text,
      );

      // You can do something with the userCredential here
    } catch (e) {
      print("Error during login: $e");
    }
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('Authentication'),
      ),
      body: Padding(
        padding: EdgeInsets.all(16.0),
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: [
            TextFormField(
              controller: _emailController,
              decoration: InputDecoration(labelText: 'Email'),
            ),
            TextFormField(
              controller: _passwordController,
              decoration: InputDecoration(labelText: 'Password'),
              obscureText: true,
            ),
            SizedBox(height: 20),
            RaisedButton(
              onPressed: _signUp,
              child: Text('Sign Up'),
            ),
            RaisedButton(
              onPressed: _login,
              child: Text('Login'),
            ),
          ],
        ),
      ),
    );
  }
}

Comments