我使用[google_sign_in 3.2.1] [1],Google登录和退出工作位于同一页面(同一班级)时。有人可以举例说明如何将“退出”功能移至另一个页面(具有另一个类)。
“我的导航菜单”可以将用户转到“首页”,“个人资料页面”两页。
假设用户在Google登录成功后首先从“身份验证页面”移至“主页”。然后,用户使用“主页”中的“导航菜单”转到“个人资料页面”,在那里他可以选择退出
如何使Google注销-_handleSignOut()函数在我的个人资料页面上起作用。
1。认证页面
import 'dart:async';
import 'dart:convert' show json;
import "package:http/http.dart" as http;
import 'package:flutter/material.dart';
import 'package:google_sign_in/google_sign_in.dart';
GoogleSignIn _googleSignIn = GoogleSignIn(
scopes: <String>[
'email',
'https://www.googleapis.com/auth/contacts.readonly',
],
);
void main() {
runApp(
MaterialApp(
title: 'Google Sign In',
home: SignInDemo(),
),
);
}
class SignInDemo extends StatefulWidget {
@override
State createState() => SignInDemoState();
}
class SignInDemoState extends State<SignInDemo> {
GoogleSignInAccount _currentUser;
String _contactText;
@override
void initState() {
super.initState();
_googleSignIn.onCurrentUserChanged.listen((GoogleSignInAccount account) {
setState(() {
_currentUser = account;
});
if (_currentUser != null) {
_handleGetContact();
}
});
_googleSignIn.signInSilently();
}
Future<Null> _handleGetContact() async {
setState(() {
_contactText = "Loading contact info...";
});
final http.Response response = await http.get(
'https://people.googleapis.com/v1/people/me/connections'
'?requestMask.includeField=person.names',
headers: await _currentUser.authHeaders,
);
if (response.statusCode != 200) {
setState(() {
_contactText = "People API gave a ${response.statusCode} "
"response. Check logs for details.";
});
print('People API ${response.statusCode} response: ${response.body}');
return;
}
final Map<String, dynamic> data = json.decode(response.body);
final String namedContact = _pickFirstNamedContact(data);
setState(() {
if (namedContact != null) {
_contactText = "I see you know $namedContact!";
} else {
_contactText = "No contacts to display.";
}
});
}
String _pickFirstNamedContact(Map<String, dynamic> data) {
final List<dynamic> connections = data['connections'];
final Map<String, dynamic> contact = connections?.firstWhere(
(dynamic contact) => contact['names'] != null,
orElse: () => null,
);
if (contact != null) {
final Map<String, dynamic> name = contact['names'].firstWhere(
(dynamic name) => name['displayName'] != null,
orElse: () => null,
);
if (name != null) {
return name['displayName'];
}
}
return null;
}
Future<Null> _handleSignIn() async {
try {
await _googleSignIn.signIn();
} catch (error) {
print(error);
}
}
Future<Null> _handleSignOut() async {
_googleSignIn.disconnect();
}
Widget _buildBody() {
if (_currentUser != null) {
return Column(
mainAxisAlignment: MainAxisAlignment.spaceAround,
children: <Widget>[
ListTile(
leading: GoogleUserCircleAvatar(
identity: _currentUser,
),
title: Text(_currentUser.displayName),
subtitle: Text(_currentUser.email),
),
const Text("Signed in successfully."),
Text(_contactText),
RaisedButton(
child: const Text('SIGN OUT'),
onPressed: _handleSignOut,
),
RaisedButton(
child: const Text('REFRESH'),
onPressed: _handleGetContact,
),
],
);
} else {
return Column(
mainAxisAlignment: MainAxisAlignment.spaceAround,
children: <Widget>[
const Text("You are not currently signed in."),
RaisedButton(
child: const Text('SIGN IN'),
onPressed: _handleSignIn,
),
],
);
}
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: const Text('Google Sign In'),
),
body: ConstrainedBox(
constraints: const BoxConstraints.expand(),
child: _buildBody(),
)
bottomNavigationBar: new BottomNavigator(),
);
}
}
2。主页
import 'package:streaming_entertainment/movie_detail_navigation.dart';
class HomePage extends StatefulWidget {
HomePage({
Key key,
}) : super(key: key);
@override
_HomePageState createState() => new _HomePageState();
}
class _HomePageState extends State<HomePage> {
@override
void initState() {
super.initState();
}
Widget welcomeMessage(){
return new Container(
width: 100.0,
child: new Padding(
padding: const EdgeInsets.all(5.0),
child: new Text(
"This is the the Home Page",
textAlign: TextAlign.center,
style: new TextStyle(fontWeight: FontWeight.bold),
overflow: TextOverflow.ellipsis,
maxLines: 3,
textScaleFactor: 1.0
),
),
);
}
Widget homeUI(){
return new Column(
mainAxisAlignment: MainAxisAlignment.spaceEvenly,
children: [
welcomeMessage(),
],
);
}
@override
Widget build(BuildContext context) {
return new Scaffold(
resizeToAvoidBottomPadding: false,
body: new Container(
child : new Center(
child : homeUI(),
),
),
bottomNavigationBar: new BottomNavigator(),
);
}
}
2。个人资料页面
class ProfilePage extends StatefulWidget {
ProfilePage({
Key key,
}) : super(key: key);
@override
_ProfilePageState createState() => new _ProfilePageState();
}
class _ProfilePageState extends State<ProfilePage> {
@override
void initState() {
super.initState();
}
_signOutFromGoogle() async {
// Google Sign Out
}
Widget welcomeMessage(){
return new Container(
width: 100.0,
child: new Padding(
padding: const EdgeInsets.all(5.0),
child: new Text(
"This is the the Profile Page",
textAlign: TextAlign.center,
style: new TextStyle(fontWeight: FontWeight.bold),
overflow: TextOverflow.ellipsis,
maxLines: 3,
textScaleFactor: 1.0
),
),
);
}
Widget logoutButton(){
return new FlatButton(
onPressed: (){
_signOutFromGoogle();
},
child: new Text('Sign Out'),
);
}
Widget profileUI(){
return new Column(
mainAxisAlignment: MainAxisAlignment.spaceEvenly,
children: [
welcomeMessage(),
logoutButton(),
],
);
}
@override
Widget build(BuildContext context) {
return new Scaffold(
resizeToAvoidBottomPadding: false,
body: new Container(
child : new Center(
child : profileUI(),
),
),
bottomNavigationBar: new BottomNavigator(),
);
}
}
2。底部导航栏
class BottomNavigator extends StatefulWidget {
BottomNavigator({
Key key,
}) : super(key: key);
@override
_BottomNavigatorState createState() => new _BottomNavigatorState();
}
class _BottomNavigatorState extends State<BottomNavigator> {
Widget navigationOptions(String imageUrl, var route, String optionName){
return new Container(
height: 40.0,
child: new GestureDetector(
child: new Column(
mainAxisAlignment: MainAxisAlignment.start,
crossAxisAlignment: CrossAxisAlignment.center,
children: [
new Container(
padding: const EdgeInsets.all(1.0),
child: new Image.asset(
imageUrl,
color: Colors.black,
fit: BoxFit.cover,
width: 20.0,
height: 20.0,
),
),
new Container(
padding: const EdgeInsets.all(1.0),
child: new Text(
optionName,
textAlign: TextAlign.right,
style: new TextStyle(fontWeight: FontWeight.bold, color: Colors.black),
textScaleFactor: 0.75,
),
),
]
),
onTap: (){
print ("Move to Selected Page");
Navigator.of(context).push(route);
},
),
);
}
Widget universalNavigationBar(){
var routeHome = new MaterialPageRoute(builder: (BuildContext context) => new HomePage());
var routeProfile = new MaterialPageRoute(builder: (BuildContext context) => new ProfilePage());
return new BottomAppBar(
color: Colors.white, // new Color(0xFFFFB500)
child: new Container(
padding: const EdgeInsets.all(5.0),
child: new Row(
mainAxisSize: MainAxisSize.max,
mainAxisAlignment: MainAxisAlignment.spaceAround,
crossAxisAlignment: CrossAxisAlignment.center,
children: <Widget>[
navigationOptions("images/home.png", routeHome, "home"),
navigationOptions("images/profile.png", routeProfile, "profile"),
],
),
),
);
}
@override
Widget build(BuildContext context) {
return universalNavigationBar();
}
}
答案 0 :(得分:2)
我已经实现了使用Firebase的google登录,它与您要查找的内容相似。
因此,您可以参考以下内容:
main.dart
import 'dart:async';
import 'package:flutter/material.dart';
import 'package:firebase_auth/firebase_auth.dart';
import 'package:google_sign_in/google_sign_in.dart';
import 'details.dart';
void main() => runApp(new MyApp());
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return new MaterialApp(
theme: new ThemeData(
primarySwatch: Colors.blue,
),
debugShowCheckedModeBanner: false,
home: new MyHomePage(),
);
}
}
class MyHomePage extends StatefulWidget {
@override
MyHomePageState createState() => MyHomePageState();
}
class MyHomePageState extends State<MyHomePage> {
final FirebaseAuth firebaseAuth = FirebaseAuth.instance;
final GoogleSignIn googleSignIn = GoogleSignIn();
static bool _LoginButton = true;
FirebaseUser firebaseUser;
void signOut(){
googleSignIn.signOut();
setState((){
_LoginButton = true;
});
Navigator.pop(context);
print(_LoginButton);
print("User Signed Out");
}
Future<FirebaseUser> _signIn() async{
if(_LoginButton==true){
setState((){
_LoginButton=false;
});
GoogleSignInAccount googleSignInAccount = await googleSignIn.signIn();
GoogleSignInAuthentication googleSignInAuthentication = await googleSignInAccount.authentication;
firebaseUser = await firebaseAuth.signInWithGoogle(idToken: googleSignInAuthentication.idToken, accessToken: googleSignInAuthentication.accessToken);
print("Username is "+firebaseUser.displayName);
setState((){
_LoginButton = true;
});
Navigator.push(context, MaterialPageRoute(builder: (context) => details(firebaseUser.displayName,signOut)));
return firebaseUser;
}
}
bool _LoginButtonBool(){
return _LoginButton;
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(title: Text("Google auth with firebase"),),
body: Center(
child: _LoginButtonBool()?Container(
child: Column(
mainAxisAlignment: MainAxisAlignment.spaceAround,
children: <Widget>[
MaterialButton(onPressed: _LoginButtonBool() ? () => _signIn().then((FirebaseUser firebaseuser ) =>print(firebaseuser)).catchError((e) => print(e)): null,
child: Text("Login"),color: Colors.orange,),
],
),
):CircularProgressIndicator(backgroundColor: Colors.greenAccent.withOpacity(0.01),),
),
);
}
}
details.dart
import 'package:flutter/material.dart';
class details extends StatefulWidget {
String name;
final Function callback;
details(this.name,this.callback);
@override
_detailsState createState() => _detailsState(name,callback);
}
class _detailsState extends State<details> {
String name;
final Function callback;
_detailsState(this.name,this.callback);
@override
Widget build(BuildContext context) {
return Scaffold(
body:Center(child: Column(
mainAxisAlignment: MainAxisAlignment.spaceAround,
children: <Widget>[
Text(name),
MaterialButton(onPressed: () => callback(),
child: Text("Log out"),color: Colors.orange),
],
),),
);
}
}