Я пытался создать приложение с помощью входа в 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),
),
],
),)
);
}
}