Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import 'package:firebase_auth/firebase_auth.dart';
- import 'package:flutter/material.dart';
- // import 'package:foodshare/screens/start.dart';
- import 'package:google_sign_in/google_sign_in.dart';
- class HomePage extends StatefulWidget {
- @override
- _HomePageState createState() => _HomePageState();
- }
- class _HomePageState extends State<HomePage> {
- final FirebaseAuth _auth = FirebaseAuth.instance;
- late User user;
- bool isloggedin = false;
- checkAuthentification() async {
- _auth.authStateChanges().listen((user) {
- if (user == null) {
- Navigator.of(context).pushReplacementNamed("start");
- }
- });
- }
- getUser() async {
- User? firebaseUser = _auth.currentUser;
- await firebaseUser?.reload();
- firebaseUser = _auth.currentUser;
- if (firebaseUser != null) {
- setState(() {
- this.user = firebaseUser!;
- this.isloggedin = true;
- });
- }
- }
- signOut() async {
- _auth.signOut();
- final googleSignIn = GoogleSignIn();
- await googleSignIn.signOut();
- }
- @override
- void initState() {
- super.initState();
- this.checkAuthentification();
- this.getUser();
- }
- @override
- Widget build(BuildContext context) {
- return Scaffold(
- body: Container(
- child: !isloggedin
- ? CircularProgressIndicator()
- : Column(
- children: <Widget>[
- SizedBox(height: 40.0),
- Container(
- height: 300,
- child: Image(
- image: AssetImage('assets/images/welcome.jpg'),
- fit: BoxFit.contain,
- ),
- ),
- Container(
- child: Text(
- "Hello ${user.displayName} you are Logged in as ${user.email}",
- style: TextStyle(
- fontSize: 20.0,
- fontWeight: FontWeight.bold,
- ),
- ),
- ),
- ElevatedButton(
- onPressed: signOut,
- child: Text(
- 'Signout',
- style: TextStyle(
- fontSize: 20.0,
- fontWeight: FontWeight.bold,
- color: Colors.white,
- ),
- ),
- style: ElevatedButton.styleFrom(
- padding: EdgeInsets.only(
- left: 70.0,
- right: 10.0,
- top: 70.0,
- bottom: 10.0,
- ),
- shape: RoundedRectangleBorder(
- borderRadius: BorderRadius.circular(10.0),
- ),
- primary: Theme.of(context).primaryColor,
- ),
- ),
- ],
- ),
- ),
- );
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement