Last active
November 9, 2020 16:28
-
-
Save Moiz-Ali-Moomin/65fa103d26b0340a564b3c6ae232f61d to your computer and use it in GitHub Desktop.
Task 4 CMD
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import 'package:firebase_auth/firebase_auth.dart'; | |
import 'package:flutter/material.dart'; | |
import 'package:modal_progress_hud/modal_progress_hud.dart'; | |
class SignUpUsingEmail extends StatefulWidget { | |
@override | |
_SignUpUsingEmailState createState() => _SignUpUsingEmailState(); | |
} | |
class _SignUpUsingEmailState extends State<SignUpUsingEmail> { | |
final _formKey = GlobalKey<FormState>(); | |
FirebaseAuth firebaseAuth = FirebaseAuth.instance; | |
TextEditingController emailController = TextEditingController(); | |
TextEditingController passwordController = TextEditingController(); | |
bool isLoading = false; | |
registerUser({String email, String password}) async { | |
try { | |
await FirebaseAuth.instance | |
.createUserWithEmailAndPassword(email: email, password: password); | |
Navigator.pushNamedAndRemoveUntil(context, 'Home', (route) => false); | |
} on FirebaseAuthException catch (e) { | |
if (e.code == 'weak-password') { | |
print('The password provided is too weak.'); | |
_showDialog('Weak Password'); | |
setState(() { | |
isLoading = false; | |
}); | |
} else if (e.code == 'email-already-in-use') { | |
print('The account already exists for that email.'); | |
_showDialog('Account already exist'); | |
setState(() { | |
isLoading = false; | |
}); | |
} | |
} catch (e) { | |
print(e.toString()); | |
} | |
} | |
void _showDialog(String errorInfo) { | |
showDialog( | |
context: context, | |
builder: (BuildContext context) { | |
return AlertDialog( | |
title: Text("Error"), | |
content: Text(errorInfo), | |
actions: <Widget>[ | |
FlatButton( | |
onPressed: () { | |
Navigator.of(context).pop(); | |
}, | |
child: Text("Close")) | |
], | |
); | |
}); | |
} | |
@override | |
Widget build(BuildContext context) { | |
return ModalProgressHUD( | |
inAsyncCall: isLoading, | |
child: Scaffold( | |
resizeToAvoidBottomPadding: false, | |
appBar: AppBar( | |
title: Text("Registration Page"), backgroundColor: Colors.black), | |
body: Form( | |
key: _formKey, | |
child: SingleChildScrollView( | |
child: Column(children: <Widget>[ | |
SizedBox( | |
height: 120.0, | |
), | |
Text( | |
'Registration Page', | |
style: TextStyle(fontSize: 44.0, fontWeight: FontWeight.bold), | |
), | |
Column( | |
children: <Widget>[ | |
SizedBox( | |
height: 10, | |
), | |
Padding( | |
padding: EdgeInsets.fromLTRB(20, 0, 20, 0), | |
child: TextFormField( | |
controller: emailController, | |
decoration: InputDecoration( | |
labelText: 'EMAIL', | |
labelStyle: TextStyle( | |
fontFamily: 'Montserrat', | |
fontWeight: FontWeight.bold, | |
color: Colors.grey), | |
// hintText: 'EMAIL', | |
// hintStyle: , | |
focusedBorder: UnderlineInputBorder( | |
borderSide: BorderSide(color: Colors.green))), | |
validator: (value) { | |
if (value.isEmpty) { | |
return 'Enter Email id'; | |
} | |
return null; | |
}, | |
), | |
), | |
SizedBox(height: 10.0), | |
Padding( | |
padding: EdgeInsets.fromLTRB(20, 0, 20, 0), | |
child: TextFormField( | |
controller: passwordController, | |
obscureText: true, | |
decoration: InputDecoration( | |
labelText: 'PASSWORD ', | |
labelStyle: TextStyle( | |
fontFamily: 'Montserrat', | |
fontWeight: FontWeight.bold, | |
color: Colors.grey), | |
focusedBorder: UnderlineInputBorder( | |
borderSide: BorderSide(color: Colors.green))), | |
validator: (value) { | |
if (value.isEmpty) { | |
return 'Enter Password'; | |
} | |
return null; | |
}), | |
), | |
SizedBox(height: 50.0), | |
Container( | |
padding: EdgeInsets.fromLTRB(20, 0, 20, 0), | |
height: 40.0, | |
child: Material( | |
borderRadius: BorderRadius.circular(20.0), | |
shadowColor: Colors.greenAccent, | |
color: Colors.green, | |
elevation: 7.0, | |
child: GestureDetector( | |
onTap: () { | |
if (_formKey.currentState.validate()) { | |
registerUser( | |
email: emailController.text, | |
password: passwordController.text); | |
} | |
setState(() { | |
isLoading = true; | |
}); | |
Future.delayed(const Duration(seconds: 5), () { | |
if (mounted) { | |
setState(() { | |
isLoading = false; | |
}); | |
} | |
}); | |
}, | |
child: Center( | |
child: Text( | |
'SIGNUP', | |
style: TextStyle( | |
color: Colors.white, | |
fontWeight: FontWeight.bold, | |
fontFamily: 'Montserrat'), | |
), | |
), | |
), | |
)), | |
SizedBox(height: 20.0), | |
Container( | |
padding: EdgeInsets.fromLTRB(20, 0, 20, 0), | |
height: 40.0, | |
color: Colors.transparent, | |
child: Container( | |
decoration: BoxDecoration( | |
border: Border.all( | |
color: Colors.black, | |
style: BorderStyle.solid, | |
width: 1.0), | |
color: Colors.transparent, | |
borderRadius: BorderRadius.circular(20.0)), | |
child: InkWell( | |
onTap: () { | |
Navigator.of(context).pop(); | |
}, | |
child: Center( | |
child: Text('Go Back', | |
style: TextStyle( | |
fontWeight: FontWeight.bold, | |
fontFamily: 'Montserrat')), | |
), | |
), | |
), | |
), | |
], | |
), | |
]), | |
), | |
), | |
), | |
); | |
} | |
@override | |
void dispose() { | |
super.dispose(); | |
emailController.dispose(); | |
passwordController.dispose(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment