import
'package:firebase_auth/firebase_auth.dart'
;
import
'package:firebase_core/firebase_core.dart'
;
import
'package:google_sign_in/google_sign_in.dart'
;
import
'package:flutter/material.dart'
;
import
'package:shared_preferences/shared_preferences.dart'
;
class
GoogleSignIn extends StatefulWidget {
const
GoogleSignIn({Key? key}) : super(key: key);
@override
State<GoogleSignIn> createState() => _GoogleSignInState();
}
class
_GoogleSignInState extends State<GoogleSignIn> {
final GoogleSignIn googleSignIn = GoogleSignIn();
String? name, imageUrl, userEmail, uid;
@override
Widget build(BuildContext context) {
final size = MediaQuery.of(context).size;
return
Scaffold(
body: Container(
height: size.height,
width: size.width,
child: Center(
child: InkWell(
onTap: (){
signInWithGoogle();
},
child: Container(
padding: EdgeInsets.all(10),
decoration: BoxDecoration(
border: Border.all(color: Colors.black),
borderRadius: BorderRadius.circular(10)
),
child: Text(
'Sign In with Google'
),
),
),
),
),
);
}
Future<User?> signInWithGoogle() async {
await Firebase.initializeApp();
User? user;
FirebaseAuth auth = FirebaseAuth.instance;
GoogleAuthProvider authProvider = GoogleAuthProvider();
try
{
final UserCredential userCredential =
await auth.signInWithPopup(authProvider);
user = userCredential.user;
}
catch
(e) {
print(e);
}
if
(user != null) {
uid = user.uid;
name = user.displayName;
userEmail = user.email;
imageUrl = user.photoURL;
SharedPreferences prefs = await SharedPreferences.getInstance();
prefs.setBool(
'auth'
,
true
);
print(
"name: $name"
);
print(
"userEmail: $userEmail"
);
print(
"imageUrl: $imageUrl"
);
}
return
user;
}
}