Commit 1404098a authored by Indika NK's avatar Indika NK

Login register part one added

parent 885a0ba4
import 'package:covidefender/home_screen.dart';
import 'package:firebase_auth/firebase_auth.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import '../main.dart';
class Register extends StatefulWidget {
// const Register({Key? key}) : super(key: key);
@override
_RegisterState createState() => _RegisterState();
}
class _RegisterState extends State<Register> {
final FirebaseAuth _auth = FirebaseAuth.instance;
final GlobalKey<FormState> _formKey = GlobalKey<FormState>();
String _email,
_password,
_name,
_address,
_NIC,
_phoneNumber,
_HomeNumber,
_awarenessLevel;
// check if the user is already logged in
checkAuthentification() async {
_auth.onAuthStateChanged.listen((user) {
if (user != null) {
Navigator.push(
context, MaterialPageRoute(builder: (context) => HomeScreen()));
}
});
}
@override
void initState() {
super.initState();
this.checkAuthentification(); //check every time
}
signUp() async {
//if validate ok then save current state
if (_formKey.currentState.validate()) _formKey.currentState.save();
try {
// create user with email and password
AuthResult result = await FirebaseAuth.instance
.createUserWithEmailAndPassword(email: _email, password: _password);
FirebaseUser user = result.user;
print("Firebase user"+user.toString());
// await AuthService(uid:user.uid).updateUserData("email", "name", "address", "NIC", "mobile", "homeNumber", "awarenessLevel");
if (user != null) {
UserUpdateInfo updateUser = UserUpdateInfo();
updateUser.displayName = _name;
user.updateProfile(updateUser);
// await FirebaseAuth.instance.currentUser.updateProfile(displayName: _name);
}
} catch (e) {
showError(e.errormessage);
}
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(),
body: SingleChildScrollView(
child: Container(
color: Colors.white,
child: Column(
children: [
Container(
// height: 250,
child: Image(
image: AssetImage("lib/assets/images/welcome2.gif"),
fit: BoxFit.contain,
),
),
Container(
// form container
child: Form(
key: _formKey,
child: Column(
children: [
Container(
child: TextFormField(
// validate the user inputs
validator: (input) {
print("Login input 1 :"+input);
if (input.isEmpty) return "Please Enter Email";
},
onChanged: (input) {
_email = input;
print("_email :"+_email);
},
decoration: InputDecoration(
labelText: 'Email',
prefixIcon: Icon(Icons.mail_outlined)),
// onSaved: (input) => _email = input,
),
),
Container(
child: TextFormField(
obscureText: true,
// validate the user inputs
validator: (input) {
if (input.length < 6)
return "Please Provide minimum 6 characters";
},
onChanged: (input) {
_password = input;
print("_pwd :"+_password);
},
decoration: InputDecoration(
labelText: 'Password',
fillColor: Colors.red,
prefixIcon: Icon(Icons.lock_open)),
// onSaved: (input) => _password = input,
),
),
Container(
child: TextFormField(
// validate the user inputs
validator: (input) {
if (input.isEmpty) return "Please Enter Name";
},
onChanged: (input) {
_name = input;
},
decoration: InputDecoration(
labelText: 'Full Name',
prefixIcon: Icon(Icons.mail_outlined)),
onSaved: (input) => _name = input,
),
),
Container(
child: TextFormField(
// validate the user inputs
validator: (input) {
if (input.isEmpty)
return "Please Enter Address";
},
decoration: InputDecoration(
labelText: 'Address',
prefixIcon: Icon(Icons.home)
),
onSaved: (input) => _address = input,
),
),
Container(
child: TextFormField(
// validate the user inputs
validator: (input) {
if (input.isEmpty )
return "Please Enter National ID card Number";
},
decoration: InputDecoration(
labelText: 'NIC Number',
prefixIcon: Icon(Icons.credit_card)
),
onSaved: (input) => _NIC = input,
),
),
Container(
child: TextFormField(
// validate the user inputs
validator: (input) {
if (input.isEmpty)
return "Please Enter Mobile Number";
},
decoration: InputDecoration(
labelText: 'Mobile Number',
prefixIcon: Icon(Icons.phone_android)
),
onSaved: (input) => _phoneNumber = input,
),
),
Container(
child: TextFormField(
decoration: InputDecoration(
labelText: 'Home Phone Number | Land Line',
prefixIcon: Icon(Icons.phone_paused_sharp)
),
onSaved: (input) => _HomeNumber = input,
),
),
Container(
child: TextFormField(
decoration: InputDecoration(
labelText: 'Your Awareness Level',
prefixIcon: Icon(Icons.notifications_paused_outlined)
),
onSaved: (input) => _awarenessLevel = input,
),
),
SizedBox(
height: 25,
),
Padding(
padding: const EdgeInsets.all(16.0),
child: SizedBox(
height: 60,
width: 300,
child: RaisedButton(
onPressed: ()
{
print("***********Register clicked**********");
signUp();
},
child: Text("Register",
style: TextStyle(
fontSize: 20.0,
fontWeight: FontWeight.bold,
color: Colors.white)),
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(10),
),
color: Colors.lightBlueAccent,
),
),
),
],
),
),
)
],
),
),
),
);
}
//Error message for login errors
void showError(String errormessage) {
print("***********SHOW ERROR**********");
print(errormessage);
showDialog(
context: context,
builder: (BuildContext context) {
return AlertDialog(
title: Text('Error'),
content: Text(errormessage),
actions: [
FlatButton(
onPressed: () {
Navigator.of(context).pop();
},
)
],
);
});
}
}
import 'dart:ui';
import 'package:covidefender/AuthConfigs/Register.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_signin_button/flutter_signin_button.dart';
import 'login.dart';
class Start extends StatefulWidget {
// const Start({Key? key}) : super(key: key);
@override
_StartState createState() => _StartState();
}
class _StartState extends State<Start> {
@override
Widget build(BuildContext context) {
return Scaffold(
body: SingleChildScrollView(
child: Container(
color: Colors.white,
child: Column(
children: [
SizedBox(
height: 20,
),
Container(
height: 250,
child: Image(
image: AssetImage("lib/assets/images/welcome.gif"),
fit: BoxFit.contain,
),
),
Padding(
padding: const EdgeInsets.all(18.0),
child: RichText(
text: TextSpan(
text: "Welcome To ",
style: TextStyle(
fontSize: 35.0,
fontWeight: FontWeight.bold,
color: Colors.red[200]),
children: <TextSpan>[
TextSpan(
text: "COVIDEFENDER",
style: TextStyle(
fontSize: 45.0,
fontWeight: FontWeight.bold,
color: Colors.lightBlueAccent))
])),
),
Text(
"Your Personal Defender Against COVID",
style: TextStyle(color: Colors.black54),
),
SizedBox(height: 20,),
Column(
children: [
SizedBox(
height: 60,
width: 300,
child: RaisedButton(
onPressed: () {
// go to login page
Navigator.push(
context, MaterialPageRoute(builder: (context) => Login()));
},
child: Text("SIGN IN",
style: TextStyle(
fontSize: 20.0,
fontWeight: FontWeight.bold,
color: Colors.white)),
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(10),
),
color: Colors.lightBlue,
),
),
// Text(
// "Dont have an account yet ?",
// style: TextStyle(color: Colors.black54),
// ),
SizedBox(height: 15,),
SizedBox(
height: 60,
width: 300,
child: RaisedButton(
onPressed: () {
// go to Register page
Navigator.push(
context, MaterialPageRoute(builder: (context) => Register()));
},
child: Text("Register Now",
style: TextStyle(
fontSize: 20.0,
fontWeight: FontWeight.bold,
color: Colors.white)),
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(10),
),
color: Colors.lightBlue,
),
),
SizedBox(height: 30,),
SizedBox(
height: 60,
width: 300,
child: SignInButton(
Buttons.Google,
text: "Sign up with Google",
onPressed: () {},
),
)
],
)
],
),
),
),
);
}
}
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:firebase_auth/firebase_auth.dart';
import '../home_screen.dart';
import '../main.dart';
class Login extends StatefulWidget {
// const Start({Key? key}) : super(key: key);
@override
_LoginState createState() => _LoginState();
}
class _LoginState extends State<Login> {
final FirebaseAuth _auth = FirebaseAuth.instance;
final GlobalKey<FormState> _formKey = GlobalKey<FormState>();
String _email, _password;
// check if the user is already logged in
checkAuthentification() async {
_auth.onAuthStateChanged.listen((user) {
if (user != null) {
Navigator.push(
context, MaterialPageRoute(builder: (context) => HomeScreen()));
}
});
}
@override
void initState() {
super.initState();
this.checkAuthentification(); // laounch everytime a user entered the app
}
// check the validation
login() async{
if(_formKey.currentState.validate()){
// if validation success save the current state with what user provided
_formKey.currentState.save();
try{
FirebaseUser user = (await _auth.signInWithEmailAndPassword(email: _email, password: _password)) as FirebaseUser;
// FirebaseUser user = result.user; 0.14.0
// UserCredential user = await _auth.signInWithEmailAndPassword(email: _email, password: _password);
}catch(e){
showError(e.errormessage);
}
}
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(),
body: SingleChildScrollView(
child: Container(
color: Colors.white,
child: Column(
children: [
Container(
// height: 250,
child: Image(
image: AssetImage("lib/assets/images/welcome2.gif"),
fit: BoxFit.contain,
),
),
Container(
// form container
child: Form(
key: _formKey,
child: Column(
children: [
Container(
child: TextFormField(
// validate the user inputs
validator: (input) {
if (input.isEmpty) return "Please Enter Email";
},
onChanged: (input) {
_email = input;
print("_email :"+_email);
},
decoration: InputDecoration(
labelText: 'Email',
prefixIcon: Icon(Icons.mail_outlined)),
// onSaved: (input) => _email = input,
),
),
Container(
child: TextFormField(
obscureText: true,
// validate the user inputs
validator: (input) {
if (input.length < 6)
return "Please Provide minimum 6 characters";
},
onChanged: (input) {
_password = input;
print("_pwd :"+_password);
},
decoration: InputDecoration(
labelText: 'Password',
fillColor: Colors.red,
prefixIcon: Icon(Icons.lock_open)),
// onSaved: (input) => _password = input,
),
),
SizedBox(
height: 25,
),
Padding(
padding: const EdgeInsets.all(16.0),
child: SizedBox(
height: 60,
width: 300,
child: RaisedButton(
onPressed: () {
login();
print("***********login clicked**********");
},
child: Text("Sign in",
style: TextStyle(
fontSize: 20.0,
fontWeight: FontWeight.bold,
color: Colors.white)),
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(10),
),
color: Colors.lightBlue,
),
),
),
],
),
),
)
],
),
),
),
);
}
//Error message for login errors
void showError(String errormessage) {
print("***********SHOW ERROR**********");
print(errormessage);
showDialog(context: context, builder: (BuildContext context){
return AlertDialog(
title: Text('Credential Error'),
content: Text(errormessage),
actions: [
FlatButton(onPressed: (){
Navigator.of(context).pop();
}, )
],
);
});
}
}
......@@ -3,13 +3,63 @@ import 'package:covidefender/SRouter/srouterDashboard.dart';
import 'package:covidefender/STracker/splash.dart';
import 'package:covidefender/EAnalyzer/Loading.dart';
import 'package:covidefender/loginPage.dart';
import 'package:firebase_auth/firebase_auth.dart';
import 'package:flutter/material.dart';
import 'AuthConfigs/StartScreen.dart';
class HomeScreen extends StatefulWidget {
_HomeScreenState createState() => _HomeScreenState();
}
class _HomeScreenState extends State<HomeScreen> {
final FirebaseAuth _auth = FirebaseAuth.instance;
bool isLoggedin = false;
FirebaseUser user;
@override
void initState() {
this.checkAuthentification();//check every time in homescreen
this.getUser();
}
// check if the user is already logged in
checkAuthentification() async {
print("***********checkAuthentification Running**********");
_auth.onAuthStateChanged.listen((user) {
if (user == null) {
Navigator.push(
context,
MaterialPageRoute(
builder: (context) =>
Start())); // push user to login register sc
}
});
}
// if user already logged in
getUser() async {
print("***********Get User Running**********");
FirebaseUser firebaseuser =
await _auth.currentUser(); // get current user from firebase
await firebaseuser?.reload();
firebaseuser = await _auth.currentUser();
if (firebaseuser != null) {
setState(() {
this.user = firebaseuser;
this.isLoggedin = true;
});
}
}
signOut() async{
_auth.signOut();
}
Widget build(BuildContext context) {
var size = MediaQuery.of(context).size;
return Scaffold(
......@@ -211,6 +261,32 @@ class _HomeScreenState extends State<HomeScreen> {
)),
],
),
),
Container(
child: SizedBox(
height: 60,
width: 300,
child: RaisedButton(
onPressed: (){
print("***********Sign out clicked**********");
signOut().whenComplete(() => Navigator.of(context)
.pushAndRemoveUntil(
MaterialPageRoute(
builder: (context) => Start()),(Route <dynamic> route) => false));
},
child: Text("Sign Out",
style: TextStyle(
fontSize: 20.0,
fontWeight: FontWeight.bold,
color: Colors.white)),
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(10),
),
color: Colors.lightBlue,
)
),
)
],
),
......
......@@ -39,6 +39,10 @@ dependencies:
rxdart: ^0.24.1
path_provider: ^1.6.11
flutter_local_notifications: ^1.4.4+1
flutter_signin_button: ^1.0.0
firebase_auth: ^0.16.0
# The following adds the Cupertino Icons font to your application.
......@@ -65,6 +69,8 @@ dev_dependencies:
sdk: flutter
dependency_overrides:
quiver: ^3.0.0
font_awesome_flutter: ^9.1.0
firebase_core : ^0.5.0
# For information on the generic Dart part of this file, see the
# following page: https://dart.dev/tools/pub/pubspec
......@@ -159,6 +165,11 @@ flutter:
- lib/assets/images/hospital.gif
- lib/assets/images/lab.gif
- lib/assets/images/notify.gif
- lib/assets/images/welcome.gif
- lib/assets/images/welcome2.gif
# To add assets to your application, add an assets section, like this:
# assets:
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment