Dart 创建MaterialApp后如何调用方法?
据我所知,创建MaterialApp实例将创建Navigator以及一些其他UI功能。当我的应用程序启动时,我想运行一个方法来检查用户是否登录,如果没有,则显示一个登录页面。我该怎么做呢?下面是我的主镖的开始Dart 创建MaterialApp后如何调用方法?,dart,flutter,Dart,Flutter,据我所知,创建MaterialApp实例将创建Navigator以及一些其他UI功能。当我的应用程序启动时,我想运行一个方法来检查用户是否登录,如果没有,则显示一个登录页面。我该怎么做呢?下面是我的主镖的开始 final scaffoldKey = new GlobalKey<ScaffoldState>(); final formKey = new GlobalKey<FormState>(); String _email; String _password; vo
final scaffoldKey = new GlobalKey<ScaffoldState>();
final formKey = new GlobalKey<FormState>();
String _email;
String _password;
void _submit() {
final form = formKey.currentState;
if (form.validate()) {
form.save();
// Email & password matched our validation rules
// and are saved to _email and _password fields.
_performLogin();
}
}
void _performLogin() {
// This is just a demo, so no actual login here.
final snackbar = new SnackBar(
content: new Text('Email: $_email, password: $_password'),
);
scaffoldKey.currentState.showSnackBar(snackbar);
}
@override
Widget build(BuildContext context) {
return new Scaffold(
key: scaffoldKey,
appBar: new AppBar(
title: new Text('Validating forms'),
),
body: new Padding(
padding: const EdgeInsets.all(16.0),
child: new Form(
key: formKey,
child: new Column(
children: [
new TextFormField(
decoration: new InputDecoration(labelText: 'Email'),
validator: (val) =>
!val.contains('@') ? 'Not a valid email.' : null,
onSaved: (val) => _email = val,
),
new TextFormField(
decoration: new InputDecoration(labelText: 'Password'),
validator: (val) =>
val.length < 6 ? 'Password too short.' : null,
onSaved: (val) => _password = val,
obscureText: true,
),
new RaisedButton(
onPressed: _submit,
child: new Text('Login'),
),
],
),
),
),
);
}
final scaffoldKey=new GlobalKey();
final formKey=new GlobalKey();
字符串\u电子邮件;
字符串\u密码;
作废(提交){
最终形式=formKey.currentState;
if(form.validate()){
form.save();
//电子邮件和密码符合我们的验证规则
//并保存到_email和_password字段中。
_performLogin();
}
}
void _performLogin(){
//这只是一个演示,所以这里没有实际登录。
最终蛇形杆=新蛇形杆(
内容:新文本(“电子邮件:$\u电子邮件,密码:$\u密码”),
);
scaffoldKey.currentState.showSnackBar(snackbar);
}
@凌驾
小部件构建(构建上下文){
归还新脚手架(
钥匙:脚手架钥匙,
appBar:新的appBar(
标题:新文本(“验证表单”),
),
车身:新衬垫(
填充:常数边集全部(16.0),
儿童:新表格(
key:formKey,
子:新列(
儿童:[
新TextFormField(
装饰:新输入装饰(标签文本:“电子邮件”),
验证器:(val)=>
!val.contains(“@”)?“不是有效的电子邮件。”:null,
onSaved:(val)=>\u email=val,
),
新TextFormField(
装饰:新输入装饰(标签文本:“密码”),
验证器:(val)=>
val.length<6?“密码太短”:null,
onSaved:(val)=>_password=val,
蒙昧文字:对,
),
新升起的按钮(
按下按钮:_提交,
子项:新文本('Login'),
),
],
),
),
),
);
}
您可以将登录和主页分成两个单独的小部件,然后在顶级小部件的构建方法中有条件地使用它们
比如:
@override
build(BuildContext context) {
return new Container(
child: isLoggedIn ? HomeWidget : LoginWidget
)
}
通常,您可以使用FutureBuilder
检查您的方法,我在这里使用FirebaseAuth
:
return new FutureBuilder(future: FirebaseAuth.instance.currentUser(),
builder: (BuildContext context, AsyncSnapshot<FirebaseUser> user) {
///Check if user data != null
return user.hasData ? new HomeScreen(....) : new LoginScreen(...)
......
返回新的FutureBuilder(future:FirebaseAuth.instance.currentUser(),
生成器:(BuildContext上下文,异步快照用户){
///检查用户数据是否为空
return user.hasData?新建主屏幕(…):新建登录屏幕(…)
......
您还可以检查以更好地管理用户身份验证状态
好的,但是我如何运行该方法来检查用户在首次打开应用程序时是否已登录?