// Copyright 2019 The Flutter Authors. All rights reserved. // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. // ignore_for_file: public_member_api_docs import 'dart:async'; import 'dart:convert' show json; import "package:http/http.dart" as http; import 'package:flutter/material.dart'; import 'package:google_sign_in/google_sign_in.dart'; GoogleSignIn _googleSignIn = GoogleSignIn( scopes: [ 'email', 'https://www.googleapis.com/auth/contacts.readonly', ], ); void main() { runApp( MaterialApp( title: 'Google Sign In', home: SignInDemo(), ), ); } class SignInDemo extends StatefulWidget { @override State createState() => SignInDemoState(); } class SignInDemoState extends State { GoogleSignInAccount _currentUser; String _contactText = ''; @override void initState() { super.initState(); _googleSignIn.onCurrentUserChanged.listen((GoogleSignInAccount account) { setState(() { _currentUser = account; }); if (_currentUser != null) { _handleGetContact(_currentUser); } }); _googleSignIn.signInSilently(); } Future _handleGetContact(GoogleSignInAccount user) async { setState(() { _contactText = "Loading contact info..."; }); final http.Response response = await http.get( Uri.parse('https://people.googleapis.com/v1/people/me/connections' '?requestMask.includeField=person.names'), headers: await user.authHeaders, ); if (response.statusCode != 200) { setState(() { _contactText = "People API gave a ${response.statusCode} " "response. Check logs for details."; }); print('People API ${response.statusCode} response: ${response.body}'); return; } final Map data = json.decode(response.body); final String namedContact = _pickFirstNamedContact(data); setState(() { if (namedContact != null) { _contactText = "I see you know $namedContact!"; } else { _contactText = "No contacts to display."; } }); } String _pickFirstNamedContact(Map data) { final List connections = data['connections']; final Map contact = connections?.firstWhere( (dynamic contact) => contact['names'] != null, orElse: () => null, ); if (contact != null) { final Map name = contact['names'].firstWhere( (dynamic name) => name['displayName'] != null, orElse: () => null, ); if (name != null) { return name['displayName']; } } return null; } Future _handleSignIn() async { try { await _googleSignIn.signIn(); } catch (error) { print(error); } } Future _handleSignOut() => _googleSignIn.disconnect(); Widget _buildBody() { GoogleSignInAccount user = _currentUser; if (user != null) { return Column( mainAxisAlignment: MainAxisAlignment.spaceAround, children: [ ListTile( leading: GoogleUserCircleAvatar( identity: user, ), title: Text(user.displayName ?? ''), subtitle: Text(user.email), ), const Text("Signed in successfully."), Text(_contactText), ElevatedButton( child: const Text('SIGN OUT'), onPressed: _handleSignOut, ), ElevatedButton( child: const Text('REFRESH'), onPressed: () => _handleGetContact(user), ), ], ); } else { return Column( mainAxisAlignment: MainAxisAlignment.spaceAround, children: [ const Text("You are not currently signed in."), ElevatedButton( 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(), )); } }