如何使用 sharedpreferences 保存用户角色并使用它们使用户保持登录状态并根据 flutter 中的角色导航到特定页面
How to use sharedpreferences to save users roles and use them to keep the user logged and naviagte to a specific page depending on role in flutter
我正在开发一个医生咨询应用程序,我必须进行用户登录(医生登录和客户登录)。
我的问题是,在关闭应用程序并重新启动后,如果医生已登录,它将转到客户的主页而不是医生的主页。在我的启动文件中,我有`
class HomeController extends StatelessWidget {
@override
Widget build(BuildContext context) {
final AuthService auth = Provider.of(context).auth;
return StreamBuilder<String>(
stream: auth.onAuthStateChanged,
builder: (context, AsyncSnapshot<String> snapshot) {
if (snapshot.connectionState == ConnectionState.active) {
final bool signedIn = snapshot.hasData;
return signedIn ? UsersHome() : OnboardingScreen();
}
return CircularProgressIndicator();
},
);
}`
I
我想使用共享首选项在应用程序本地保存用户角色。因此,如果客户登录,它将转到 ClientHome,如果医生登录,它将转到 DoctorHome。
有没有其他选择。我的后端是 firebase
我需要帮助
这是我的登录页面类似于医生登录
enum AuthMode { 注册,登录 }
final primaryColor = Colors.blue;
enum AuthFormType { signIn, signUp, reset, anonymous, convert }
class Login extends StatefulWidget {
final AuthFormType authFormType;
Login({Key key, @required this.authFormType}) : super(key: key);
@override
_LoginState createState() =>
_LoginState(authFormType: this.authFormType);
}
class _LoginState extends State<Login> {
AuthFormType authFormType;
_LoginState({this.authFormType});
final formKey = GlobalKey<FormState>();
String _email, _password, _name, _warning;
void switchFormState(String state) {
formKey.currentState.reset();
if (state == "signUp") {
setState(() {
authFormType = AuthFormType.signUp;
});
} else if (state == 'home') {
Navigator.of(context).pop();
} else {
setState(() {
authFormType = AuthFormType.signIn;
});
}
}
bool validate() {
final form = formKey.currentState;
if (authFormType == AuthFormType.anonymous) {
return true;
}
form.save();
if (form.validate()) {
form.save();
return true;
} else {
return false;
}
}
void submit() async {
if (validate()) {
try {
final auth = Provider.of(context).auth;
switch (authFormType) {
case AuthFormType.signIn:
await auth.signInWithEmailAndPassword(_email, _password);
Navigator.of(context).pushReplacementNamed('/home');
break;
case AuthFormType.signUp:
await auth.createUserWithEmailAndPassword
(
_email, _password, _name);
Navigator.of(context).pushReplacementNamed('/home');
break;
case AuthFormType.reset:
await auth.sendPasswordResetEmail(_email);
_warning = "A password reset link has been sent to $_email";
setState(() {
authFormType = AuthFormType.signIn;
});
break;
case AuthFormType.anonymous:
await auth.singInAnonymously();
Navigator.of(context).pushReplacementNamed('/home');
break;
case AuthFormType.convert:
await auth.convertUserWithEmail(_email, _password, _name);
Navigator.of(context).pop();
break;
}
} catch (e) {
print(e);
setState(() {
_warning = e.message;
});
}
}
}
@override
Widget build(BuildContext context) {
final _auth = Provider.of(context).auth;
return Scaffold(
body: Container(
width: double.infinity,
decoration: BoxDecoration(
color: Color(0xFF5bd75b)
),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: <Widget>[
SizedBox(height: 80,),
Padding(
padding: EdgeInsets.all(20),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start, //m8Oq9ycK9VntE8mk8om5yoQbB10= first one
children: <Widget>[
Center(child: FadeAnimation(1, buildHeaderText(),), ),
SizedBox(height: 10,),
],
),
),
SizedBox(height: 20),
Expanded(
child: Container(
decoration: BoxDecoration(
color: Color(0xFF32cd32),
borderRadius: BorderRadius.only(topLeft: Radius.circular(60), topRight: Radius.circular(60))
),
child: SingleChildScrollView(
child: Padding(
padding: EdgeInsets.all(30),
child: Column(
children: <Widget>[
SizedBox(height: 0,),
FadeAnimation(1.4, Container(
child: Column(
children: <Widget>[
showAlert(),
Padding(
padding: const EdgeInsets.all(20.0),
child: Form(
key: formKey,
child: Column(
children: buildInputs(),
),
),
),
],
),
)),
SizedBox(height: 0,),
FadeAnimation(2,
Container(
child: Column(
children:buildButtons(),
),
)
),
SizedBox(height: 0,),
FadeAnimation(1.7, Text("Continue with social media", style: TextStyle(color: Colors.white),)),
SizedBox(height: 20,),
Row(
children: <Widget>[
Expanded(
child: FadeAnimation(1.8, Container(
height: 50,
decoration: BoxDecoration(
borderRadius: BorderRadius.circular(50),
color: Colors.blue
),
child: Center(
child: FlatButton(
child: Text("Facebook", style: TextStyle(color: Colors.white, fontWeight: FontWeight.bold),),
onPressed: (){
},
)
),
)),
),
SizedBox(width: 30,),
Expanded(
child: FadeAnimation(1.9, Container(
height: 50,
decoration: BoxDecoration(
borderRadius: BorderRadius.circular(50),
color: Colors.white
),
child: Center(
child: FlatButton(
child: Text("Google", style: TextStyle(color: Colors.redAccent, fontWeight: FontWeight.bold),),
onPressed: () async {
try {
if(authFormType == AuthFormType.convert) {
await _auth.convertWithGoogle();
Navigator.of(context).pop();
} else {
await _auth.signInWithGoogle();
Navigator.of(context).pushReplacementNamed('/home');
}
} catch (e) {
setState(() {
print(e);
_warning = e.message;
});
}
},
)
),
)),
)
],
),
SizedBox(height: 0),
FadeAnimation(3, FlatButton(child:Text('By continueing, you agree to Terms & Conditions', style: TextStyle(color: Colors.white, fontSize: 12, ), ),
onPressed: (){
Navigator.of(context).pushReplacementNamed('/Terms');
},
)
)
],
),
),
),
),
)
],
),
),
);
}
Widget showAlert() {
if (_warning != null) {
return Container(
color: Colors.amberAccent,
width: double.infinity,
padding: EdgeInsets.all(8.0),
child: Row(
children: <Widget>[
Padding(
padding: const EdgeInsets.only(right: 8.0),
child: Icon(Icons.error_outline),
),
Expanded(
child: AutoSizeText(
_warning,
maxLines: 3,
),
),
Padding(
padding: const EdgeInsets.only(left: 8.0),
child: IconButton(
icon: Icon(Icons.close),
onPressed: () {
setState(() {
_warning = null;
});
},
),
)
],
),
);
}
return SizedBox(
height: 0,
);
}
AutoSizeText buildHeaderText() {
String _headerText;
if (authFormType == AuthFormType.signIn) {
_headerText = "Sign In";
} else if (authFormType == AuthFormType.reset) {
_headerText = "Reset Password";
} else {
_headerText = "Create New Account";
}
return AutoSizeText(
_headerText,
maxLines: 1,
textAlign: TextAlign.center,
style: TextStyle(
fontSize: 35,
color: Colors.white,
),
);
}
List<Widget> buildInputs() {
List<Widget> textFields = [];
if (authFormType == AuthFormType.reset) {
textFields.add(
TextFormField(
validator: EmailValidator.validate,
style: TextStyle(fontSize: 22.0),
decoration: buildSignUpInputDecoration("Email"),
onSaved: (value) => _email = value,
),
);
textFields.add(SizedBox(height: 20));
return textFields;
}
// if were in the sign up state add name
if ([AuthFormType.signUp, AuthFormType.convert].contains(authFormType)) {
textFields.add(
TextFormField(
validator: NameValidator.validate,
style: TextStyle(fontSize: 22.0),
decoration: buildSignUpInputDecoration("Full Name"),
onSaved: (value) => _name = value,
),
);
textFields.add(SizedBox(height: 20));
}
// add email & password
textFields.add(
TextFormField(
validator: EmailValidator.validate,
style: TextStyle(fontSize: 22.0),
decoration: buildSignUpInputDecoration("Email"),
onSaved: (value) => _email = value,
),
);
textFields.add(SizedBox(height: 20));
textFields.add(
TextFormField(
validator: PasswordValidator.validate,
style: TextStyle(fontSize: 22.0),
decoration: buildSignUpInputDecoration("Password"),
obscureText: true,
onSaved: (value) => _password = value,
),
);
textFields.add(SizedBox(height: 20));
return textFields;
}
InputDecoration buildSignUpInputDecoration(String hint) {
return InputDecoration(
hintText: hint,
filled: true,
fillColor: Colors.white,
focusColor: Colors.white,
enabledBorder: OutlineInputBorder(
borderSide: BorderSide(color: Colors.white, width: 0.0)),
contentPadding:
const EdgeInsets.only(left: 14.0, bottom: 10.0, top: 10.0),
);
}
List<Widget> buildButtons() {
String _switchButtonText, _newFormState, _submitButtonText;
bool _showForgotPassword = false;
if (authFormType == AuthFormType.signIn) {
_switchButtonText = "Create New Account";
_newFormState = "signUp";
_submitButtonText = "Sign In";
_showForgotPassword = true;
} else if (authFormType == AuthFormType.reset) {
_switchButtonText = "Return to Sign In";
_newFormState = "signIn";
_submitButtonText = "Submit";
} else if (authFormType == AuthFormType.convert) {
_switchButtonText = "Cancel";
_newFormState = "home";
_submitButtonText = "Sign Up";
} else {
_switchButtonText = "Have an Account? Sign In";
_newFormState = "signIn";
_submitButtonText = "Sign Up";
}
return [
Container(
width: MediaQuery.of(context).size.width * 0.7,
child: RaisedButton(
shape:
RoundedRectangleBorder(borderRadius: BorderRadius.circular(30.0)),
color: Colors.white,
textColor: primaryColor,
child: Padding(
padding: const EdgeInsets.all(8.0),
child: Text(
_submitButtonText,
style: TextStyle(fontSize: 20.0, fontWeight: FontWeight.w300),
),
),
onPressed: submit,
),
),
showForgotPassword(_showForgotPassword),
FlatButton(
child: Text(
_switchButtonText,
style: TextStyle(color: Colors.white),
),
onPressed: () {
switchFormState(_newFormState);
},
),
];
}
List<Widget> buildOre() {
String _switchButtonText;
bool _showForgotPassword = false;
if (authFormType == AuthFormType.signIn) {
_switchButtonText = "Create New Account";
_showForgotPassword = true;
} else if (authFormType == AuthFormType.reset) {
_switchButtonText = "Return to Sign In";
} else if (authFormType == AuthFormType.convert) {
_switchButtonText = "Cancel";
} else {
_switchButtonText = "Have an Account? Sign In";
}
return [
Container(
width: MediaQuery.of(context).size.width * 0.7,
child: RaisedButton(
shape:
RoundedRectangleBorder(borderRadius: BorderRadius.circular(30.0)),
color: Colors.white,
textColor: primaryColor,
child: Padding(
padding: const EdgeInsets.all(8.0),
child: Text(
''
),
),
onPressed: submit,
),
),
showForgotPassword(_showForgotPassword),
FlatButton(
child: Text(
_switchButtonText,
style: TextStyle(color: Colors.white),
),
onPressed: () {
},
),
];
}
Widget showForgotPassword(bool visible) {
return Visibility(
child: FlatButton(
child: Text(
"Forgot Password?",
style: TextStyle(color: Colors.white),
),
onPressed: () {
setState(() {
authFormType = AuthFormType.reset;
});
},
),
visible: visible,
);
}
}
THIS IS THE FIREBASE-FIRESTORE
THIS IS THE CODE
我假设 DoctorsHome
是您屏幕的名称,isDoctor
是指示用户角色是否为 'doctor' 的字段。
这样,如果用户已签名,您将根据 isDoctor 显示 DoctorHome 或 UsersHome,否则显示 OnboardingScreen。
编辑
if (snapshot.connectionState == ConnectionState.active) {
final bool signedIn = snapshot.hasData;
final String role = snapshot.data['role'];
final bool isDoctor;
if (role=='doctor') { isDoctor = true } else { isDoctor = false}
return signedIn ? (){ isDoctor ? DoctorsHome() : UsersHome() }() : OnboardingScreen();
}
我正在开发一个医生咨询应用程序,我必须进行用户登录(医生登录和客户登录)。 我的问题是,在关闭应用程序并重新启动后,如果医生已登录,它将转到客户的主页而不是医生的主页。在我的启动文件中,我有`
class HomeController extends StatelessWidget {
@override
Widget build(BuildContext context) {
final AuthService auth = Provider.of(context).auth;
return StreamBuilder<String>(
stream: auth.onAuthStateChanged,
builder: (context, AsyncSnapshot<String> snapshot) {
if (snapshot.connectionState == ConnectionState.active) {
final bool signedIn = snapshot.hasData;
return signedIn ? UsersHome() : OnboardingScreen();
}
return CircularProgressIndicator();
},
);
}`
I
我想使用共享首选项在应用程序本地保存用户角色。因此,如果客户登录,它将转到 ClientHome,如果医生登录,它将转到 DoctorHome。 有没有其他选择。我的后端是 firebase
我需要帮助 这是我的登录页面类似于医生登录 enum AuthMode { 注册,登录 }
final primaryColor = Colors.blue;
enum AuthFormType { signIn, signUp, reset, anonymous, convert }
class Login extends StatefulWidget {
final AuthFormType authFormType;
Login({Key key, @required this.authFormType}) : super(key: key);
@override
_LoginState createState() =>
_LoginState(authFormType: this.authFormType);
}
class _LoginState extends State<Login> {
AuthFormType authFormType;
_LoginState({this.authFormType});
final formKey = GlobalKey<FormState>();
String _email, _password, _name, _warning;
void switchFormState(String state) {
formKey.currentState.reset();
if (state == "signUp") {
setState(() {
authFormType = AuthFormType.signUp;
});
} else if (state == 'home') {
Navigator.of(context).pop();
} else {
setState(() {
authFormType = AuthFormType.signIn;
});
}
}
bool validate() {
final form = formKey.currentState;
if (authFormType == AuthFormType.anonymous) {
return true;
}
form.save();
if (form.validate()) {
form.save();
return true;
} else {
return false;
}
}
void submit() async {
if (validate()) {
try {
final auth = Provider.of(context).auth;
switch (authFormType) {
case AuthFormType.signIn:
await auth.signInWithEmailAndPassword(_email, _password);
Navigator.of(context).pushReplacementNamed('/home');
break;
case AuthFormType.signUp:
await auth.createUserWithEmailAndPassword
(
_email, _password, _name);
Navigator.of(context).pushReplacementNamed('/home');
break;
case AuthFormType.reset:
await auth.sendPasswordResetEmail(_email);
_warning = "A password reset link has been sent to $_email";
setState(() {
authFormType = AuthFormType.signIn;
});
break;
case AuthFormType.anonymous:
await auth.singInAnonymously();
Navigator.of(context).pushReplacementNamed('/home');
break;
case AuthFormType.convert:
await auth.convertUserWithEmail(_email, _password, _name);
Navigator.of(context).pop();
break;
}
} catch (e) {
print(e);
setState(() {
_warning = e.message;
});
}
}
}
@override
Widget build(BuildContext context) {
final _auth = Provider.of(context).auth;
return Scaffold(
body: Container(
width: double.infinity,
decoration: BoxDecoration(
color: Color(0xFF5bd75b)
),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: <Widget>[
SizedBox(height: 80,),
Padding(
padding: EdgeInsets.all(20),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start, //m8Oq9ycK9VntE8mk8om5yoQbB10= first one
children: <Widget>[
Center(child: FadeAnimation(1, buildHeaderText(),), ),
SizedBox(height: 10,),
],
),
),
SizedBox(height: 20),
Expanded(
child: Container(
decoration: BoxDecoration(
color: Color(0xFF32cd32),
borderRadius: BorderRadius.only(topLeft: Radius.circular(60), topRight: Radius.circular(60))
),
child: SingleChildScrollView(
child: Padding(
padding: EdgeInsets.all(30),
child: Column(
children: <Widget>[
SizedBox(height: 0,),
FadeAnimation(1.4, Container(
child: Column(
children: <Widget>[
showAlert(),
Padding(
padding: const EdgeInsets.all(20.0),
child: Form(
key: formKey,
child: Column(
children: buildInputs(),
),
),
),
],
),
)),
SizedBox(height: 0,),
FadeAnimation(2,
Container(
child: Column(
children:buildButtons(),
),
)
),
SizedBox(height: 0,),
FadeAnimation(1.7, Text("Continue with social media", style: TextStyle(color: Colors.white),)),
SizedBox(height: 20,),
Row(
children: <Widget>[
Expanded(
child: FadeAnimation(1.8, Container(
height: 50,
decoration: BoxDecoration(
borderRadius: BorderRadius.circular(50),
color: Colors.blue
),
child: Center(
child: FlatButton(
child: Text("Facebook", style: TextStyle(color: Colors.white, fontWeight: FontWeight.bold),),
onPressed: (){
},
)
),
)),
),
SizedBox(width: 30,),
Expanded(
child: FadeAnimation(1.9, Container(
height: 50,
decoration: BoxDecoration(
borderRadius: BorderRadius.circular(50),
color: Colors.white
),
child: Center(
child: FlatButton(
child: Text("Google", style: TextStyle(color: Colors.redAccent, fontWeight: FontWeight.bold),),
onPressed: () async {
try {
if(authFormType == AuthFormType.convert) {
await _auth.convertWithGoogle();
Navigator.of(context).pop();
} else {
await _auth.signInWithGoogle();
Navigator.of(context).pushReplacementNamed('/home');
}
} catch (e) {
setState(() {
print(e);
_warning = e.message;
});
}
},
)
),
)),
)
],
),
SizedBox(height: 0),
FadeAnimation(3, FlatButton(child:Text('By continueing, you agree to Terms & Conditions', style: TextStyle(color: Colors.white, fontSize: 12, ), ),
onPressed: (){
Navigator.of(context).pushReplacementNamed('/Terms');
},
)
)
],
),
),
),
),
)
],
),
),
);
}
Widget showAlert() {
if (_warning != null) {
return Container(
color: Colors.amberAccent,
width: double.infinity,
padding: EdgeInsets.all(8.0),
child: Row(
children: <Widget>[
Padding(
padding: const EdgeInsets.only(right: 8.0),
child: Icon(Icons.error_outline),
),
Expanded(
child: AutoSizeText(
_warning,
maxLines: 3,
),
),
Padding(
padding: const EdgeInsets.only(left: 8.0),
child: IconButton(
icon: Icon(Icons.close),
onPressed: () {
setState(() {
_warning = null;
});
},
),
)
],
),
);
}
return SizedBox(
height: 0,
);
}
AutoSizeText buildHeaderText() {
String _headerText;
if (authFormType == AuthFormType.signIn) {
_headerText = "Sign In";
} else if (authFormType == AuthFormType.reset) {
_headerText = "Reset Password";
} else {
_headerText = "Create New Account";
}
return AutoSizeText(
_headerText,
maxLines: 1,
textAlign: TextAlign.center,
style: TextStyle(
fontSize: 35,
color: Colors.white,
),
);
}
List<Widget> buildInputs() {
List<Widget> textFields = [];
if (authFormType == AuthFormType.reset) {
textFields.add(
TextFormField(
validator: EmailValidator.validate,
style: TextStyle(fontSize: 22.0),
decoration: buildSignUpInputDecoration("Email"),
onSaved: (value) => _email = value,
),
);
textFields.add(SizedBox(height: 20));
return textFields;
}
// if were in the sign up state add name
if ([AuthFormType.signUp, AuthFormType.convert].contains(authFormType)) {
textFields.add(
TextFormField(
validator: NameValidator.validate,
style: TextStyle(fontSize: 22.0),
decoration: buildSignUpInputDecoration("Full Name"),
onSaved: (value) => _name = value,
),
);
textFields.add(SizedBox(height: 20));
}
// add email & password
textFields.add(
TextFormField(
validator: EmailValidator.validate,
style: TextStyle(fontSize: 22.0),
decoration: buildSignUpInputDecoration("Email"),
onSaved: (value) => _email = value,
),
);
textFields.add(SizedBox(height: 20));
textFields.add(
TextFormField(
validator: PasswordValidator.validate,
style: TextStyle(fontSize: 22.0),
decoration: buildSignUpInputDecoration("Password"),
obscureText: true,
onSaved: (value) => _password = value,
),
);
textFields.add(SizedBox(height: 20));
return textFields;
}
InputDecoration buildSignUpInputDecoration(String hint) {
return InputDecoration(
hintText: hint,
filled: true,
fillColor: Colors.white,
focusColor: Colors.white,
enabledBorder: OutlineInputBorder(
borderSide: BorderSide(color: Colors.white, width: 0.0)),
contentPadding:
const EdgeInsets.only(left: 14.0, bottom: 10.0, top: 10.0),
);
}
List<Widget> buildButtons() {
String _switchButtonText, _newFormState, _submitButtonText;
bool _showForgotPassword = false;
if (authFormType == AuthFormType.signIn) {
_switchButtonText = "Create New Account";
_newFormState = "signUp";
_submitButtonText = "Sign In";
_showForgotPassword = true;
} else if (authFormType == AuthFormType.reset) {
_switchButtonText = "Return to Sign In";
_newFormState = "signIn";
_submitButtonText = "Submit";
} else if (authFormType == AuthFormType.convert) {
_switchButtonText = "Cancel";
_newFormState = "home";
_submitButtonText = "Sign Up";
} else {
_switchButtonText = "Have an Account? Sign In";
_newFormState = "signIn";
_submitButtonText = "Sign Up";
}
return [
Container(
width: MediaQuery.of(context).size.width * 0.7,
child: RaisedButton(
shape:
RoundedRectangleBorder(borderRadius: BorderRadius.circular(30.0)),
color: Colors.white,
textColor: primaryColor,
child: Padding(
padding: const EdgeInsets.all(8.0),
child: Text(
_submitButtonText,
style: TextStyle(fontSize: 20.0, fontWeight: FontWeight.w300),
),
),
onPressed: submit,
),
),
showForgotPassword(_showForgotPassword),
FlatButton(
child: Text(
_switchButtonText,
style: TextStyle(color: Colors.white),
),
onPressed: () {
switchFormState(_newFormState);
},
),
];
}
List<Widget> buildOre() {
String _switchButtonText;
bool _showForgotPassword = false;
if (authFormType == AuthFormType.signIn) {
_switchButtonText = "Create New Account";
_showForgotPassword = true;
} else if (authFormType == AuthFormType.reset) {
_switchButtonText = "Return to Sign In";
} else if (authFormType == AuthFormType.convert) {
_switchButtonText = "Cancel";
} else {
_switchButtonText = "Have an Account? Sign In";
}
return [
Container(
width: MediaQuery.of(context).size.width * 0.7,
child: RaisedButton(
shape:
RoundedRectangleBorder(borderRadius: BorderRadius.circular(30.0)),
color: Colors.white,
textColor: primaryColor,
child: Padding(
padding: const EdgeInsets.all(8.0),
child: Text(
''
),
),
onPressed: submit,
),
),
showForgotPassword(_showForgotPassword),
FlatButton(
child: Text(
_switchButtonText,
style: TextStyle(color: Colors.white),
),
onPressed: () {
},
),
];
}
Widget showForgotPassword(bool visible) {
return Visibility(
child: FlatButton(
child: Text(
"Forgot Password?",
style: TextStyle(color: Colors.white),
),
onPressed: () {
setState(() {
authFormType = AuthFormType.reset;
});
},
),
visible: visible,
);
}
}
THIS IS THE FIREBASE-FIRESTORE
THIS IS THE CODE
我假设 DoctorsHome
是您屏幕的名称,isDoctor
是指示用户角色是否为 'doctor' 的字段。
这样,如果用户已签名,您将根据 isDoctor 显示 DoctorHome 或 UsersHome,否则显示 OnboardingScreen。
编辑
if (snapshot.connectionState == ConnectionState.active) {
final bool signedIn = snapshot.hasData;
final String role = snapshot.data['role'];
final bool isDoctor;
if (role=='doctor') { isDoctor = true } else { isDoctor = false}
return signedIn ? (){ isDoctor ? DoctorsHome() : UsersHome() }() : OnboardingScreen();
}