此代码可在仿真器上正常工作:
但是在真实设备中:
不要转到下一页
class LoginPageWidget extends StatelessWidget {
final GoogleSignIn _googleSignIn = GoogleSignIn();
final FirebaseAuth _auth = FirebaseAuth.instance;
Future<FirebaseUser> _handleSignIn() async {
FirebaseUser user;
bool isSignedIn = await _googleSignIn.isSignedIn();
if (isSignedIn) {user = await _auth.currentUser();
} else {
final GoogleSignInAccount googleUser = await _googleSignIn.signIn();
final GoogleSignInAuthentication googleAuth =
await googleUser.authentication;
final AuthCredential credential = GoogleAuthProvider.getCredential(
accessToken: googleAuth.accessToken, idToken: googleAuth.idToken);
user = (await _auth.signInWithCredential(credential)).user;}
return user;}
void onGoogleSignIn(BuildContext context) async {
FirebaseUser user = await _handleSignIn();
Navigator.push(context,MaterialPageRoute(builder: (context) => Home(user, _googleSignIn)));}
@override
Widget build(BuildContext context) {
return Scaffold(
body: Container(
padding: EdgeInsets.all(50),
child: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
crossAxisAlignment: CrossAxisAlignment.center,
children: <Widget>[
Hero(tag: 'logo',
child: Container(width: 120.0,
child: Image.asset("assets/logo.png"),)),
SizedBox(height: 40,),
FittedBox(fit: BoxFit.fitWidth,
child: FlatButton(
shape: RoundedRectangleBorder(borderRadius: BorderRadius.circular(20)),
onPressed: () {onGoogleSignIn(context);},
color: Colors.blueAccent,
child: Padding(
padding: EdgeInsets.all(10),
child: Row(
mainAxisAlignment: MainAxisAlignment.center,
crossAxisAlignment: CrossAxisAlignment.center,
children: <Widget>[
Image(image: AssetImage("assets/google_logo.png"), height: 35.0),
SizedBox(width: 15),
Text('Entrar com Google',style: TextStyle(color: Colors.white, fontSize:20
),),],),),),),],),),),);}}