Newby Coder header banner

Google Login in a Flutter application

In Flutter, authentication using Google sign-in can be implemented by using google_sign_in package which provides a GoogleSignIn widget

GoogleSignIn.signIn() method initiates a typical webview login workflow of Google which asks a user to login, if not already, and allow permissions for the application like access basic profile data, email etc if logging in for first time or if application requires more permissions that earlier

Steps involved in application code :

Firebase setup :

Usage

Import
import 'package:google_sign_in/google_sign_in.dart';
import 'package:flutter_auth_buttons/flutter_auth_buttons.dart';
Invoke Google sign in and get user data
  final GoogleSignIn _googleSignIn = GoogleSignIn();
  GoogleSignInAccount _googleUser = await _googleSignIn.signIn();
Sign out
await _googleSignIn.signOut();

Creating new Flutter App

Check Flutter installation to setup Flutter

Use flutter create command to create a Flutter project (here nc_google_login) :

flutter create nc_google_login

Dependency

Firebase

Following setup requires an App to be registered to Firebase to integrate google sign in

Firebase is an app development platform offering typical cloud services for mobile app development and also decent free usage limits for testing & development

It seems to be occupying a position of being a conventional choice of cloud platform for contemporary mobile app development

Create Firebase project

Go to Firebase Console, sign in with a Google account and create a project

cl-firebase-create-project

Enter a project name, a project can be used to create multiple apps

cl-firebase-create-project-name

Optionally enable Google analytics and click on Create project

Enable Authentication in Firebase Console

Click the Authentication option in left navigation tab

cl-firebase-create-add-sign-in

Click on Set up the sign-in method and choose Google from among the Sign-in providers and Enable it

Enter a name for app to be shown when a user logs in using google

cl-firebase-create-add-sign-in-public-facing-name

Create Android app in Firebase console

App Code

nc_google_login/lib/main.dart
import 'package:flutter/material.dart';
import 'dart:async';
//import 'dart:typed_data';
import 'package:google_sign_in/google_sign_in.dart';
import 'package:flutter_auth_buttons/flutter_auth_buttons.dart';

void main() => runApp(new MaterialApp(
  title: 'Flutter Authentication App',
  home: new LoginPage(),
));

class LoginPage extends StatefulWidget {
  @override
  State<StatefulWidget> createState() => new _LoginPageState();
}

class _LoginPageState extends State<LoginPage> {
  final GlobalKey<FormState> _formKey = new GlobalKey<FormState>();
  final GoogleSignIn _googleSignIn = GoogleSignIn();
  GoogleSignInAccount _googleUser;

  Future<GoogleSignInAccount> _handleSignIn() async {
    _googleUser = await _googleSignIn.signIn();
    this.setState((){});
  }

  Widget renderButton() {
    return Center(
      child: GoogleSignInButton(
        onPressed: this._handleSignIn,
        darkMode: true,
        textStyle: TextStyle(
          fontSize: 14,
          color: Colors.white,
        ),
      ),
    );
  }

  Widget renderUser() {
      return Center(
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: [
            Container( height:200, width:200, child: GoogleUserCircleAvatar(identity:_googleUser)),
            Container(
              padding: EdgeInsets.only(top:20),
              child: Column(
                children: [
                  Text('Name: ${_googleUser.displayName}'),
                  Text('Email: ${_googleUser.email}'),
              ],
            ),
          ),
        ],
      ),
    );
  }

  @override
  Widget build(BuildContext context) {
    var render = _googleUser == null? renderButton() : renderUser();
    return new Scaffold(
      appBar: new AppBar(title: new Text('Google Login'),),
      body: new Container(
        child: render,
      ),
    );
  }
}

Run instructions

Ensure a supported device is connected or emulator is started

Go to project directory

Use flutter run command to run

flutter run

It builds and runs app on an available android/ios device

cl-flutter-android-google-login1cl-flutter-android-google-login2
cl-flutter-android-google-login3cl-flutter-android-google-login4