import 'package:flutter/material.dart';
import 'package:shared_preferences/shared_preferences.dart';
import './newauth.dart';
void main() {
runApp(Welcome());
}
var occupied;
var setter;
class Welcome extends StatefulWidget {
@override
_WelcomeState createState() => _WelcomeState();
}
class _WelcomeState extends State<Welcome> {
getVisitingFlagsignin() async{
SharedPreferences preferences = await SharedPreferences.getInstance();
setState(() {
occupied = preferences.getBool("alreadyVisited");
print(occupied);
});
}
@override
Widget build(BuildContext context) {
Scaffold loginScaffold = new Scaffold(
appBar: AppBar(
title: Text('LOGIN'),
backgroundColor: Colors.orange[700],
),
body: Center(
child: Container(
margin: EdgeInsets.all(15),
child: Column(
children: <Widget>[
Image.asset('assets/applogo.png'),
Padding(
padding: const EdgeInsets.all(40.0),
child: SizedBox(
width: 152,
child: RaisedButton(
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(50)),
child: Row(
children: <Widget>[
Image.asset(
'assets/Glogo.png',
height: 30,
),
Padding(
padding: const EdgeInsets.all(15.0),
child: Text(
'Login',
style: TextStyle(
fontSize: 15, fontFamily: 'Michroma'),
),
),
],
),
onPressed: () {
handleSignIn();
}),
),
)
],
),
),
),
);
在这里我称呼我的脚手架。我的问题是,当按下登录黄油时,我从另一个基于flutter文档的dart文件中调用了Google登录功能,在此没有太大更改。如果已经有用户登录,我可以成功登录并初始化应用程序的状态。按下登录按钮时,这就是我如何呼叫其他支架并可能将一些数据传递给它的地方。 / p>
Scaffold hompagescaffold = new Scaffold(
appBar: AppBar(
title: Text('Homepage'),
),
body: RaisedButton(child: Text('signout'),onPressed: (){signOutGoogle();},),
);
return new MaterialApp(home: occupied == true ? hompagescaffold:loginScaffold);
}
void initState() {
super.initState();
getVisitingFlagsignin();
}
}
这是我的auth.dart文件。因此,当按下登录按钮时,将调用handlesign in函数,然后运行所有必需的google登录并触发firebase身份验证,然后将调用共享的首选项以将我的布尔首选项设置为true。接下来我要做的是调出我的主页支架,以便替换登录支架。
import 'package:firebase_auth/firebase_auth.dart';
import 'package:google_sign_in/google_sign_in.dart';
import 'package:shared_preferences/shared_preferences.dart';
final FirebaseAuth _auth = FirebaseAuth.instance;
final GoogleSignIn _googleSignIn = GoogleSignIn();
setVisitingFlagsignin() async{
SharedPreferences preferences = await SharedPreferences.getInstance();
preferences.setBool("alreadyVisited", true);
print(preferences.getBool("alreadyVisited"));
}
Future<FirebaseUser> handleSignIn() async {
final GoogleSignInAccount googleUser = await _googleSignIn.signIn();
final GoogleSignInAuthentication googleAuth = await googleUser.authentication;
final AuthCredential credential = GoogleAuthProvider.getCredential(
accessToken: googleAuth.accessToken,
idToken: googleAuth.idToken,
);
final FirebaseUser user = (await _auth.signInWithCredential(credential)).user;
print("signed in " + user.displayName);
setVisitingFlagsignin();
return user;
}
setVisitingFlagsignout() async{
SharedPreferences preferences = await SharedPreferences.getInstance();
await preferences.setBool("alreadyVisited", false);
print (preferences.getBool("alreadyVisited"));
}
void signOutGoogle() async{
await _googleSignIn.signOut();
print ('User has signed out');
setVisitingFlagsignout();
}