easy_google_sign_in 0.1.0
easy_google_sign_in: ^0.1.0 copied to clipboard
One-liner Google Sign-In for Firebase Auth. Call a single function in onPressed or use the provided button widget.
example/lib/main.dart
import 'package:flutter/material.dart';
import 'package:firebase_core/firebase_core.dart';
import 'package:firebase_auth/firebase_auth.dart';
import 'package:easy_google_sign_in/easy_google_sign_in.dart';
// If you used FlutterFire CLI, import your generated firebase_options.dart
// import 'firebase_options.dart';
void main() async {
WidgetsFlutterBinding.ensureInitialized();
// If you have DefaultFirebaseOptions, use:
// await Firebase.initializeApp(options: DefaultFirebaseOptions.currentPlatform);
await Firebase.initializeApp();
runApp(const MyApp());
}
class MyApp extends StatelessWidget {
const MyApp({super.key});
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Easy Google Sign-In Demo',
theme: ThemeData(colorSchemeSeed: Colors.indigo, useMaterial3: true),
home: const DemoPage(),
debugShowCheckedModeBanner: false,
);
}
}
class DemoPage extends StatefulWidget {
const DemoPage({super.key});
@override
State<DemoPage> createState() => _DemoPageState();
}
class _DemoPageState extends State<DemoPage> {
User? _user;
@override
void initState() {
super.initState();
_user = currentFirebaseUser;
FirebaseAuth.instance.userChanges().listen((u) {
setState(() => _user = u);
});
}
@override
Widget build(BuildContext context) {
final signedIn = isFirebaseSignedIn;
return Scaffold(
appBar: AppBar(title: const Text('Easy Google Sign-In Example')),
body: Center(
child: Column(
mainAxisSize: MainAxisSize.min,
children: [
if (signedIn) ...[
CircleAvatar(
radius: 32,
backgroundImage: _user?.photoURL != null
? NetworkImage(_user!.photoURL!)
: null,
child:
_user?.photoURL == null ? const Icon(Icons.person) : null,
),
const SizedBox(height: 12),
Text(_user?.displayName ?? _user?.email ?? 'Signed in'),
const SizedBox(height: 20),
ElevatedButton(
onPressed: () async => signOutFromGoogle(),
child: const Text('Sign out'),
),
] else ...[
// Option A: one-liner onPressed
ElevatedButton(
onPressed: easyGoogleSignInOnPressed(
onSuccess: (cred) =>
debugPrint('Logged in: ${cred.user?.uid}'),
),
child: const Text('Sign in with Google'),
),
const SizedBox(height: 12),
// Option B: ready-made widget
EasyGoogleSignInButton(
onSuccess: (cred) => ScaffoldMessenger.of(context).showSnackBar(
SnackBar(content: Text('Welcome ${cred.user?.displayName}!')),
),
),
],
],
),
),
);
}
}