Main
Main
dart';
import 'package:flutter/material.dart';
import 'package:firebase_auth/firebase_auth.dart';
import 'package:loginfirebase3/profile_screen.dart';
void main() {
runApp(const MyApp());
}
@override
Widget build(BuildContext context) {
return MaterialApp(
home: MyHomePage(title: 'Flutter Demo Home Page'),
);
}
}
@override
State<MyHomePage> createState() => _MyHomePageState();
}
@override
Widget build(BuildContext context) {
return Scaffold(
body: FutureBuilder(
builder: (context, snapshot) {
if (snapshot.connectionState == ConnectionState.done) {
return LoginScreen();
}
return Center(
child: CircularProgressIndicator(),
);
},
),
);
}
}
@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,
),
),
),
),
],
),
);
}
}