当前位置:网站首页>使用 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
的方式在这种场景下的代码层级更清晰,也易于维护。
边栏推荐
- [200 opencv routines] 223 Polygon fitting for feature extraction (cv.approxpolydp)
- Develop those things: go plus c.free to free memory, and what are the reasons for compilation errors?
- Using enumeration to realize English to braille
- Dry goods sharing | devaxpress v22.1 original help document download collection
- Goal: do not exclude yaml syntax. Try to get started quickly
- 三元表达式、各生成式、匿名函数
- How polardb-x does distributed database hotspot analysis
- Restore backup data on persistent volumes
- Demon daddy guide post - simple version
- Demon daddy C
猜你喜欢
NVR硬盘录像机通过国标GB28181协议接入EasyCVR,设备通道信息不显示是什么原因?
Have you ever been confused? Once a test / development programmer, ignorant gadget C bird upgrade
The little money made by the program ape is a P!
Ternary expressions, generative expressions, anonymous functions
Kirin Xin'an operating system derivative solution | storage multipath management system, effectively improving the reliability of data transmission
你可曾迷茫?曾经的测试/开发程序员,懵懂的小菜C鸟升级......
Reinforcement learning - learning notes 9 | multi step TD target
Talk about relational database and serverless
[JDBC Part 1] overview, get connection, CRUD
Google SEO external chain backlinks research tool recommendation
随机推荐
【colmap】稀疏重建转为MVSNet格式输入
Jerry's about TWS pairing mode configuration [chapter]
Implementation of mahout Pearson correlation
201215-03-19 - cocos2dx memory management - specific explanation "recommended collection"
[uvalive 6663 count the regions] (DFS + discretization) [easy to understand]
Have you ever been confused? Once a test / development programmer, ignorant gadget C bird upgrade
The function is really powerful!
What stocks can a new account holder buy? Is the stock trading account safe
UVA 12230 – crossing rivers (probability) "suggested collection"
Demon daddy B3 read extensively in a small amount, and completed 20000 vocabulary+
Demon daddy B2 breaks through grammar and completes orthodox oral practice
Default constraint and zero fill constraint of MySQL constraint
Typescript TS basic knowledge type declaration
[开源] .Net ORM 访问 Firebird 数据库
谈谈制造企业如何制定敏捷的数字化转型策略
大数据开源项目,一站式全自动化全生命周期运维管家ChengYing(承影)走向何方?
Programming mode - table driven programming
Codeforces Round #275 (Div. 2) C – Diverse Permutation (构造)[通俗易懂]
浅解ARC中的 __bridge、__bridge_retained和__bridge_transfer
NVR hard disk video recorder is connected to easycvr through the national standard gb28181 protocol. What is the reason why the device channel information is not displayed?