Flutter 让用户保持登录状态
我正在Flitter中运行一个应用程序,我正在处理身份验证部分。我想知道如何在重新加载应用程序后保持用户登录。这是我用来记录日志的代码,我已经使用了共享首选项,但仍然不起作用。我应该在代码中添加什么或更改什么?我应该在主页()中添加什么吗? 我忘了提到我打开应用程序时的第一个页面是main.dart,第二个页面是LoginScreen(在这里你可以选择你登录的账户类型,例如:facebook、谷歌、电子邮件)Flutter 让用户保持登录状态,flutter,Flutter,我正在Flitter中运行一个应用程序,我正在处理身份验证部分。我想知道如何在重新加载应用程序后保持用户登录。这是我用来记录日志的代码,我已经使用了共享首选项,但仍然不起作用。我应该在代码中添加什么或更改什么?我应该在主页()中添加什么吗? 我忘了提到我打开应用程序时的第一个页面是main.dart,第二个页面是LoginScreen(在这里你可以选择你登录的账户类型,例如:facebook、谷歌、电子邮件) 导入“包装:颤振/材料.省道”; 导入“SignUp.dart”; 进口“brazie
导入“包装:颤振/材料.省道”;
导入“SignUp.dart”;
进口“brazierContainer.dart”;
导入“package:google_fonts/google_fonts.dart”;
导入“LoginScreen.dart”;
导入“HomePage.dart”;
导入“auth.dart”;
导入“package:shared_preferences/shared_preferences.dart”;
Future main()异步{
WidgetsFlutterBinding.ensureInitialized();
SharedReferences prefs=等待SharedReferences.getInstance();
var email=prefs.getString('email');
打印(电子邮件);
runApp(MaterialApp(home:email==null?LoginPage():Homepage());
}
类LoginPage扩展StatefulWidget{
LoginPage({Key-Key,this.title}):super(Key:Key);
最后的字符串标题;
@凌驾
_LoginPagentate createState()=>_LoginPagentate();
}
类_loginpagentate扩展状态{
final _formKey=GlobalKey();
字符串email='';
字符串密码=“”;
字符串错误=“”;
布尔加载=假;
最终身份验证=身份验证();
@凌驾
小部件构建(构建上下文){
回装
?主页()
:脚手架(
resizeToAvoidBottomInset:false,
resizeToAvoidBottomPadding:false,
主体:容器(
高度:900.0,
宽度:500.0,
子:列(
儿童:[
填充物(
填充:仅限常量边集(顶部:50.0),
孩子:排(
儿童:[
图标按钮(
图标:图标(Icons.arrow\u back\u ios,颜色:Colors.blue),
已按下:(){
导航器。更换(
上下文
材料路线(
生成器:(context)=>LoginScreen());
},
)
],
),
),
填充物(
填充:仅限常量边集(顶部:40.0),
孩子:RichText(
text:TextSpan(
文字:“Tariffo”,
样式:TextStyle(
颜色:颜色,蓝色,
fontFamily:“签名画家”,
尺寸:60),,
),
),
),
大小盒子(
高度:400.0,
孩子:表格(
键:_formKey,
子:列(
儿童:[
填充物(
填充:仅限常量边设置(
左:40.0,右:40.0,顶部:40.0),
子项:TextFormField(
验证器:(val)=>
val.isEmpty?“输入电子邮件”:null,
一旦更改:(val){
设置状态(()=>email=val);
},
样式:TextStyle(颜色:Colors.black),
装饰:输入装饰(
hintText:“输入电子邮件”,
hintStyle:TextStyle(
fontFamily:“Antra”,
字体大小:12.0,
颜色:颜色。黑色),
),
),
填充物(
填充:仅限常量边设置(
左:40.0,右:40.0,顶部:40.0),
子项:TextFormField(
验证器:(val)=>val.length<8
?“输入密码>8位”
:null,
一旦更改:(val){
设置状态(()=>password=val);
},
样式:TextStyle(颜色:Colors.black),
装饰:输入装饰(
hintText:“输入密码”,
hintStyle:TextStyle(
fontFamily:“Antra”,
字体大小:12.0,
颜色:颜色。黑色),
蒙昧文字:对,
),
),
尺寸箱(高度:50),
填充物(
填充:仅限常量边集(顶部:40.0),
子:材质按钮(
身高:50,
最小宽度:300,
形状:圆形矩形边框(
边界半径:边界半径。圆形(10)),
颜色:颜色,蓝色,
onPressed:()异步{
共享引用优先级=
等待SharedReferences.getInstance();
import 'package:flutter/material.dart';
import 'SignUp.dart';
import 'brazierContainer.dart';
import 'package:google_fonts/google_fonts.dart';
import 'LoginScreen.dart';
import 'HomePage.dart';
import 'auth.dart';
import 'package:shared_preferences/shared_preferences.dart';
Future<void> main() async {
WidgetsFlutterBinding.ensureInitialized();
SharedPreferences prefs = await SharedPreferences.getInstance();
var email = prefs.getString('email');
print(email);
runApp(MaterialApp(home: email == null ? LoginPage() : Homepage()));
}
class LoginPage extends StatefulWidget {
LoginPage({Key key, this.title}) : super(key: key);
final String title;
@override
_LoginPageState createState() => _LoginPageState();
}
class _LoginPageState extends State<LoginPage> {
final _formKey = GlobalKey<FormState>();
String email = '';
String password = '';
String error = '';
bool loading = false;
final Authentication authentication = Authentication();
@override
Widget build(BuildContext context) {
return loading
? Homepage()
: Scaffold(
resizeToAvoidBottomInset: false,
resizeToAvoidBottomPadding: false,
body: Container(
height: 900.0,
width: 500.0,
child: Column(
children: <Widget>[
Padding(
padding: const EdgeInsets.only(top: 50.0),
child: Row(
children: <Widget>[
IconButton(
icon: Icon(Icons.arrow_back_ios, color: Colors.blue),
onPressed: () {
Navigator.pushReplacement(
context,
MaterialPageRoute(
builder: (context) => LoginScreen()));
},
)
],
),
),
Padding(
padding: const EdgeInsets.only(top: 40.0),
child: RichText(
text: TextSpan(
text: 'Tariffo',
style: TextStyle(
color: Colors.blue,
fontFamily: 'SignPainter',
fontSize: 60),
),
),
),
SizedBox(
height: 400.0,
child: Form(
key: _formKey,
child: Column(
children: <Widget>[
Padding(
padding: const EdgeInsets.only(
left: 40.0, right: 40.0, top: 40.0),
child: TextFormField(
validator: (val) =>
val.isEmpty ? 'enter email' : null,
onChanged: (val) {
setState(() => email = val);
},
style: TextStyle(color: Colors.black),
decoration: InputDecoration(
hintText: 'enter email',
hintStyle: TextStyle(
fontFamily: 'Antra',
fontSize: 12.0,
color: Colors.black)),
),
),
Padding(
padding: const EdgeInsets.only(
left: 40.0, right: 40.0, top: 40.0),
child: TextFormField(
validator: (val) => val.length < 8
? 'enter password > 8 digits'
: null,
onChanged: (val) {
setState(() => password = val);
},
style: TextStyle(color: Colors.black),
decoration: InputDecoration(
hintText: 'enter password',
hintStyle: TextStyle(
fontFamily: 'Antra',
fontSize: 12.0,
color: Colors.black)),
obscureText: true,
),
),
SizedBox(height: 50),
Padding(
padding: const EdgeInsets.only(top: 40.0),
child: MaterialButton(
height: 50,
minWidth: 300,
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(10)),
color: Colors.blue,
onPressed: () async {
SharedPreferences prefs =
await SharedPreferences.getInstance();
prefs.setString(
'email', 'useremail@gmail.com');
if (_formKey.currentState.validate()) {
setState(() => loading = true);
dynamic result = await authentication
.signUpWithEmailAndPassword(
email, password);
if (result == null) {
setState(() => error =
'Sorry,These credentials will not work out');
loading = false;
}
}
},
child: Text(
'Sign in',
style: TextStyle(
fontFamily: 'Antra', color: Colors.white),
),
),
),
Container(
padding: EdgeInsets.symmetric(
vertical: 10, horizontal: 10),
alignment: Alignment.centerRight,
child: Text('Forgot Password ?',
style: TextStyle(
fontSize: 14,
fontWeight: FontWeight.w500)),
),
],
),
)),
_createAccountLabel(),
],
),
),
);
}
Widget _createAccountLabel() {
return InkWell(
onTap: () {
Navigator.push(
context, MaterialPageRoute(builder: (context) => SignupPage()));
},
child: Container(
margin: EdgeInsets.symmetric(vertical: 20),
padding: EdgeInsets.all(15),
alignment: Alignment.bottomCenter,
child: Row(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
Text(
"Don't you have an account ?",
style: TextStyle(fontSize: 13, fontWeight: FontWeight.w600),
),
SizedBox(
width: 10,
),
Text(
'Register',
style: TextStyle(
color: Colors.blue,
fontSize: 13,
fontWeight: FontWeight.w600),
),
],
),
),
);
}
}