Flutter firebase身份验证后,将不同用户重定向到颤振中的不同屏幕
我正在创建一个应用程序,不同的用户应该被重定向到不同的页面。如果其xyz@xyz.com,他应该被重定向到SpecialUserHome,所有其他用户都应该被重定向到UserHome。我是个新手,有人能帮我怎么做吗?这是我的地图Flutter firebase身份验证后,将不同用户重定向到颤振中的不同屏幕,flutter,dart,firebase-authentication,Flutter,Dart,Firebase Authentication,我正在创建一个应用程序,不同的用户应该被重定向到不同的页面。如果其xyz@xyz.com,他应该被重定向到SpecialUserHome,所有其他用户都应该被重定向到UserHome。我是个新手,有人能帮我怎么做吗?这是我的地图 class MappingPage extends StatefulWidget { final AuthImplementation auth; MappingPage({ this.auth, }); State<StatefulWi
class MappingPage extends StatefulWidget {
final AuthImplementation auth;
MappingPage({
this.auth,
});
State<StatefulWidget> createState() {
return _MappingPageState();
}
}
enum AuthStatus {
notSignedIn,
signedIn,
}
class _MappingPageState extends State<MappingPage> {
AuthStatus authStatus = AuthStatus.notSignedIn;
@override
void initState() {
super.initState();
widget.auth.getCurrentUser().then((firebaseUserId) {
setState(() {
authStatus = firebaseUserId == null
? AuthStatus.notSignedIn
: AuthStatus.signedIn;
});
});
}
void _signedIn() {
setState(() {
authStatus = AuthStatus.signedIn;
});
}
void _signOut() {
setState(() {
authStatus = AuthStatus.notSignedIn;
});
}
@override
Widget build(BuildContext context) {
switch (authStatus) {
case AuthStatus.notSignedIn:
return LoginRegisterPage(
auth: widget.auth,
onSignedIn: _signedIn,
);
case AuthStatus.signedIn:
return UserHome(
auth: widget.auth,
onSignedOut: _signOut,
);
}
return null;
}
}
class MappingPage扩展StatefulWidget{
最终授权执行授权;
绘图页({
这个.auth,
});
状态createState(){
返回_MappingPageState();
}
}
枚举身份验证状态{
没有签名,
签名,
}
类_MappingPageState扩展状态{
AuthStatus AuthStatus=AuthStatus.notSignedIn;
@凌驾
void initState(){
super.initState();
widget.auth.getCurrentUser().then((firebaseUserId){
设置状态(){
authStatus=firebaseUserId==null
?AuthStatus.notSignedIn
:AuthStatus.signedIn;
});
});
}
void _signedIn(){
设置状态(){
authStatus=authStatus.signedIn;
});
}
void _signOut(){
设置状态(){
authStatus=authStatus.notSignedIn;
});
}
@凌驾
小部件构建(构建上下文){
交换机(身份验证状态){
案例AuthStatus.notSignedIn:
返回登录注册页面(
auth:widget.auth,
onSignedIn:_signedIn,
);
案例AuthStatus.signedIn:
返回用户主页(
auth:widget.auth,
onSignedOut:_signOut,
);
}
返回null;
}
}
还有我的login.dart
class LoginPage extends StatefulWidget {
LoginPage({
this.auth,
this.onSignedIn,
});
final AuthImplementation auth;
final VoidCallback onSignedIn;
State<StatefulWidget> createState() {
return _LoginState();
}
}
enum FormType { login, register }
class _LoginState extends State<LoginPage> {
final formKey = GlobalKey<FormState>();
FormType _formType = FormType.login;
String _email = '';
String _password = '';
//methods
bool validateAndSave() {
final form = formKey.currentState;
if (form.validate()) {
form.save();
return true;
} else {
return false;
}
}
void validateAndSubmit() async {
if (validateAndSave()) {
try {
if (_formType == FormType.login) {
String userId = await widget.auth.signIn(_email, _password);
dialogBox.information(
context, 'Congratulations!', 'You logged in successfully');
print('Login user id =' + userId);
} else {
String userId = await widget.auth.signUp(_username,_email, _mobile, _password);
dialogBox.information(context, 'Congratulations!',
'Your account has been created successfully');
print('Register user id =' + userId);
}
widget.onSignedIn();
} catch (e) {
print(e.toString());
}
}
}
void moveToLogin() {
formKey.currentState.reset();
setState(() {
_formType = FormType.login;
});
}
//Design
@override
Widget build(BuildContext context) {
return Scaffold(
body: Container(
margin: EdgeInsets.all(15.0),
child: Form(
key: formKey,
child: ListView(
children: <Widget>[
child: Column(crossAxisAlignment: CrossAxisAlignment.stretch,
children: createInputs() + createButtons(),
),
),
],
),
),
),
);
}
List<Widget> createInputs() {
return [
TextFormField(
validator: (value) {
return value.isEmpty ? 'please provide email' : null;
},
onSaved: (value) {
return _email = value;
},
),
obscureText: true,
validator: (value) {
return value.isEmpty ? 'password is required!' : null;
},
onSaved: (value) {
return _password = value;
},
),
];
}
List<Widget> createButtons() {
if (_formType == FormType.login) {
return [
RaisedButton(
child: Text('Login'),
onPressed: validateAndSubmit,
),
];
}
}
}
class LoginPage扩展StatefulWidget{
登录页({
这个.auth,
这是我的名字,
});
最终授权执行授权;
最终作废已签字;
状态createState(){
返回_LoginState();
}
}
枚举表单类型{login,register}
类_LoginState扩展了状态{
final formKey=GlobalKey();
FormType _FormType=FormType.login;
字符串_email='';
字符串_password='';
//方法
bool validateAndSave(){
最终形式=formKey.currentState;
if(form.validate()){
form.save();
返回true;
}否则{
返回false;
}
}
void validateAndSubmit()异步{
if(validateAndSave()){
试一试{
if(_formType==formType.login){
字符串userId=wait widget.auth.sign(\u email,\u password);
对话框.信息(
上下文,“恭喜!”,“您已成功登录”);
打印('登录用户id='+用户id);
}否则{
字符串userId=wait widget.auth.signUp(\u用户名、\u电子邮件、\u手机、\u密码);
dialogBox.information(上下文“恭喜!”,
“您的帐户已成功创建”);
打印(“注册用户id=”+用户id);
}
widget.onSignedIn();
}捕获(e){
打印(如toString());
}
}
}
void moveToLogin(){
formKey.currentState.reset();
设置状态(){
_formType=formType.login;
});
}
//设计
@凌驾
小部件构建(构建上下文){
返回脚手架(
主体:容器(
边距:所有边缘集(15.0),
孩子:表格(
key:formKey,
子:ListView(
儿童:[
子项:列(crossAxisAlignment:crossAxisAlignment.stretch,
子项:createInputs()+createButtons(),
),
),
],
),
),
),
);
}
列表createInputs(){
返回[
TextFormField(
验证器:(值){
return value.isEmpty?“请提供电子邮件”:null;
},
已保存:(值){
返回_email=value;
},
),
蒙昧文字:对,
验证器:(值){
返回值.isEmpty?“需要密码!”:null;
},
已保存:(值){
返回_password=value;
},
),
];
}
列表创建按钮(){
if(_formType==formType.login){
返回[
升起的按钮(
子项:文本('Login'),
onPressed:validateAndSubmit,
),
];
}
}
}
您应该查看提供程序
颤振插件。下面是YouTube上的一个优秀教程,介绍如何在Flatter中使用firebase实现用户身份验证登录。您应该查看flatter的provider
插件。下面是YouTube上的一个优秀教程,介绍如何在Flatter中使用firebase实现用户身份验证登录。