Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import 'package:blogging_app/Firebase/google_signIn.dart';
- import 'package:blogging_app/Firebase/main_tgglePage.dart';
- import 'package:blogging_app/Other_screens/home_page.dart';
- import 'package:firebase_auth/firebase_auth.dart';
- import 'package:flutter/material.dart';
- import 'package:font_awesome_flutter/font_awesome_flutter.dart';
- class LoginScreen extends StatefulWidget {
- final GoogleSignin googleSignin;
- LoginScreen({
- this.googleSignin,
- });
- @override
- _LoginScreenState createState() => _LoginScreenState();
- }
- class _LoginScreenState extends State<LoginScreen> {
- String errorText = "";
- final scaffoldKey = GlobalKey<ScaffoldState>();
- FirebaseUser userF;
- String state;
- ToggleStatus toggleStatus = ToggleStatus.unknown;
- @override
- void initState() {
- widget.googleSignin.currentUser().then((user) {
- setState(() {
- userF = user;
- user?.uid != null
- ? toggleStatus = ToggleStatus.signedIn
- : toggleStatus = ToggleStatus.signedOut;
- user?.photoUrl != null ? state = 'google' : state = 'firebase';
- });
- });
- super.initState();
- }
- @override
- Widget build(BuildContext context) {
- return toggleStatus == ToggleStatus.signedIn
- ? HomePage(
- state: state,
- user: userF,
- googleSignin: widget.googleSignin,
- )
- : Scaffold(
- backgroundColor: Colors.white,
- key: scaffoldKey,
- body: Hero(
- tag: 'nepal',
- child: Container(
- decoration: BoxDecoration(
- image: DecorationImage(
- image: AssetImage(
- "assets/images/bgLight.png",
- ),
- fit: BoxFit.contain),
- ),
- child: ListView(
- physics: BouncingScrollPhysics(),
- reverse: true,
- children: <Widget>[
- Padding(
- padding: const EdgeInsets.only(bottom: 60.0),
- child: LoginPage(
- scaffoldKey: scaffoldKey,
- googleSignin: widget.googleSignin,
- userF: userF,
- ),
- ),
- ],
- ))));
- }
- }
- class LoginPage extends StatefulWidget {
- final GlobalKey scaffoldKey;
- final GoogleSignin googleSignin;
- final FirebaseUser userF;
- LoginPage({
- this.scaffoldKey,
- this.googleSignin,
- this.userF,
- });
- @override
- _LoginPageState createState() => _LoginPageState();
- }
- class _LoginPageState extends State<LoginPage> {
- String errorText = "";
- final TextEditingController _pwController = TextEditingController();
- String _email = "";
- String _password = "";
- final _formKey = GlobalKey<FormState>();
- @override
- Widget build(BuildContext context) {
- return Column(children: <Widget>[
- Row(
- mainAxisAlignment: MainAxisAlignment.center,
- children: <Widget>[
- _raisedButton(" SignIn ", Icons.lock_open, () {
- _showForm(context, 0);
- }),
- SizedBox(
- width: 30.0,
- ),
- _raisedButton(" SignUp ", Icons.note_add, () {
- _showForm(context, 1);
- }),
- ],
- ),
- SizedBox(height: 30.0),
- _outlineButton(
- " Continue with Google",
- Icon(
- FontAwesomeIcons.google,
- color: Colors.red,
- ), () async {
- try {
- await widget.googleSignin.signIn().then((user) {
- Navigator.pushReplacement(
- context,
- MaterialPageRoute(
- builder: (context) => HomePage(
- googleSignin: widget.googleSignin,
- state: 'google',
- user: user,
- )));
- });
- } catch (e) {
- print(e.toString());
- }
- })
- ]);
- }
- Widget _raisedButton(String label, IconData icons, ontap()) {
- return SizedBox(
- height: 50.0,
- child: RaisedButton.icon(
- icon: Icon(
- icons,
- color: Colors.white,
- ),
- onPressed: () {
- ontap();
- },
- label: Text(
- label,
- ),
- shape: RoundedRectangleBorder(borderRadius: BorderRadius.circular(28)),
- ),
- );
- }
- Widget _outlineButton(String label, Icon icons, ontap()) {
- return SizedBox(
- height: 55.0,
- child: OutlineButton.icon(
- borderSide: BorderSide(color: Colors.red, width: 2.0),
- highlightedBorderColor: Colors.red,
- disabledBorderColor: Colors.green,
- padding: EdgeInsets.symmetric(horizontal: 25.0),
- icon: icons,
- onPressed: () {
- ontap();
- },
- label: Text(label, style: TextStyle(color: Colors.black)),
- shape: RoundedRectangleBorder(borderRadius: BorderRadius.circular(28)),
- ),
- );
- }
- Future _showForm(BuildContext context, int number) {
- return showDialog(
- context: context,
- builder: (context) {
- return Container(
- child: Dialog(
- child: Container(
- padding: EdgeInsets.symmetric(vertical: 10.0),
- decoration: BoxDecoration(
- borderRadius: BorderRadius.circular(300.0),
- ),
- child: Form(
- key: _formKey,
- child: ListView(
- shrinkWrap: true,
- physics: BouncingScrollPhysics(),
- padding:
- EdgeInsets.symmetric(vertical: 30.0, horizontal: 8.0),
- children: <Widget>[
- TextFormField(
- validator: (label) => label.contains("@gmail.com")
- ? null
- : "Enter valid email",
- onSaved: (label) => _email = label,
- decoration: InputDecoration(
- hintText: "Email",
- icon: Icon(
- Icons.email,
- color: Colors.grey,
- ),
- focusedBorder: UnderlineInputBorder(
- borderSide: BorderSide(color: Colors.black)),
- border: UnderlineInputBorder(),
- enabledBorder: UnderlineInputBorder(
- borderSide: BorderSide(color: Colors.grey),
- ),
- )),
- SizedBox(
- height: 10.0,
- ),
- TextFormField(
- validator: (label) => label.length >= 6
- ? null
- : "Try using longer password",
- onSaved: (password) => _password = password,
- controller: number == 1 ? _pwController : null,
- obscureText: true,
- decoration: InputDecoration(
- hintText: "Password",
- icon: Icon(
- Icons.lock_outline,
- color: Colors.grey,
- ),
- focusedBorder: UnderlineInputBorder(
- borderSide: BorderSide(color: Colors.black)),
- border: UnderlineInputBorder(),
- enabledBorder: UnderlineInputBorder(
- borderSide: BorderSide(color: Colors.grey),
- ),
- )),
- number == 1
- ? TextFormField(
- obscureText: true,
- validator: (confirm) =>
- confirm == _pwController.text
- ? null
- : "Password doesnt match",
- decoration: InputDecoration(
- hintText: "Confirm Password",
- icon: Icon(
- Icons.lock,
- color: Colors.grey,
- ),
- focusedBorder: UnderlineInputBorder(
- borderSide:
- BorderSide(color: Colors.black)),
- border: UnderlineInputBorder(),
- enabledBorder: UnderlineInputBorder(
- borderSide: BorderSide(color: Colors.grey),
- ),
- ))
- : Container(),
- SizedBox(
- height: 10.0,
- ),
- Padding(
- padding: const EdgeInsets.symmetric(horizontal: 25.0),
- child: SizedBox(
- height: 55.0,
- child: RaisedButton(
- color: Colors.blue.shade900,
- shape: RoundedRectangleBorder(
- borderRadius: BorderRadius.circular(25.0)),
- child: Text(
- number == 0 ? "LogIn" : "Create account",
- style: TextStyle(color: Colors.white)),
- onPressed: () => _firebaseLogin(0)),
- ),
- ),
- ],
- ),
- ),
- ),
- ),
- );
- });
- }
- Future _showAlert(String title, String email, int color) {
- return showDialog(
- context: context,
- child: AlertDialog(
- title:
- Center(child: Text(title, style: TextStyle(color: Colors.black))),
- content: Text(email,
- style: TextStyle(
- color: color == 1 ? Colors.black : Colors.red,
- fontWeight: FontWeight.bold)),
- actions: <Widget>[
- FlatButton(
- child: Text("OK"),
- onPressed: () {
- Navigator.pop(context);
- },
- )
- ],
- ),
- );
- }
- Future _firebaseLogin(int number) async {
- if (_formKey.currentState.validate()) {
- _formKey.currentState.save();
- if (number == 0) {
- try {
- await widget.googleSignin
- .firebaseSignIn(_email, _password)
- .then((user) {
- Navigator.pushReplacement(
- context,
- MaterialPageRoute(
- builder: (context) => HomePage(
- googleSignin: widget.googleSignin,
- state: 'firebase',
- user: user,
- )));
- });
- print("LoggedIn: $_email and $_password");
- } catch (e) {
- print(e.toString());
- if (e.toString().contains(
- 'The password is invalid or the user does not have a password.')) {
- setState(() {
- errorText = "Incorrect Password";
- });
- }
- if (e.toString().contains(
- 'There is no user record corresponding to this identifier. The user may have been deleted.')) {
- setState(() {
- errorText = "No user found corresponding to $_email";
- });
- }
- _showAlert("Error", errorText, 0);
- }
- } else {
- try {
- await widget.googleSignin.firebaseRegister(_email, _password);
- Navigator.pop(context);
- _showAlert(
- "Created user successfully. Now you can login with your account",
- _email,
- 1);
- print("Registered: $_email and $_password");
- } catch (e) {
- if (e.toString().contains(
- 'The email address is already in use by another account.')) {
- setState(() {
- errorText =
- "Email already in use by another user. Try using another email";
- });
- }
- _showAlert("Error", errorText, 0);
- }
- }
- _formKey.currentState.reset();
- } else {
- print("Error");
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement