我在理解 BLoC 模式中的一种行为时遇到了问题。使用 flutter_bloc 插件
我基于这个做了一个项目:https ://felangel.github.io/bloc/#/flutterlogintutorial
没什么特别的。我有两个带有按钮的页面,可以在它们之间导航。因此,如果我转到第二页然后返回登录页面并专注于任何输入字段,我会看到一个额外的输出(“状态”);
一步步:
- 启动应用程序 -> Flutter 调试控制台:LoginInitial
- 按“第二页”
- 按'登录页面'->颤振调试控制台:LoginInitial
- 专注于任何输入字段->颤振调试控制台:LoginInitial
任何想法为什么会发生在第四步?
登录页面:
Widget build(BuildContext context) {
return BlocBuilder<LoginEvent, LoginState>(
bloc: _loginBloc,
builder: (
BuildContext context,
LoginState state,
) {
print(' $state');
if (state is LoginFailure) {
_onWidgetDidBuild(() {
Scaffold.of(context).showSnackBar(
SnackBar(
content: Text('${state.error}'),
backgroundColor: Colors.red,
),
);
});
}
return Form(
child: Column(
children: [
TextFormField(
decoration: InputDecoration(labelText: 'username'),
controller: _usernameController,
),
TextFormField(
decoration: InputDecoration(labelText: 'password'),
controller: _passwordController,
obscureText: true,
),
RaisedButton(
onPressed:
state is! LoginLoading ? _onLoginButtonPressed : null,
child: Text('Login'),
),
Container(
child:
state is LoginLoading ? CircularProgressIndicator() : null,
),
],
),
);
},
);
}