mirror of
https://github.com/nisrulz/flutter-examples.git
synced 2025-11-08 20:50:04 +00:00
Added a new example for google signin.
This commit is contained in:
40
google_signin/lib/home.dart
Normal file
40
google_signin/lib/home.dart
Normal file
@@ -0,0 +1,40 @@
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:meta/meta.dart';
|
||||
import 'package:flutter/foundation.dart';
|
||||
|
||||
class Home extends StatelessWidget {
|
||||
Home(
|
||||
{Key key,
|
||||
@required this.onSignin,
|
||||
@required this.onLogout,
|
||||
@required this.showLoading})
|
||||
: super(key: key);
|
||||
|
||||
final VoidCallback onSignin;
|
||||
final VoidCallback onLogout;
|
||||
bool showLoading = false;
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return new Scaffold(
|
||||
appBar: new AppBar(title: new Text("Sign In")),
|
||||
body: new Container(
|
||||
padding: const EdgeInsets.all(20.0),
|
||||
child: new Center(
|
||||
child: new Column(
|
||||
mainAxisAlignment: MainAxisAlignment.center,
|
||||
children: <Widget>[
|
||||
showLoading
|
||||
? new CircularProgressIndicator()
|
||||
: new RaisedButton(
|
||||
onPressed: this.onSignin,
|
||||
child: new Text("Sign In"),
|
||||
color: Colors.lightBlueAccent,
|
||||
),
|
||||
//new RaisedButton(onPressed: this.onLogout, child: new Text("Logout"), color: Colors.amberAccent),
|
||||
],
|
||||
),
|
||||
)),
|
||||
);
|
||||
}
|
||||
}
|
||||
86
google_signin/lib/main.dart
Normal file
86
google_signin/lib/main.dart
Normal file
@@ -0,0 +1,86 @@
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:google_sign_in/google_sign_in.dart';
|
||||
import 'package:firebase_auth/firebase_auth.dart';
|
||||
import 'home.dart';
|
||||
import 'user.dart';
|
||||
import 'dart:async';
|
||||
|
||||
void main() {
|
||||
runApp(new App());
|
||||
}
|
||||
|
||||
class App extends StatefulWidget {
|
||||
AppState createState() => new AppState();
|
||||
}
|
||||
|
||||
class AppState extends State<App> {
|
||||
String _username = "";
|
||||
Widget currentPage;
|
||||
GoogleSignIn googleSignIn;
|
||||
Widget userPage;
|
||||
@override
|
||||
void initState() {
|
||||
super.initState();
|
||||
userPage = new Home(
|
||||
onSignin: () {
|
||||
_signin();
|
||||
print("Sign");
|
||||
},
|
||||
onLogout: _logout,
|
||||
showLoading: false,
|
||||
);
|
||||
}
|
||||
|
||||
Future<FirebaseUser> _signin() async {
|
||||
setState(() {
|
||||
userPage = new Home(onSignin: null, onLogout: _logout, showLoading: true);
|
||||
});
|
||||
FirebaseAuth _auth = FirebaseAuth.instance;
|
||||
try {
|
||||
googleSignIn = new GoogleSignIn();
|
||||
GoogleSignInAccount googleSignInAccount = await googleSignIn.signIn();
|
||||
GoogleSignInAuthentication gauth =
|
||||
await googleSignInAccount.authentication;
|
||||
FirebaseUser user = await _auth.signInWithGoogle(
|
||||
accessToken: gauth.accessToken,
|
||||
idToken: gauth.idToken,
|
||||
);
|
||||
|
||||
setState(() {
|
||||
_username = user.displayName;
|
||||
userPage = new User(
|
||||
onLogout: _logout,
|
||||
user: user,
|
||||
);
|
||||
});
|
||||
|
||||
return user;
|
||||
} catch (e) {
|
||||
print(e.toString());
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
void _logout() async {
|
||||
await googleSignIn.signOut();
|
||||
setState(() {
|
||||
userPage = new Home(
|
||||
onSignin: () {
|
||||
_signin();
|
||||
print("Sign");
|
||||
},
|
||||
onLogout: _logout,
|
||||
showLoading: false,
|
||||
);
|
||||
});
|
||||
|
||||
print("Logged Out");
|
||||
}
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return new MaterialApp(
|
||||
home: userPage,
|
||||
);
|
||||
}
|
||||
}
|
||||
39
google_signin/lib/user.dart
Normal file
39
google_signin/lib/user.dart
Normal file
@@ -0,0 +1,39 @@
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:meta/meta.dart';
|
||||
import 'package:flutter/foundation.dart';
|
||||
import 'package:firebase_auth/firebase_auth.dart';
|
||||
|
||||
class User extends StatelessWidget {
|
||||
User({Key key, @required this.onLogout, @required this.user})
|
||||
: super(key: key);
|
||||
|
||||
VoidCallback onLogout;
|
||||
String username;
|
||||
FirebaseUser user;
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return new Scaffold(
|
||||
appBar: new AppBar(
|
||||
title: new Text("Welcome"),
|
||||
actions: <Widget>[
|
||||
new IconButton(
|
||||
icon: new Icon(Icons.exit_to_app), onPressed: this.onLogout)
|
||||
],
|
||||
),
|
||||
body: new Container(
|
||||
padding: const EdgeInsets.all(20.0),
|
||||
child: new Center(
|
||||
child: new Column(
|
||||
mainAxisAlignment: MainAxisAlignment.center,
|
||||
children: <Widget>[
|
||||
new Image.network(user.photoUrl),
|
||||
new Text(
|
||||
user.displayName,
|
||||
textScaleFactor: 1.5,
|
||||
),
|
||||
],
|
||||
))),
|
||||
);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user