Created
April 28, 2020 05:34
-
-
Save happyharis/96a13a412147e4877c581c0413b317c9 to your computer and use it in GitHub Desktop.
Flutter web Google Sign In
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!DOCTYPE html> | |
<html> | |
<head> | |
<meta charset="UTF-8" /> | |
<meta content="IE=Edge" http-equiv="X-UA-Compatible" /> | |
<meta name="description" content="A new Flutter project." /> | |
<!-- iOS meta tags & icons --> | |
<meta name="apple-mobile-web-app-capable" content="yes" /> | |
<meta name="apple-mobile-web-app-status-bar-style" content="black" /> | |
<meta name="apple-mobile-web-app-title" content="flutter_web_google_auth" /> | |
<link rel="apple-touch-icon" href="icons/Icon-192.png" /> | |
<!-- Favicon --> | |
<link rel="shortcut icon" type="image/png" href="favicon.png" /> | |
<title>flutter_web_google_auth</title> | |
<link rel="manifest" href="manifest.json" /> | |
</head> | |
<body> | |
<!-- This script installs service_worker.js to provide PWA functionality to | |
application. For more information, see: | |
https://developers.google.com/web/fundamentals/primers/service-workers --> | |
<script> | |
if ("serviceWorker" in navigator) { | |
window.addEventListener("load", function () { | |
navigator.serviceWorker.register("flutter_service_worker.js"); | |
}); | |
} | |
</script> | |
<script src="main.dart.js" type="application/javascript"></script> | |
<meta | |
name="google-signin-client_id" | |
content="<insert your google api>.apps.googleusercontent.com" | |
/> | |
</body> | |
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import 'dart:async'; | |
import 'package:flutter/material.dart'; | |
import 'package:google_sign_in/google_sign_in.dart'; | |
GoogleSignIn _googleSignIn = GoogleSignIn( | |
scopes: <String>[ | |
'email', | |
], | |
); | |
void main() { | |
runApp( | |
MaterialApp( | |
title: 'Google Sign In', | |
home: SignInDemo(), | |
), | |
); | |
} | |
class SignInDemo extends StatefulWidget { | |
@override | |
State createState() => SignInDemoState(); | |
} | |
class SignInDemoState extends State<SignInDemo> { | |
GoogleSignInAccount _currentUser; | |
String _contactText; | |
@override | |
void initState() { | |
super.initState(); | |
_googleSignIn.onCurrentUserChanged.listen((GoogleSignInAccount account) { | |
setState(() { | |
_currentUser = account; | |
}); | |
}); | |
_googleSignIn.signInSilently(); | |
} | |
Future<void> _handleSignIn() async { | |
try { | |
await _googleSignIn.signIn(); | |
} catch (error) { | |
print(error); | |
} | |
} | |
Future<void> _handleSignOut() => _googleSignIn.disconnect(); | |
Widget _buildBody() { | |
if (_currentUser != null) { | |
return Column( | |
mainAxisAlignment: MainAxisAlignment.spaceAround, | |
children: <Widget>[ | |
ListTile( | |
leading: GoogleUserCircleAvatar( | |
identity: _currentUser, | |
), | |
title: Text(_currentUser.displayName ?? ''), | |
subtitle: Text(_currentUser.email ?? ''), | |
), | |
const Text("Signed in successfully."), | |
Text(_contactText ?? ''), | |
RaisedButton( | |
child: const Text('SIGN OUT'), | |
onPressed: _handleSignOut, | |
), | |
], | |
); | |
} else { | |
return Column( | |
mainAxisAlignment: MainAxisAlignment.spaceAround, | |
children: <Widget>[ | |
const Text("You are not currently signed in."), | |
RaisedButton( | |
child: const Text('SIGN IN'), | |
onPressed: _handleSignIn, | |
), | |
], | |
); | |
} | |
} | |
@override | |
Widget build(BuildContext context) { | |
return Scaffold( | |
appBar: AppBar( | |
title: const Text('Google Sign In'), | |
), | |
body: ConstrainedBox( | |
constraints: const BoxConstraints.expand(), | |
child: _buildBody(), | |
)); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
name: flutter_web_google_auth | |
description: A new Flutter project. | |
publish_to: "none" | |
version: 1.0.0+1 | |
environment: | |
sdk: ">=2.7.0 <3.0.0" | |
dependencies: | |
flutter: | |
sdk: flutter | |
google_sign_in: ^4.4.4 | |
cupertino_icons: ^0.1.3 | |
dev_dependencies: | |
flutter_test: | |
sdk: flutter | |
flutter: | |
uses-material-design: true |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
ClientID not set. Either set it on a <meta name="google-signin-client_id" content="CLIENT_ID" /> tag, or pass clientId when calling init()