Integrating Google Sign-In
Firstly add google_sign_in dependencies in your pubspac.yaml file
Then add internet permission in your androidManifest.xml file
Then write your code in
main.dart
import'package:flutter/material.dart';
import 'package:google_sign_in/google_sign_in.dart';
GoogleSignIn _googleSignIn = GoogleSignIn(scopes: ['profile', 'email']);
void main() => runApp(MaterialApp(
debugShowCheckedModeBanner: false,
title: 'Google Sign in',
home: SignInDemo(),
));
class SignInDemo extends StatefulWidget {
@override
_SignInDemoState createState() => _SignInDemoState();
}
class _SignInDemoState extends State<SignInDemo> {
GoogleSignInAccount _currentUser;
@override
void initState() {
super.initState();
_googleSignIn.onCurrentUserChanged.listen((GoogleSignInAccount account){
setState(() {
_currentUser = account;
});
});
_googleSignIn.signInSilently();
}
@override
Widget build(BuildContext context) {
return Scaffold(
body: Center(child: _buildBody()),
);
}
Widget _buildBody() {
if (_currentUser != null) {
return Column(
mainAxisAlignment: MainAxisAlignment.center,
crossAxisAlignment: CrossAxisAlignment.center,
mainAxisSize: MainAxisSize.max,
children: <Widget>[
ListTile(
leading: GoogleUserCircleAvatar(
identity: _currentUser,
),
title: Text(_currentUser.displayName ?? ''),
subtitle: Text(_currentUser.email ?? ''),
),
RaisedButton(
onPressed: _handleSignOut,
child: Text('SIGN OUT'),
)
],
);
}
else{
return Column(
mainAxisAlignment: MainAxisAlignment.center,
crossAxisAlignment: CrossAxisAlignment.center,
mainAxisSize: MainAxisSize.max,
children: <Widget>[
RaisedButton(
onPressed: _handleSignIn,
child: Image.network('https://www.pngarts.com/files/4/Google-PNG-Image-Transparent-Background.png',
height: 70.0,width: 70.0,),
shape: CircleBorder(),
),
],
);
}
}
Future<void> _handleSignIn() async{
try{
await _googleSignIn.signIn();
}catch(error){
print(error);
}
}
Future<void> _handleSignOut() async{
_googleSignIn.disconnect();
}
}
import 'package:google_sign_in/google_sign_in.dart';
GoogleSignIn _googleSignIn = GoogleSignIn(scopes: ['profile', 'email']);
void main() => runApp(MaterialApp(
debugShowCheckedModeBanner: false,
title: 'Google Sign in',
home: SignInDemo(),
));
class SignInDemo extends StatefulWidget {
@override
_SignInDemoState createState() => _SignInDemoState();
}
class _SignInDemoState extends State<SignInDemo> {
GoogleSignInAccount _currentUser;
@override
void initState() {
super.initState();
_googleSignIn.onCurrentUserChanged.listen((GoogleSignInAccount account){
setState(() {
_currentUser = account;
});
});
_googleSignIn.signInSilently();
}
@override
Widget build(BuildContext context) {
return Scaffold(
body: Center(child: _buildBody()),
);
}
Widget _buildBody() {
if (_currentUser != null) {
return Column(
mainAxisAlignment: MainAxisAlignment.center,
crossAxisAlignment: CrossAxisAlignment.center,
mainAxisSize: MainAxisSize.max,
children: <Widget>[
ListTile(
leading: GoogleUserCircleAvatar(
identity: _currentUser,
),
title: Text(_currentUser.displayName ?? ''),
subtitle: Text(_currentUser.email ?? ''),
),
RaisedButton(
onPressed: _handleSignOut,
child: Text('SIGN OUT'),
)
],
);
}
else{
return Column(
mainAxisAlignment: MainAxisAlignment.center,
crossAxisAlignment: CrossAxisAlignment.center,
mainAxisSize: MainAxisSize.max,
children: <Widget>[
RaisedButton(
onPressed: _handleSignIn,
child: Image.network('https://www.pngarts.com/files/4/Google-PNG-Image-Transparent-Background.png',
height: 70.0,width: 70.0,),
shape: CircleBorder(),
),
],
);
}
}
Future<void> _handleSignIn() async{
try{
await _googleSignIn.signIn();
}catch(error){
print(error);
}
}
Future<void> _handleSignOut() async{
_googleSignIn.disconnect();
}
}
Confir your project in firebase and google peaple api
Comments