Advertisement
Guest User

Untitled

a guest
Jun 20th, 2021
201
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Dart 3.00 KB | None | 0 0
  1. import 'package:firebase_auth/firebase_auth.dart';
  2. import 'package:flutter/material.dart';
  3. // import 'package:foodshare/screens/start.dart';
  4. import 'package:google_sign_in/google_sign_in.dart';
  5.  
  6. class HomePage extends StatefulWidget {
  7.   @override
  8.   _HomePageState createState() => _HomePageState();
  9. }
  10.  
  11. class _HomePageState extends State<HomePage> {
  12.   final FirebaseAuth _auth = FirebaseAuth.instance;
  13.   late User user;
  14.   bool isloggedin = false;
  15.  
  16.   checkAuthentification() async {
  17.     _auth.authStateChanges().listen((user) {
  18.       if (user == null) {
  19.         Navigator.of(context).pushReplacementNamed("start");
  20.       }
  21.     });
  22.   }
  23.  
  24.   getUser() async {
  25.     User? firebaseUser = _auth.currentUser;
  26.     await firebaseUser?.reload();
  27.     firebaseUser = _auth.currentUser;
  28.  
  29.     if (firebaseUser != null) {
  30.       setState(() {
  31.         this.user = firebaseUser!;
  32.         this.isloggedin = true;
  33.       });
  34.     }
  35.   }
  36.  
  37.   signOut() async {
  38.     _auth.signOut();
  39.  
  40.     final googleSignIn = GoogleSignIn();
  41.     await googleSignIn.signOut();
  42.   }
  43.  
  44.   @override
  45.   void initState() {
  46.     super.initState();
  47.     this.checkAuthentification();
  48.     this.getUser();
  49.   }
  50.  
  51.   @override
  52.   Widget build(BuildContext context) {
  53.     return Scaffold(
  54.       body: Container(
  55.         child: !isloggedin
  56.             ? CircularProgressIndicator()
  57.             : Column(
  58.                 children: <Widget>[
  59.                   SizedBox(height: 40.0),
  60.                   Container(
  61.                     height: 300,
  62.                     child: Image(
  63.                       image: AssetImage('assets/images/welcome.jpg'),
  64.                       fit: BoxFit.contain,
  65.                     ),
  66.                   ),
  67.                   Container(
  68.                     child: Text(
  69.                       "Hello ${user.displayName} you are Logged in as ${user.email}",
  70.                       style: TextStyle(
  71.                         fontSize: 20.0,
  72.                         fontWeight: FontWeight.bold,
  73.                       ),
  74.                     ),
  75.                   ),
  76.                   ElevatedButton(
  77.                     onPressed: signOut,
  78.                     child: Text(
  79.                       'Signout',
  80.                       style: TextStyle(
  81.                         fontSize: 20.0,
  82.                         fontWeight: FontWeight.bold,
  83.                         color: Colors.white,
  84.                       ),
  85.                     ),
  86.                     style: ElevatedButton.styleFrom(
  87.                       padding: EdgeInsets.only(
  88.                         left: 70.0,
  89.                         right: 10.0,
  90.                         top: 70.0,
  91.                         bottom: 10.0,
  92.                       ),
  93.                       shape: RoundedRectangleBorder(
  94.                         borderRadius: BorderRadius.circular(10.0),
  95.                       ),
  96.                       primary: Theme.of(context).primaryColor,
  97.                     ),
  98.                   ),
  99.                 ],
  100.               ),
  101.       ),
  102.     );
  103.   }
  104. }
  105.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement