Created
April 30, 2019 00:04
-
-
Save mfreeman451/5480ad76485bc149eae7a4141c111427 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import 'dart:convert'; | |
import 'package:flutter/material.dart'; | |
import 'package:flutter_facebook_login/flutter_facebook_login.dart'; | |
import 'package:http/http.dart' as http; | |
import 'package:chaseapp/topbar.dart'; | |
class LoginPage extends StatefulWidget { | |
@override | |
_LoginPageState createState() => _LoginPageState(); | |
} | |
class _LoginPageState extends State<LoginPage> { | |
bool isLoggedIn = false; | |
var profileData; | |
var facebookLogin = FacebookLogin(); | |
void onLoginStatusChanged(bool isLoggedIn, {profileData}) { | |
setState(() { | |
this.isLoggedIn = isLoggedIn; | |
this.profileData = profileData; | |
}); | |
} | |
@override | |
Widget build(BuildContext context) { | |
return MaterialApp( | |
home: Scaffold( | |
appBar: TopBar(context), | |
/* AppBar( | |
title: Text("Facebook Login"), | |
actions: <Widget>[ | |
IconButton( | |
icon: Icon( | |
Icons.exit_to_app, | |
color: Colors.white, | |
), | |
onPressed: () => facebookLogin.isLoggedIn | |
.then((isLoggedIn) => isLoggedIn ? _logout() : {}), | |
), | |
], | |
), | |
*/ | |
body: Container( | |
child: Center( | |
child: isLoggedIn | |
? _displayUserData(profileData) | |
: _displayLoginButton(), | |
), | |
), | |
), | |
); | |
} | |
void initiateFacebookLogin() async { | |
var facebookLoginResult = | |
await facebookLogin.logInWithReadPermissions(['email']); | |
switch (facebookLoginResult.status) { | |
case FacebookLoginStatus.error: | |
onLoginStatusChanged(false); | |
break; | |
case FacebookLoginStatus.cancelledByUser: | |
onLoginStatusChanged(false); | |
break; | |
case FacebookLoginStatus.loggedIn: | |
var graphResponse = await http.get( | |
'https://graph.facebook.com/v2.12/me?fields=name,first_name,last_name,email,picture.height(200)&access_token=${facebookLoginResult.accessToken.token}'); | |
var profile = json.decode(graphResponse.body); | |
print(profile.toString()); | |
onLoginStatusChanged(true, profileData: profile); | |
break; | |
} | |
} | |
_displayUserData(profileData) { | |
return Column( | |
mainAxisSize: MainAxisSize.min, | |
children: <Widget>[ | |
Container( | |
height: 200.0, | |
width: 200.0, | |
decoration: BoxDecoration( | |
shape: BoxShape.circle, | |
image: DecorationImage( | |
fit: BoxFit.fill, | |
image: NetworkImage( | |
profileData['picture']['data']['url'], | |
), | |
), | |
), | |
), | |
SizedBox(height: 28.0), | |
Text( | |
"Logged in as: ${profileData['name']}", | |
style: TextStyle( | |
fontSize: 20.0, | |
), | |
), | |
], | |
); | |
} | |
_displayLoginButton() { | |
return RaisedButton( | |
child: Text("Login with Facebook"), | |
onPressed: () => initiateFacebookLogin(), | |
); | |
} | |
_logout() async { | |
await facebookLogin.logOut(); | |
onLoginStatusChanged(false); | |
print("Logged out"); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment