Flutter setState() 不触发重建
Flutter setState() not triggering re-build
我有一个登录页面,我想在点击按钮时将其更改为 CupertinoActivityIndicator
。这是我的代码:
class AuthRegister extends StatefulWidget {
AuthRegister({Key key, this.authenticator}): super(key: key);
final FirebaseAuth authenticator;
@override
_AuthRegisterState createState() => _AuthRegisterState();
}
class _AuthRegisterState extends State<AuthRegister> {
final _formKey = GlobalKey<FormState>();
final _emailController = TextEditingController();
final _passwordController = TextEditingController();
bool _loading = false;
void _register(context) async {
setState(() {
_loading = true;
});
try {
sleep(const Duration(seconds: 2));
throw ("Debug");
} catch (e) {
print(e);
setState(() {
_loading = false;
});
Fluttertoast.showToast(msg: e);
}
}
@override
void dispose() {
_emailController.dispose();
_passwordController.dispose();
super.dispose();
}
@override
Widget build(BuildContext context) {
// I even tried this and it still fails
// if (_loading) {
// return CupertinoActivityIndicator();
// }
return CupertinoPageScaffold(
child: Form(
key: _formKey,
child: ListView(children: [
Text('CREATE AN ACCOUNT'),
Padding(
padding: EdgeInsets.symmetric(horizontal: 20),
child: MyInput(type: 'email', controller: _emailController)),
Padding(
padding: EdgeInsets.symmetric(horizontal: 20),
child: MyInput(type: 'password', controller: _passwordController)),
Padding(
padding: EdgeInsets.only(top: 36, bottom: 14, left: 45, right: 45),
// HERE ---- \/ \/ ----
child: (_loading) ?
CupertinoActivityIndicator() :
FullButton(
onPressed: () {
// I tried doing setState here but it didn't work
// setState(() {
// _loading = true;
// });
_register(context);
},
text: 'CREATE AN ACCOUNT',
fillColor: true),
),
// /HERE ---- /\ /\ ----
Center(
child: Text(
'FORGOT PASSWORD?',
style: TextStyle(
color: Color.fromRGBO(0xEE, 0x56, 0x42, 1.0),
fontFamily: 'Montserrat',
fontSize: 12),
),
),
]),
),
);
}
}
如您所见,我尝试了不同的方法。我一直在网上搜索,到处都是,似乎我做的一切都是正确的。在和我有同样问题的人中,我能找到的是用于执行条件的布尔变量是在某些函数中本地声明的,或者是使用 StatelessWidgets 等的人......
我做错了什么?
替换
sleep(const Duration(seconds: 2));
和
Future.delayed(Duration(seconds: 2)).then((_) {
throw ("Debug");
});
我有一个登录页面,我想在点击按钮时将其更改为 CupertinoActivityIndicator
。这是我的代码:
class AuthRegister extends StatefulWidget {
AuthRegister({Key key, this.authenticator}): super(key: key);
final FirebaseAuth authenticator;
@override
_AuthRegisterState createState() => _AuthRegisterState();
}
class _AuthRegisterState extends State<AuthRegister> {
final _formKey = GlobalKey<FormState>();
final _emailController = TextEditingController();
final _passwordController = TextEditingController();
bool _loading = false;
void _register(context) async {
setState(() {
_loading = true;
});
try {
sleep(const Duration(seconds: 2));
throw ("Debug");
} catch (e) {
print(e);
setState(() {
_loading = false;
});
Fluttertoast.showToast(msg: e);
}
}
@override
void dispose() {
_emailController.dispose();
_passwordController.dispose();
super.dispose();
}
@override
Widget build(BuildContext context) {
// I even tried this and it still fails
// if (_loading) {
// return CupertinoActivityIndicator();
// }
return CupertinoPageScaffold(
child: Form(
key: _formKey,
child: ListView(children: [
Text('CREATE AN ACCOUNT'),
Padding(
padding: EdgeInsets.symmetric(horizontal: 20),
child: MyInput(type: 'email', controller: _emailController)),
Padding(
padding: EdgeInsets.symmetric(horizontal: 20),
child: MyInput(type: 'password', controller: _passwordController)),
Padding(
padding: EdgeInsets.only(top: 36, bottom: 14, left: 45, right: 45),
// HERE ---- \/ \/ ----
child: (_loading) ?
CupertinoActivityIndicator() :
FullButton(
onPressed: () {
// I tried doing setState here but it didn't work
// setState(() {
// _loading = true;
// });
_register(context);
},
text: 'CREATE AN ACCOUNT',
fillColor: true),
),
// /HERE ---- /\ /\ ----
Center(
child: Text(
'FORGOT PASSWORD?',
style: TextStyle(
color: Color.fromRGBO(0xEE, 0x56, 0x42, 1.0),
fontFamily: 'Montserrat',
fontSize: 12),
),
),
]),
),
);
}
}
如您所见,我尝试了不同的方法。我一直在网上搜索,到处都是,似乎我做的一切都是正确的。在和我有同样问题的人中,我能找到的是用于执行条件的布尔变量是在某些函数中本地声明的,或者是使用 StatelessWidgets 等的人......
我做错了什么?
替换
sleep(const Duration(seconds: 2));
和
Future.delayed(Duration(seconds: 2)).then((_) {
throw ("Debug");
});