如何处理_googleSignIn.disconnect()(谷歌注销)在一个单独的页面在Flutter?

osh3o9ms  于 2023-02-25  发布在  Flutter
关注(0)|答案(2)|浏览(109)

我使用过[google_sign_in 3.2.1][1],Google登录和注销在同一页面(同一类)上时可以工作。有人能解释一下如何将注销功能移动到不同的页面(具有不同的类)吗?最好用一个示例。
我的导航菜单可以将用户路由到两个页面“主页”、“配置文件页”。
假设用户在成功Google登录后首先从“身份验证页面”移动到“主页”,然后用户使用“主页”中的“导航菜单”转到“个人资料页面”,在该页面中他可以选择注销
如何让Google Sign Out - _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();
  } 

}
i7uaboj4

i7uaboj41#

我已经实现了谷歌登录与firebase,它是类似于你正在寻找的。
所以你可以参考这个:
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),
        ],
      ),),
    );
  }
}
ztyzrc3y

ztyzrc3y2#

我这样实现,它解决了一个关键问题,即自动登录到以前使用的谷歌帐户。

  • 顺便说一句,我使用谷歌和电子邮件/密码验证我的项目.*
class HomeDrawer extends StatefulWidget {
      const HomeDrawer({super.key});
    
      @override
      State<HomeDrawer> createState() => _HomeDrawerState();
    }
    
    class _HomeDrawerState extends State<HomeDrawer> {
      bool _isGoogleAuth = false;

      void outputProviderID() {

        String providerId;

        // get the current user
        User? user = FirebaseAuth.instance.currentUser;

        // check if user is not null
        if (user != null) {
          // iterate over each provider data
          for (UserInfo info in user.providerData) {
            providerId = info.providerId;
            if (providerId == 'google.com') {
              setState(() {
                _isGoogleAuth = true;
              });
            }
          }
        }
      }
    
      @override
      void initState() {
        outputProviderID();
        super.initState();
      }
    
      @override
      Widget build(BuildContext context) {
        return Drawer(
          child: SafeArea(
            child: Column(
              crossAxisAlignment: CrossAxisAlignment.start,
              children: [
                
    
                // log out
                DrawerButton(
                  buttonText: 'Log Out',
                  buttonIcon: const FaIcon(
                    FontAwesomeIcons.signOutAlt,
                    size: 20,
                    color: Colors.red,
                  ),
                  buttonFunction: () {
                    if (_isGoogleAuth) {
                      GoogleSignIn().disconnect().then(
                            (value) => FirebaseAuth.instance.signOut(),
                          );
                    } else {
                      FirebaseAuth.instance.signOut();
                    }
                  },
                ),


              ],
            ),
          ),
        );
      }
    }

您可以添加try{}catch(e){}块来处理错误。

相关问题