我尝试用Google登录名构建应用。实际上,除了我选择了我的Google帐户之后,一切都没有问题,它没有切换到下一页。此外,它只是“重新加载”登录页面。那么如何解决登录后进入新页面的问题?
这是我的主要代码:
import 'package:flutter/material.dart'; import 'package:font_awesome_flutter/font_awesome_flutter.dart'; import 'package:firebase_auth/firebase_auth.dart'; import 'package:google_sign_in/google_sign_in.dart'; import 'ProfileScreen.dart'; void main() => runApp(MyApp()); class MyApp extends StatelessWidget { // This widget is the root of your application. @override Widget build(BuildContext context) { return MaterialApp( title: 'Flutter Google Signin APP', debugShowCheckedModeBanner: false, theme: ThemeData( primaryColor: Color(0xff9C58D2), ), home: GoogleSignApp(), ); } } class GoogleSignApp extends StatefulWidget { @override _GoogleSignAppState createState() => _GoogleSignAppState(); } class _GoogleSignAppState extends State<GoogleSignApp> { final FirebaseAuth _firebaseAuth = FirebaseAuth.instance; final GoogleSignIn _googlSignIn = new GoogleSignIn(); Future<FirebaseUser> _signIn(BuildContext context) async { Scaffold.of(context).showSnackBar(new SnackBar( content: new Text('Sign in'), )); final GoogleSignInAccount googleUser = await _googlSignIn.signIn(); final GoogleSignInAuthentication googleAuth =await googleUser.authentication; final AuthCredential credential = GoogleAuthProvider.getCredential( accessToken: googleAuth.accessToken, idToken: googleAuth.idToken, ); FirebaseUser userDetails = await _firebaseAuth.signInWithCredential(credential); ProviderDetails providerInfo = new ProviderDetails(userDetails.providerId); List<ProviderDetails> providerData = new List<ProviderDetails>(); providerData.add(providerInfo); UserDetails details = new UserDetails( userDetails.providerId, userDetails.displayName, userDetails.photoUrl, userDetails.email, providerData, ); Navigator.push( context, new MaterialPageRoute( builder: (context) => new ProfileScreen(detailsUser: details), ), ); return userDetails; } @override Widget build(BuildContext context) { return Scaffold( body: Builder( builder: (context) => Stack( fit: StackFit.expand, children: <Widget>[ Container( width: MediaQuery.of(context).size.width, height: MediaQuery.of(context).size.height, child: Image.network( 'https://images.unsplash.com/photo-1518050947974-4be8c7469f0c?ixlib=rb-1.2.1&ixid=eyJhcHBfaWQiOjEyMDd9&auto=format&fit=crop&w=500&q=60' ,fit: BoxFit.fill, color: Color.fromRGBO(255, 255, 255, 0.6), colorBlendMode: BlendMode.modulate ), ), Column( mainAxisAlignment: MainAxisAlignment.center, children: <Widget>[ SizedBox(height:10.0), Container( width: 250.0, child: Align( alignment: Alignment.center, child: RaisedButton( shape: RoundedRectangleBorder( borderRadius: new BorderRadius.circular(30.0)), color: Color(0xffffffff), child: Row( mainAxisAlignment: MainAxisAlignment.start, children: <Widget>[ Icon(FontAwesomeIcons.google,color: Color(0xffCE107C),), SizedBox(width:10.0), Text( 'Sign in with Google', style: TextStyle(color: Colors.black,fontSize: 18.0), ), ],), onPressed: () => _signIn(context) .then((FirebaseUser user) => print(user)) .catchError((e) => print(e)), ), ) ), Container( width: 250.0, child: Align( alignment: Alignment.center, child: RaisedButton( shape: RoundedRectangleBorder( borderRadius: new BorderRadius.circular(30.0)), color: Color(0xffffffff), child: Row( mainAxisAlignment: MainAxisAlignment.start, children: <Widget>[ Icon(FontAwesomeIcons.facebookF,color: Color(0xff4754de),), SizedBox(width:10.0), Text( 'Sign in with Facebook', style: TextStyle(color: Colors.black,fontSize: 18.0), ), ],), onPressed: () {}, ), ) ), Container( width: 250.0, child: Align( alignment: Alignment.center, child: RaisedButton( shape: RoundedRectangleBorder( borderRadius: new BorderRadius.circular(30.0)), color: Color(0xffffffff), child: Row( mainAxisAlignment: MainAxisAlignment.start, children: <Widget>[ Icon(FontAwesomeIcons.solidEnvelope,color: Color(0xff4caf50),), SizedBox(width:10.0), Text( 'Sign in with Email', style: TextStyle(color: Colors.black,fontSize: 18.0), ), ],), onPressed: () {}, ), ) ), ], ), ], ),), ); } } class UserDetails { final String providerDetails; final String userName; final String photoUrl; final String userEmail; final List<ProviderDetails> providerData; UserDetails(this.providerDetails,this.userName, this.photoUrl,this.userEmail, this.providerData); } class ProviderDetails { ProviderDetails(this.providerDetails); final String providerDetails; }
这是我的第二页:
import 'package:flutter/material.dart';
import 'main.dart';
import 'package:font_awesome_flutter/font_awesome_flutter.dart';
import 'package:google_sign_in/google_sign_in.dart';
class ProfileScreen extends StatelessWidget {
final UserDetails detailsUser;
ProfileScreen({Key key, @required this.detailsUser}) : super(key: key);
@override
Widget build(BuildContext context) {
final GoogleSignIn _gSignIn = GoogleSignIn();
return Scaffold(
appBar: AppBar(
title: Text(detailsUser.userName),
automaticallyImplyLeading: false,
actions: <Widget>[
IconButton(
icon: Icon(
FontAwesomeIcons.signOutAlt,
size: 20.0,
color: Colors.white,
),
onPressed: (){
_gSignIn.signOut();
print('Signed out');
Navigator.pop(context);
},
),
],
),
body:Center(child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
CircleAvatar(
backgroundImage:NetworkImage(detailsUser.photoUrl),
radius: 50.0,
),
SizedBox(height:10.0),
Text(
"Name : " + detailsUser.userName,
style: TextStyle(fontWeight: FontWeight.bold, color: Colors.black,fontSize: 20.0),
),
SizedBox(height:10.0),
Text(
"Email : " + detailsUser.userEmail,
style: TextStyle(fontWeight: FontWeight.bold, color: Colors.black,fontSize: 20.0),
),
SizedBox(height:10.0),
Text(
"Provider : " + detailsUser.providerDetails,
style: TextStyle(fontWeight: FontWeight.bold, color: Colors.black,fontSize: 20.0),
),
],
),)
);
}
}
我尝试用Google登录名构建应用。实际上,除了我选择了我的Google帐户之后,一切都没有问题,它没有切换到下一页。此外,它只是“重新加载”符号-...
查看此github页面,它对此有解决方案。this is link to github
将Google身份验证添加到您的应用可能会令人沮丧。我建议您在执行签名时查看控制台,并查找任何日志并在此处与我们共享。