Lab 10
Lab 10
main.dart
import 'package:firebase_core/firebase_core.dart';
import 'package:flutter/material.dart';
import 'package:lab10/login.dart';
signup.dart
import 'package:firebase_auth/firebase_auth.dart';
import 'package:flutter/material.dart';
import 'package:lab10/Login.dart';
@override
_SignupState createState() => _SignupState();
}
@override
void dispose() {
// Clean up the controller when the widget is disposed.
emailController.dispose();
passwordController.dispose();
confirmPasswordController.dispose();
super.dispose();
}
registration() async {
if (password == confirmPassword) {
try {
UserCredential userCredential = await FirebaseAuth.instance
.createUserWithEmailAndPassword(email: email, password: password);
print(userCredential);
ScaffoldMessenger.of(context).showSnackBar(
SnackBar(
backgroundColor: Colors.redAccent,
content: Text(
"Registered Successfully. Please Login..",
style: TextStyle(fontSize: 20.0),
),
),
);
Navigator.pushReplacement(
context,
MaterialPageRoute(
builder: (context) => Login(),
),
);
} on FirebaseAuthException catch (e) {
if (e.code == 'weak-password') {
print("Password Provided is too Weak");
ScaffoldMessenger.of(context).showSnackBar(
SnackBar(
backgroundColor: Colors.orangeAccent,
content: Text(
"Password Provided is too Weak",
style: TextStyle(fontSize: 18.0, color: Colors.black),
),
),
);
} else if (e.code == 'email-already-in-use') {
print("Account Already exists");
ScaffoldMessenger.of(context).showSnackBar(
SnackBar(
backgroundColor: Colors.orangeAccent,
content: Text(
"Account Already exists",
style: TextStyle(fontSize: 18.0, color: Colors.black),
),
),
);
}
}
} else {
print("Password and Confirm Password doesn't match");
ScaffoldMessenger.of(context).showSnackBar(
SnackBar(
backgroundColor: Colors.orangeAccent,
content: Text(
"Password and Confirm Password doesn't match",
style: TextStyle(fontSize: 16.0, color: Colors.black),
),
),
);
}
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text("User SignUp"),
),
body: Form(
key: _formKey,
child: Padding(
padding: EdgeInsets.symmetric(vertical: 20, horizontal: 30),
child: ListView(
children: [
Container(
margin: EdgeInsets.symmetric(vertical: 10.0),
child: TextFormField(
autofocus: false,
decoration: InputDecoration(
labelText: 'Email: ',
labelStyle: TextStyle(fontSize: 20.0),
border: OutlineInputBorder(),
errorStyle:
TextStyle(color: Colors.redAccent, fontSize: 15),
),
controller: emailController,
validator: (value) {
if (value == null || value.isEmpty) {
return 'Please Enter Email';
} else if (!value.contains('@')) {
return 'Please Enter Valid Email';
}
return null;
},
),
),
Container(
margin: EdgeInsets.symmetric(vertical: 10.0),
child: TextFormField(
autofocus: false,
obscureText: true,
decoration: InputDecoration(
labelText: 'Password: ',
labelStyle: TextStyle(fontSize: 20.0),
border: OutlineInputBorder(),
errorStyle:
TextStyle(color: Colors.redAccent, fontSize: 15),
),
controller: passwordController,
validator: (value) {
if (value == null || value.isEmpty) {
return 'Please Enter Password';
}
return null;
},
),
),
Container(
margin: EdgeInsets.symmetric(vertical: 10.0),
child: TextFormField(
autofocus: false,
obscureText: true,
decoration: InputDecoration(
labelText: 'Confirm Password: ',
labelStyle: TextStyle(fontSize: 20.0),
border: OutlineInputBorder(),
errorStyle:
TextStyle(color: Colors.redAccent, fontSize: 15),
),
controller: confirmPasswordController,
validator: (value) {
if (value == null || value.isEmpty) {
return 'Please Enter Password';
}
return null;
},
),
),
Container(
child: Row(
mainAxisAlignment: MainAxisAlignment.center,
children: [
ElevatedButton(
onPressed: () {
// Validate returns true if the form is valid, otherwise false.
if (_formKey.currentState!.validate()) {
setState(() {
email = emailController.text;
password = passwordController.text;
confirmPassword = confirmPasswordController.text;
});
registration();
}
},
child: Text(
'Sign Up',
style: TextStyle(fontSize: 18.0),
),
),
],
),
),
Container(
child: Row(
mainAxisAlignment: MainAxisAlignment.center,
children: [
Text("Already have an Account? "),
TextButton(
onPressed: () => {
Navigator.pushReplacement(
context,
PageRouteBuilder(
pageBuilder:
(context, animation1, animation2) =>
Login(),
transitionDuration: Duration(seconds: 0),
),
)
},
child: Text('Login'))
],
),
)
],
),
),
),
);
}
}
login.dart
import 'package:firebase_auth/firebase_auth.dart';
import 'package:flutter/material.dart';
import 'package:lab10/signup.dart';
import 'package:lab10/dashboard.dart';
@override
_LoginState createState() => _LoginState();
}
userLogin() async {
try {
await FirebaseAuth.instance
.signInWithEmailAndPassword(email: email, password: password);
Navigator.pushReplacement(
context,
MaterialPageRoute(
builder: (context) => Dashboard(),
),
);
} on FirebaseAuthException catch (e) {
if (e.code == 'user-not-found') {
print("No User Found for that Email");
ScaffoldMessenger.of(context).showSnackBar(
SnackBar(
backgroundColor: Colors.orangeAccent,
content: Text(
"No User Found for that Email",
style: TextStyle(fontSize: 18.0, color: Colors.black),
),
),
);
} else if (e.code == 'wrong-password') {
print("Wrong Password Provided by User");
ScaffoldMessenger.of(context).showSnackBar(
SnackBar(
backgroundColor: Colors.orangeAccent,
content: Text(
"Wrong Password Provided by User",
style: TextStyle(fontSize: 18.0, color: Colors.black),
),
),
);
}
}
}
@override
void dispose() {
// Clean up the controller when the widget is disposed.
emailController.dispose();
passwordController.dispose();
super.dispose();
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text("User Login"),
),
body: Form(
key: _formKey,
child: Padding(
padding: EdgeInsets.symmetric(vertical: 20, horizontal: 30),
child: ListView(
children: [
Container(
margin: EdgeInsets.symmetric(vertical: 10.0),
child: TextFormField(
autofocus: false,
decoration: InputDecoration(
labelText: 'Email: ',
labelStyle: TextStyle(fontSize: 20.0),
border: OutlineInputBorder(),
errorStyle:
TextStyle(color: Colors.redAccent, fontSize: 15),
),
controller: emailController,
validator: (value) {
if (value == null || value.isEmpty) {
return 'Please Enter Email';
} else if (!value.contains('@')) {
return 'Please Enter Valid Email';
}
return null;
},
),
),
Container(
margin: EdgeInsets.symmetric(vertical: 10.0),
child: TextFormField(
autofocus: false,
obscureText: true,
decoration: InputDecoration(
labelText: 'Password: ',
labelStyle: TextStyle(fontSize: 20.0),
border: OutlineInputBorder(),
errorStyle:
TextStyle(color: Colors.redAccent, fontSize: 15),
),
controller: passwordController,
validator: (value) {
if (value == null || value.isEmpty) {
return 'Please Enter Password';
}
return null;
},
),
),
Container(
margin: EdgeInsets.only(left: 60.0),
child: Row(
mainAxisAlignment: MainAxisAlignment.center,
children: [
ElevatedButton(
onPressed: () {
],
),
)
],
),
),
),
);
}
}
dashboard.dart
import 'package:flutter/material.dart';
@override
Widget build(BuildContext context) {
// Material App
return MaterialApp(
home:Scaffold(
appBar: AppBar(
title: Text("CMR INSTITUTE OF TECHNOLOGY"),
centerTitle: true,
backgroundColor: Colors.deepOrange,
),
body: Center(
child: Image(
image:
NetworkImage('https://i0.wp.com/cmrithyderabad.edu.in/wp-content/uploads/2021/09/
cropped-CMR-IT-logo-1.webp?w=731&ssl=1'),
),
)
));
}
}