当前位置:网站首页>使用 BlocConsumer 同时构建响应式组件和监听状态
使用 BlocConsumer 同时构建响应式组件和监听状态
2022-07-07 21:34:00 【岛上码农@公众号同名】
前言
在上一篇Flutter 入门与实战(八十九):使用BlocListener处理状态变化中,我们利用了 BlocListener 监听状态变化并做一些相应的处理,同时使用了 BlocBuilder 构建了响应式界面。回顾代码,我们会发现存在冗余的部分,我们需要将 BlocBuilder 作为 BlocListener 的子组件,这其实有点别扭,因为 BlocBuilder构建的页面按说不应该是 BlocListener 的子组件,而是同级结构。那有没有更好的方式解决这个问题呢,事实上,flutter_bloc 给出了 BlocConsumer 来解决这一问题。
BlocConsumer实现机制
BlocConsumer 像是 BlocBuilder和 BlocListener 的聚合组件,支持构建响应式组件的同时监听状态变化。同时也支持按条件刷新组件或响应状态变化监听回调。BlocConsumer 的构造方法如下:
const BlocConsumer({
Key? key,
required this.builder,
required this.listener,
this.bloc,
this.buildWhen,
this.listenWhen,
}) : super(key: key);
其中参数说明如下:
builder:响应式组件构造方法,和BlocBuilder的相同。listener:状态变化处理监听回调函数,和BlocListener的定义一致。bloc:可选的Bloc状态对象,如果没有指定,会自动从当前的BuildContext中查找对应类型的状态对象。buildWhen:可选参数,接收前后的状态对象,可以根据前后状态对象返回bool值,若为true才会刷新组件。listenWhen:可选参数,接收前后的状态对象,可以根据前后状态对象返回bool值,若为true才会调用listener回调方法。
再来看BlocConsumer的 builder 方法:
@override
Widget build(BuildContext context) {
if (widget.bloc == null) context.select<B, int>(identityHashCode);
return BlocBuilder<B, S>(
bloc: _bloc,
builder: widget.builder,
buildWhen: (previous, current) {
if (widget.listenWhen?.call(previous, current) ?? true) {
widget.listener(context, current);
}
return widget.buildWhen?.call(previous, current) ?? true;
},
);
}
可以看到,实际实现还是基于 BlocBuilder 实现,而在 BlocBuilder 的 builderWhen 中,会根据 listenWhen 的返回值来决定是否调用 listener 回调方法,从而实现了 BlocBuilder和BlocListener 的聚合。
BlocConsumer 应用
我们对上一篇的代码进行改造,然后看一下两部分代码的比对,如下图所示。

可以看到,代码的行数其实没什么改变,但是整个代码的层级会更清晰,这样会使得代码更易于维护,完整代码已经上传至:BLoC 状态管理代码。
总结
本篇介绍了 BlocConsumer 的实现机制,以及比对了 BlocBuilder 和BlocListener分开实现时的差异。通过对比发现可以看到,BlocConsumer 这种聚合了 BlocBuilder 和 BlocListener 的方式在这种场景下的代码层级更清晰,也易于维护。 
边栏推荐
- Restapi version control strategy [eolink translation]
- Win11时间怎么显示星期几?Win11怎么显示今天周几?
- Problems encountered in installing mysql8 for Ubuntu and the detailed installation process
- Lingyun going to sea | saihe & Huawei cloud: jointly help the sustainable development of cross-border e-commerce industry
- Leetcode SQL first day
- Unity3d 4.3.4f1执行项目
- Use br to recover backup data on azure blob storage
- UVA 12230 – crossing rivers (probability) "suggested collection"
- Implementation of mahout Pearson correlation
- 201215-03-19 - cocos2dx memory management - specific explanation "recommended collection"
猜你喜欢
![[open source] Net ORM accessing Firebird database](/img/a2/4eff4f0af53bf3b9839a73019a212f.png)
[open source] Net ORM accessing Firebird database

L2: current situation, prospects and pain points of ZK Rollup
![The maximum number of meetings you can attend [greedy + priority queue]](/img/f3/e8e939e0393efc404cc159d7d33364.png)
The maximum number of meetings you can attend [greedy + priority queue]

Jenkins user rights management

Automatic classification of defective photovoltaic module cells in electronic images

Virtual machine network configuration in VMWare

用语雀写文章了,功能真心强大!

Goal: do not exclude yaml syntax. Try to get started quickly

ISO 26262 - considerations other than requirements based testing

Embedded development: how to choose the right RTOS for the project?
随机推荐
Actual combat: sqlserver 2008 Extended event XML is converted to standard table format [easy to understand]
Is it safe to open an account online now? I want to know where I can open an account in Nanning now?
Mahout-Pearson correlation的实现
Which financial products will yield high returns in 2022?
Open source OA development platform: contract management user manual
Reinforcement learning - learning notes 9 | multi step TD target
Jerry's about TWS pairing mode configuration [chapter]
Programming mode - table driven programming
Deadlock conditions and preventive treatment [easy to understand]
Reinforcement learning - learning notes 8 | Q-learning
How to integrate Google APIs with Google's application system (1) -introduction to Google APIs
Is private equity legal in China? Is it safe?
The difference between NPM uninstall and RM direct deletion
Jenkins user rights management
How does win11 unblock the keyboard? Method of unlocking keyboard in win11
Arlo's troubles
npm uninstall和rm直接删除的区别
Restapi version control strategy [eolink translation]
648. Word replacement
Meta force force meta universe system development fossage model