Advertisement
lockzackary

Untitled

Mar 14th, 2018
977
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Dart 3.81 KB | None | 0 0
  1. import 'package:flutter/material.dart';
  2. import 'package:font_awesome_flutter/font_awesome_flutter.dart';
  3. import 'package:shared_preferences/shared_preferences.dart';
  4. import 'package:firebase_auth/firebase_auth.dart';
  5. import 'package:firebase_database/firebase_database.dart';
  6. import '../util/authentication.dart' as auth;
  7.  
  8. bool buttons_visible = false;
  9. bool loader_visible = true;
  10. FirebaseUser _firebaseUser = null;
  11. final databaseReference = FirebaseDatabase.instance.reference();
  12.  
  13. class Login extends StatefulWidget {
  14.   @override
  15.   LoginState createState() => new LoginState();
  16. }
  17.  
  18. class LoginState extends State<Login> {
  19.   @override
  20.   void initState() {
  21.     super.initState();
  22.     _checkAuthStatus();
  23.   }
  24.  
  25.   _swapvisibility(){
  26.     setState((){
  27.       buttons_visible = !buttons_visible;
  28.       loader_visible = !loader_visible;
  29.     });
  30.   }
  31.  
  32.   _checkAuthStatus() async {
  33.     SharedPreferences prefs = await SharedPreferences.getInstance();
  34.     print('will attempt auto sign in');
  35.     _performSignin(prefs.getString("auth_provider"));
  36.   }
  37.  
  38.   _performSignin(String auth_provider) async {
  39.     if(auth_provider==null){
  40.       print('no auth info yet');
  41.       _swapvisibility();
  42.       return;
  43.     }
  44.  
  45.     print('display buttons');
  46.     _swapvisibility();
  47.  
  48.     if (auth_provider == 'facebook') _firebaseUser = await auth.signInWithFacebook();
  49.     if (auth_provider == 'google') _firebaseUser = await auth.signInWithGoogle();
  50.   }
  51.  
  52.   var linearGradient = new BoxDecoration(
  53.     gradient: new LinearGradient(
  54.       begin: FractionalOffset.centerRight,
  55.       end: FractionalOffset.bottomLeft,
  56.       colors: [
  57.         const Color(0xFFCFD8DC),
  58.         const Color(0xFF607D8B),
  59.       ],
  60.     ),
  61.   );
  62.  
  63.   @override
  64.   Widget build(BuildContext context) {
  65.     return new Scaffold(
  66.       body: new Container(
  67.         decoration: linearGradient,
  68.         child: new Center(
  69.           child: new Column(
  70.             mainAxisAlignment: MainAxisAlignment.center,
  71.             children: <Widget>[
  72.               new Opacity(
  73.                   opacity: loader_visible ? 1.0 : 0.0,
  74.                   child:
  75.                   new Container(
  76.                       padding: const EdgeInsets.all(32.0),
  77.                       child: new CircularProgressIndicator()
  78.                   )
  79.               ),
  80.               new Opacity(
  81.                   opacity: buttons_visible ? 1.0 : 0.0,
  82.                   child:
  83.                   new Column(
  84.  
  85.                       mainAxisAlignment: MainAxisAlignment.center,
  86.                       children: <Widget>[
  87.                         new Container(
  88.                             width: 240.0,
  89.                             padding: const EdgeInsets.all(8.0),
  90.                             child:
  91.                             new RaisedButton.icon(
  92.                                 icon: new Icon(FontAwesomeIcons.facebook),
  93.                                 label: new Text('Sign in with Facebook'),
  94.                                 onPressed: () {
  95.                                   _performSignin('facebook');
  96.                                 }
  97.                             )
  98.                         ),
  99.                         new Container(
  100.                             width: 240.0,
  101.                             padding: const EdgeInsets.all(8.0),
  102.                             child:
  103.                             new RaisedButton.icon(
  104.                                 icon: new Icon(FontAwesomeIcons.google),
  105.                                 label: new Text('Sign in with Google'),
  106.                                 onPressed: () {
  107.                                   _performSignin('google');
  108.                                 }
  109.                             ),
  110.                         ),
  111.  
  112.                       ]
  113.                   )
  114.               )
  115.             ]
  116.           )
  117.         )
  118.       )
  119.     );
  120.   }
  121. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement