0% found this document useful (0 votes)
36 views3 pages

Main

This document contains code for a Flutter login screen application that uses Firebase authentication. It initializes Firebase, builds a login screen widget with email/password fields, and includes a login button that signs in with Firebase Auth and navigates to a profile screen if successful.

Uploaded by

Khoa Đặng
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
36 views3 pages

Main

This document contains code for a Flutter login screen application that uses Firebase authentication. It initializes Firebase, builds a login screen widget with email/password fields, and includes a login button that signs in with Firebase Auth and navigates to a profile screen if successful.

Uploaded by

Khoa Đặng
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 3

import 'package:firebase_core/firebase_core.

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

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

class MyApp extends StatelessWidget {


const MyApp({super.key});

@override
Widget build(BuildContext context) {
return MaterialApp(
home: MyHomePage(title: 'Flutter Demo Home Page'),
);
}
}

class MyHomePage extends StatefulWidget {


const MyHomePage({super.key, required this.title});

final String title;

@override
State<MyHomePage> createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {


Future<FirebaseApp> _initializeFirebase() async {
FirebaseApp firebaseApp = await Firebase.initializeApp();
return firebaseApp;
}

@override
Widget build(BuildContext context) {
return Scaffold(
body: FutureBuilder(
builder: (context, snapshot) {
if (snapshot.connectionState == ConnectionState.done) {
return LoginScreen();
}
return Center(
child: CircularProgressIndicator(),
);
},
),
);
}
}

class LoginScreen extends StatefulWidget {


const LoginScreen({super.key});

@override
State<LoginScreen> createState() => _LoginScreenState();
}
class _LoginScreenState extends State<LoginScreen> {
static Future<User?> loginUsingEmailPassword(
{required String email,
required String password,
required BuildContext context}) async {
FirebaseAuth auth = FirebaseAuth.instance;
User? user;
try {
UserCredential userCredential = await auth.signInWithEmailAndPassword(
email: email, password: password);
user = userCredential.user;
} on FirebaseAuthException catch (e) {
if (e.code == "user-not-found") {
print("No User found for that email");
}
}
return user;
}

@override
Widget build(BuildContext context) {
TextEditingController _emailController = TextEditingController();
TextEditingController _passwordController = TextEditingController();
return Padding(
padding: EdgeInsets.all(16.0),
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Text(
"MyApp Title",
style: TextStyle(
color: Colors.black,
fontSize: 28.0,
fontWeight: FontWeight.bold,
),
),
Text(
"Login to Your App",
style: TextStyle(
color: Colors.black,
fontSize: 44.0,
fontWeight: FontWeight.bold,
),
),
SizedBox(
height: 44.0,
),
TextField(
controller: _emailController,
keyboardType: TextInputType.emailAddress,
decoration: InputDecoration(
hintText: "User Email",
prefixIcon: Icon(
Icons.mail,
color: Colors.black,
)),
),
SizedBox(
height: 8.0,
),
TextField(
controller: _passwordController,
obscureText: true,
decoration: InputDecoration(
hintText: "User Password",
prefixIcon: Icon(
Icons.lock,
color: Colors.black,
)),
),
SizedBox(
height: 12.0,
),
Text(
"Don't Remember your Password?",
style: TextStyle(color: Colors.blue),
),
const SizedBox(
height: 88.0,
),
Container(
width: double.infinity,
child: RawMaterialButton(
fillColor: Color(0xFF0069FE),
elevation: 0.0,
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(12.0)),
onPressed: () async {
User? user = await loginUsingEmailPassword(
email: _emailController.text,
password: _passwordController.text,
context: context);
print(user);
if (user != null) {
Navigator.of(context).pushReplacement(MaterialPageRoute(
builder: (context) => ProfileScreen(),
));
}
},
child: Text(
"Login",
style: TextStyle(
color: Colors.white,
fontSize: 18.0,
),
),
),
),
],
),
);
}
}

You might also like