我通过 block 向服务器发送数据,并在发送过程中显示 progressSnackBar
,然后在成功时显示 successSnackBar
。有时,这需要不到一秒的时间,并且根本不显示 progressSnackBar
是有意义的 - 换句话说等待一秒钟,然后检查状态是否仍然是 UpdatingAccount
。我尝试过使用涉及 Future.delay(...)
的不同组合,但都失败了,我可能可以进行 setState
hack,但有没有办法在内部实现此目的 Bloc 监听者?
BlocListener<AccountBloc, AccountState>(
listener: (BuildContext context, state) {
if (state is UpdatingAccount) { // <-- delay this
Scaffold.of(context)
..hideCurrentSnackBar()
..showSnackBar(progressSnackBar());
} else if (state is AccountUpdated) {
Scaffold.of(context)
..hideCurrentSnackBar()
..showSnackBar(successSnackBar());
}
},
// rest...
),
最佳答案
我最终使小部件成为有状态的,并给它一个 _updated
bool 成员。
BlocListener<AccountBloc, AccountState>(
listener: (BuildContext context, state) {
if (state is UpdatingAccount) {
_updated = false;
Future.delayed(Duration(seconds: 1), () {
if (!_updated) {
Scaffold.of(context)
..hideCurrentSnackBar()
..showSnackBar(progressSnackBar());
}
});
} else if (state is AccountUpdated) {
_updated = true;
Scaffold.of(context)
..hideCurrentSnackBar()
..showSnackBar(successSnackBar());
}
},
// rest...
),
关于flutter - Flutter block 监听器内的延迟状态检查,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59143436/