Advertisement
rachmadi

home_fascia_flutter

Sep 14th, 2018
105
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Dart 2.01 KB | None | 0 0
  1. import 'dart:async';
  2.  
  3. import 'package:firebase_auth/firebase_auth.dart';
  4. import 'package:flutter/material.dart';
  5.  
  6. import 'sign_in.dart';
  7.  
  8. class HomePage extends StatefulWidget {
  9.   @override
  10.   _HomePageState createState() => new _HomePageState();
  11. }
  12.  
  13. enum AuthStatus { notSignIn, signIn }
  14.  
  15. class _HomePageState extends State<HomePage> {
  16.   AuthStatus _authStatus = AuthStatus.signIn;
  17.   initState() {
  18.     super.initState();
  19.     getCurrentUser().then((userId) {
  20.       setState(() {
  21.         _authStatus = userId == null ? AuthStatus.notSignIn : AuthStatus.signIn;
  22.       });
  23.     });
  24.   }
  25.  
  26.   Future<String> getCurrentUser() async {
  27.     try {
  28.       FirebaseUser user = await FirebaseAuth.instance.currentUser();
  29.       return user.uid;
  30.     } catch (e) {
  31.       print('Error: $e');
  32.       return null;
  33.     }
  34.   }
  35.  
  36.   void signOut() async {
  37.     try {
  38.       FirebaseAuth.instance.signOut();
  39.       this.setState(() {
  40.         _authStatus = AuthStatus.notSignIn;
  41.       });
  42.       print("Sign out");
  43.     } catch (e) {
  44.       print('Error: $e');
  45.     }
  46.   }
  47.  
  48.   @override
  49.   Widget build(BuildContext context) {
  50.     switch (_authStatus) {
  51.       case AuthStatus.notSignIn:
  52.         return new SignInPage();
  53.       case AuthStatus.signIn:
  54.         return new MaterialApp(
  55.           title: 'Fascia',
  56.           theme: new ThemeData(
  57.             primarySwatch: Colors.teal,
  58.           ),
  59.           home: new Scaffold(
  60.             appBar: new AppBar(
  61.               title: new Text('Fascia'),
  62.               actions: <Widget>[
  63.                 new IconButton(
  64.                   icon: new Icon(Icons.exit_to_app),
  65.                   onPressed: signOut,
  66.                 ),
  67.               ],
  68.             ),
  69.             body: new Center(
  70.               child: new Text(
  71.                 'Welcome Fascia user!',
  72.               ),
  73.             ),
  74.           ),
  75.         );
  76.     }
  77.     return null;
  78.   }
  79. }
  80.  
  81. //class MyHomePage extends StatelessWidget {
  82. //  @override
  83. //  Widget build(BuildContext context) {
  84. //    return
  85. //  }
  86. //}
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement