10: Create a User Profile Interface using Fire-base.
[Link]
import 'package:firebase_core/firebase_core.dart';
import 'package:flutter/[Link]';
import 'package:lab10/[Link]';
void main() async{
[Link]();
await [Link]();
runApp(MyApp());
}
class MyApp extends StatelessWidget {
final Future<FirebaseApp> _initialization = [Link]();
@override
Widget build(BuildContext context) {
return FutureBuilder(
future: _initialization,
builder: (context, snapshot) {
// Check for Errors
if ([Link]) {
print("Something Went Wrong");
}
if ([Link] == [Link]) {
return Center(child: CircularProgressIndicator());
}
return MaterialApp(
title: 'Flutter Firebase EMail Password Auth',
theme: ThemeData(
primarySwatch: [Link],
),
debugShowCheckedModeBanner: false,
home: Login(),
);
});
}
}
[Link]
import 'package:firebase_auth/firebase_auth.dart';
import 'package:flutter/[Link]';
import 'package:lab10/[Link]';
class Signup extends StatefulWidget {
Signup({Key? key}) : super(key: key);
@override
_SignupState createState() => _SignupState();
}
class _SignupState extends State<Signup> {
final _formKey = GlobalKey<FormState>();
var email = "";
var password = "";
var confirmPassword = "";
// Create a text controller and use it to retrieve the current value
// of the TextField.
final emailController = TextEditingController();
final passwordController = TextEditingController();
final confirmPasswordController = TextEditingController();
@override
void dispose() {
// Clean up the controller when the widget is disposed.
[Link]();
[Link]();
[Link]();
[Link]();
}
registration() async {
if (password == confirmPassword) {
try {
UserCredential userCredential = await [Link]
.createUserWithEmailAndPassword(email: email, password: password);
print(userCredential);
[Link](context).showSnackBar(
SnackBar(
backgroundColor: [Link],
content: Text(
"Registered Successfully. Please Login..",
style: TextStyle(fontSize: 20.0),
),
),
);
[Link](
context,
MaterialPageRoute(
builder: (context) => Login(),
),
);
} on FirebaseAuthException catch (e) {
if ([Link] == 'weak-password') {
print("Password Provided is too Weak");
[Link](context).showSnackBar(
SnackBar(
backgroundColor: [Link],
content: Text(
"Password Provided is too Weak",
style: TextStyle(fontSize: 18.0, color: [Link]),
),
),
);
} else if ([Link] == 'email-already-in-use') {
print("Account Already exists");
[Link](context).showSnackBar(
SnackBar(
backgroundColor: [Link],
content: Text(
"Account Already exists",
style: TextStyle(fontSize: 18.0, color: [Link]),
),
),
);
}
}
} else {
print("Password and Confirm Password doesn't match");
[Link](context).showSnackBar(
SnackBar(
backgroundColor: [Link],
content: Text(
"Password and Confirm Password doesn't match",
style: TextStyle(fontSize: 16.0, color: [Link]),
),
),
);
}
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text("User SignUp"),
),
body: Form(
key: _formKey,
child: Padding(
padding: [Link](vertical: 20, horizontal: 30),
child: ListView(
children: [
Container(
margin: [Link](vertical: 10.0),
child: TextFormField(
autofocus: false,
decoration: InputDecoration(
labelText: 'Email: ',
labelStyle: TextStyle(fontSize: 20.0),
border: OutlineInputBorder(),
errorStyle:
TextStyle(color: [Link], fontSize: 15),
),
controller: emailController,
validator: (value) {
if (value == null || [Link]) {
return 'Please Enter Email';
} else if () {
return 'Please Enter Valid Email';
}
return null;
},
),
),
Container(
margin: [Link](vertical: 10.0),
child: TextFormField(
autofocus: false,
obscureText: true,
decoration: InputDecoration(
labelText: 'Password: ',
labelStyle: TextStyle(fontSize: 20.0),
border: OutlineInputBorder(),
errorStyle:
TextStyle(color: [Link], fontSize: 15),
),
controller: passwordController,
validator: (value) {
if (value == null || [Link]) {
return 'Please Enter Password';
}
return null;
},
),
),
Container(
margin: [Link](vertical: 10.0),
child: TextFormField(
autofocus: false,
obscureText: true,
decoration: InputDecoration(
labelText: 'Confirm Password: ',
labelStyle: TextStyle(fontSize: 20.0),
border: OutlineInputBorder(),
errorStyle:
TextStyle(color: [Link], fontSize: 15),
),
controller: confirmPasswordController,
validator: (value) {
if (value == null || [Link]) {
return 'Please Enter Password';
}
return null;
},
),
),
Container(
child: Row(
mainAxisAlignment: [Link],
children: [
ElevatedButton(
onPressed: () {
// Validate returns true if the form is valid, otherwise false.
if (_formKey.currentState!.validate()) {
setState(() {
email = [Link];
password = [Link];
confirmPassword = [Link];
});
registration();
}
},
child: Text(
'Sign Up',
style: TextStyle(fontSize: 18.0),
),
),
],
),
),
Container(
child: Row(
mainAxisAlignment: [Link],
children: [
Text("Already have an Account? "),
TextButton(
onPressed: () => {
[Link](
context,
PageRouteBuilder(
pageBuilder:
(context, animation1, animation2) =>
Login(),
transitionDuration: Duration(seconds: 0),
),
)
},
child: Text('Login'))
],
),
)
],
),
),
),
);
}
}
[Link]
import 'package:firebase_auth/firebase_auth.dart';
import 'package:flutter/[Link]';
import 'package:lab10/[Link]';
import 'package:lab10/[Link]';
class Login extends StatefulWidget {
Login({Key? key}) : super(key: key);
@override
_LoginState createState() => _LoginState();
}
class _LoginState extends State<Login> {
final _formKey = GlobalKey<FormState>();
var email = "";
var password = "";
// Create a text controller and use it to retrieve the current value
// of the TextField.
final emailController = TextEditingController();
final passwordController = TextEditingController();
userLogin() async {
try {
await [Link]
.signInWithEmailAndPassword(email: email, password: password);
[Link](
context,
MaterialPageRoute(
builder: (context) => Dashboard(),
),
);
} on FirebaseAuthException catch (e) {
if ([Link] == 'user-not-found') {
print("No User Found for that Email");
[Link](context).showSnackBar(
SnackBar(
backgroundColor: [Link],
content: Text(
"No User Found for that Email",
style: TextStyle(fontSize: 18.0, color: [Link]),
),
),
);
} else if ([Link] == 'wrong-password') {
print("Wrong Password Provided by User");
[Link](context).showSnackBar(
SnackBar(
backgroundColor: [Link],
content: Text(
"Wrong Password Provided by User",
style: TextStyle(fontSize: 18.0, color: [Link]),
),
),
);
}
}
}
@override
void dispose() {
// Clean up the controller when the widget is disposed.
[Link]();
[Link]();
[Link]();
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text("User Login"),
),
body: Form(
key: _formKey,
child: Padding(
padding: [Link](vertical: 20, horizontal: 30),
child: ListView(
children: [
Container(
margin: [Link](vertical: 10.0),
child: TextFormField(
autofocus: false,
decoration: InputDecoration(
labelText: 'Email: ',
labelStyle: TextStyle(fontSize: 20.0),
border: OutlineInputBorder(),
errorStyle:
TextStyle(color: [Link], fontSize: 15),
),
controller: emailController,
validator: (value) {
if (value == null || [Link]) {
return 'Please Enter Email';
} else if () {
return 'Please Enter Valid Email';
}
return null;
},
),
),
Container(
margin: [Link](vertical: 10.0),
child: TextFormField(
autofocus: false,
obscureText: true,
decoration: InputDecoration(
labelText: 'Password: ',
labelStyle: TextStyle(fontSize: 20.0),
border: OutlineInputBorder(),
errorStyle:
TextStyle(color: [Link], fontSize: 15),
),
controller: passwordController,
validator: (value) {
if (value == null || [Link]) {
return 'Please Enter Password';
}
return null;
},
),
),
Container(
margin: [Link](left: 60.0),
child: Row(
mainAxisAlignment: [Link],
children: [
ElevatedButton(
onPressed: () {
// Validate returns true if the form is valid, otherwise false.
if (_formKey.currentState!.validate()) {
setState(() {
email = [Link];
password = [Link];
});
userLogin();
}
},
child: Text(
'Login',
style: TextStyle(fontSize: 18.0),
),
),
TextButton(
onPressed: () => {
},
child: Text(
'Forgot Password ?',
style: TextStyle(fontSize: 14.0),
),
),
],
),
),
Container(
child: Row(
mainAxisAlignment: [Link],
children: [
Text("Don't have an Account? "),
TextButton(
onPressed: () => {
[Link](
context,
PageRouteBuilder(
pageBuilder: (context, a, b) => Signup(),
transitionDuration: Duration(seconds: 0),
),
(route) => false)
},
child: Text('Signup'),
),
],
),
)
],
),
),
),
);
}
}
[Link]
import 'package:flutter/[Link]';
class Dashboard extends StatelessWidget {
const Dashboard({Key? key}) : super(key: key);
@override
Widget build(BuildContext context) {
// Material App
return MaterialApp(
home:Scaffold(
appBar: AppBar(
title: Text("CMR INSTITUTE OF TECHNOLOGY"),
centerTitle: true,
backgroundColor: [Link],
),
body: Center(
child: Image(
image:
NetworkImage('[Link]
[Link]?w=731&ssl=1'),
),
)
));
}
}