Add Google Sign in to you Flutter app just in few steps
3 min readMay 13, 2022
Following steps might be helpful to achieve.
1. Setting up your project
Setup your Flutter project
2. Adding pub.dev library
firebase_auth
google_sign_in
3. Implement Google Sign in functionality
4. Creating firebase project
- Add your project-level build.gradle file.
dependencies {
classpath 'com.android.tools.build:gradle:3.5.0'
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
classpath 'com.google.gms:google-services:4.3.3'
}
- Add to your project app-level build. Gradle file
dependencies {
implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
implementation 'com.google.firebase:firebase-analytics:17.2.2'}
apply plugin: 'com.google.gms.google-services'
5. Getting SHA-1 key
To generate SHA key please follow this link https://developers.google.com/android/guides/client-auth
7. Adding google authentication
create an instance of GoogleSignIn.
final FirebaseAuth _auth = FirebaseAuth.instance;
final GoogleSignIn _googleSignIn = GoogleSignIn();
8. Testing our app functionality
Add this code to your main.dart file and run and test it
import 'package:flutter/material.dart';
import 'package:google_sign_in/google_sign_in.dart';void main() {
runApp(const MaterialApp(
home: Home(),
));
}final GoogleSignIn _googleSignIn = GoogleSignIn(scopes: ['email']);class Home extends StatefulWidget {
const Home({Key? key}) : super(key: key);@override
_HomeState createState() => _HomeState();
}class _HomeState extends State<Home> {
GoogleSignInAccount? _currentUser;@override
void initState() {
_googleSignIn.onCurrentUserChanged.listen((account) {
setState(() {
_currentUser = account;
});
});
_googleSignIn.signInSilently();
super.initState();
}@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: const Text('Flutter Google Sign in'),
),
body: Container(
alignment: Alignment.center,
child: _buildWidget(),
),
);
}Widget _buildWidget() {
GoogleSignInAccount? user = _currentUser;
if (user != null) {
return Padding(
padding: const EdgeInsets.fromLTRB(2, 12, 2, 12),
child: Column(
children: [
ListTile(
leading: GoogleUserCircleAvatar(identity: user),
title: Text(
user.displayName ?? '',
style: TextStyle(fontSize: 22),
),
subtitle: Text(user.email, style: TextStyle(fontSize: 22)),
),
const SizedBox(
height: 20,
),
const Text(
'Signed in successfully',
style: TextStyle(fontSize: 20),
),
const SizedBox(
height: 10,
),
ElevatedButton(onPressed: signOut, child: const Text('Sign out'))
],
),
);
} else {
return Padding(
padding: const EdgeInsets.all(12.0),
child: Column(
children: [
const SizedBox(
height: 20,
),
const Text(
'You are not signed in',
style: TextStyle(fontSize: 30),
),
const SizedBox(
height: 10,
),
ElevatedButton(
onPressed: signIn,
child: const Padding(
padding: EdgeInsets.all(8.0),
child: Text('Sign in', style: TextStyle(fontSize: 30)),
)),
],
),
);
}
}void signOut() {
_googleSignIn.disconnect();
}Future<void> signIn() async {
try {
await _googleSignIn.signIn();
} catch (e) {
print('Error signing in $e');
}
}
}