当前位置:网站首页>使用 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
的方式在这种场景下的代码层级更清晰,也易于维护。
边栏推荐
- Demon daddy A3 stage near normal speed speech flow initial contact
- Codeforces Round #275 (Div. 2) C – Diverse Permutation (构造)[通俗易懂]
- How does win11 time display the day of the week? How does win11 display the day of the week today?
- Implementation of mahout Pearson correlation
- 强化学习-学习笔记9 | Multi-Step-TD-Target
- The new version of onespin 360 DV has been released, refreshing the experience of FPGA formal verification function
- Redis - basic use (key, string, list, set, Zset, hash, geo, bitmap, hyperloglog, transaction)
- Index summary (assault version)
- [colmap] sparse reconstruction is converted to mvsnet format input
- Restapi version control strategy [eolink translation]
猜你喜欢
Where is the big data open source project, one-stop fully automated full life cycle operation and maintenance steward Chengying (background)?
Jenkins user rights management
An in-depth understanding of fp/fn/precision/recall
Tcp/ip protocol stack
Win11时间怎么显示星期几?Win11怎么显示今天周几?
Ternary expressions, generative expressions, anonymous functions
SQL injection error report injection function graphic explanation
Matplotlib drawing interface settings
Jerry's about TWS channel configuration [chapter]
NVR硬盘录像机通过国标GB28181协议接入EasyCVR,设备通道信息不显示是什么原因?
随机推荐
Is private equity legal in China? Is it safe?
ISO 26262 - considerations other than requirements based testing
Reinforcement learning - learning notes 8 | Q-learning
EasyCVR配置中心录像计划页面调整分辨率时的显示优化
Le capital - investissement est - il légal en Chine? C'est sûr?
Jerry's about TWS channel configuration [chapter]
Static test tool
Tsconfig of typescript TS basics JSON configuration options
【colmap】稀疏重建转为MVSNet格式输入
Programming mode - table driven programming
What stocks can a new account holder buy? Is the stock trading account safe
UVA 11080 – place the guards
【JDBC Part 1】概述、获取连接、CRUD
Description of the difference between character varying and character in PostgreSQL database
Ad domain group policy management
Navicat connect 2002 - can't connect to local MySQL server through socket '/var/lib/mysql/mysql Sock 'solve
Demon daddy A3 stage near normal speed speech flow initial contact
你可曾迷茫?曾经的测试/开发程序员,懵懂的小菜C鸟升级......
Wechat official account oauth2.0 authorizes login and displays user information
L2: current situation, prospects and pain points of ZK Rollup