· 4 years ago · Jun 18, 2021, 07:56 PM
1// Copyright 2019 The Flutter Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5import 'dart:async';
6
7import 'package:flutter/material.dart';
8import 'package:google_sign_in/google_sign_in.dart';
9
10import 'package:extension_google_sign_in_as_googleapis_auth/extension_google_sign_in_as_googleapis_auth.dart';
11import 'package:googleapis/people/v1.dart';
12import 'package:googleapis/admob/v1.dart';
13import 'package:googleapis/youtube/v3.dart';
14
15final GoogleSignIn _googleSignIn = GoogleSignIn(
16 scopes: <String>[PeopleServiceApi.contactsReadonlyScope, YouTubeApi.youtubeReadonlyScope, AdMobApi.admobReadonlyScope, AdMobApi.admobReportScope],
17);
18
19void main() {
20 runApp(
21 const MaterialApp(
22 title: 'Google Sign In',
23 home: SignInDemo(),
24 ),
25 );
26}
27
28/// The main widget of this demo.
29class SignInDemo extends StatefulWidget {
30 /// Creates the main widget of this demo.
31 const SignInDemo({Key? key}) : super(key: key);
32
33 @override
34 State createState() => SignInDemoState();
35}
36
37/// The state of the main widget.
38class SignInDemoState extends State<SignInDemo> {
39 GoogleSignInAccount? _currentUser;
40 String? _contactText;
41
42 @override
43 void initState() {
44 super.initState();
45 print(AdMobApi.admobReadonlyScope);
46 print(AdMobApi.admobReportScope);
47 _googleSignIn.onCurrentUserChanged.listen((GoogleSignInAccount? account) {
48 setState(() {
49 _currentUser = account;
50 });
51 if (_currentUser != null) {
52 _handleGetContact();
53 }
54 });
55 _googleSignIn.signInSilently();
56 }
57
58 Future<void> _handleGetContact() async {
59 setState(() {
60 _contactText = 'Loading contact info...';
61 });
62 // TEST USAGE OF PEOPLE API - WORKS
63 final PeopleServiceApi peopleApi = PeopleServiceApi((await _googleSignIn.authenticatedClient())!);
64 final ListConnectionsResponse response = await peopleApi.people.connections.list(
65 'people/me',
66 personFields: 'names',
67 );
68 // TEST USAGE OF YOUTUBE API - WORKS
69 final YouTubeApi youTubeApi = YouTubeApi((await _googleSignIn.authenticatedClient())!);
70 print(await youTubeApi.playlistItems.list(
71 ['snippet'],
72 playlistId: 'LL', // Liked List
73 ));
74 // TEST USAGE OF ADMOB API - DOES NOT WORK, why?
75 final AdMobApi admobApi = AdMobApi((await _googleSignIn.authenticatedClient())!);
76 print(await admobApi.accounts.list(pageSize: 1));
77
78 final String? firstNamedContactName = _pickFirstNamedContact(response.connections);
79
80 setState(() {
81 if (firstNamedContactName != null) {
82 _contactText = 'I see you know $firstNamedContactName!';
83 } else {
84 _contactText = 'No contacts to display.';
85 }
86 });
87 }
88
89 String? _pickFirstNamedContact(List<Person>? connections) {
90 return connections
91 ?.firstWhere(
92 (Person person) => person.names != null,
93 )
94 .names
95 ?.firstWhere(
96 (Name name) => name.displayName != null,
97 )
98 .displayName;
99 }
100
101 Future<void> _handleSignIn() async {
102 try {
103 await _googleSignIn.signIn();
104 } catch (error) {
105 print(error);
106 }
107 }
108
109 Future<void> _handleSignOut() => _googleSignIn.disconnect();
110
111 Widget _buildBody() {
112 if (_currentUser != null) {
113 return Column(
114 mainAxisAlignment: MainAxisAlignment.spaceAround,
115 children: <Widget>[
116 ListTile(
117 leading: GoogleUserCircleAvatar(
118 identity: _currentUser!,
119 ),
120 title: Text(_currentUser!.displayName ?? ''),
121 subtitle: Text(_currentUser!.email),
122 ),
123 const Text('Signed in successfully.'),
124 Text(_contactText ?? ''),
125 ElevatedButton(
126 child: const Text('SIGN OUT'),
127 onPressed: _handleSignOut,
128 ),
129 ElevatedButton(
130 child: const Text('REFRESH'),
131 onPressed: _handleGetContact,
132 ),
133 ],
134 );
135 } else {
136 return Column(
137 mainAxisAlignment: MainAxisAlignment.spaceAround,
138 children: <Widget>[
139 const Text('You are not currently signed in.'),
140 ElevatedButton(
141 child: const Text('SIGN IN'),
142 onPressed: _handleSignIn,
143 ),
144 ],
145 );
146 }
147 }
148
149 @override
150 Widget build(BuildContext context) {
151 return Scaffold(
152 appBar: AppBar(
153 title: const Text('Google Sign In'),
154 ),
155 body: ConstrainedBox(
156 constraints: const BoxConstraints.expand(),
157 child: _buildBody(),
158 ));
159 }
160}