当前位置:网站首页>Use blocconsumer to build responsive components and monitor status at the same time
Use blocconsumer to build responsive components and monitor status at the same time
2022-07-07 22:00:00 【Ma Nong @ official account on the island with the same name】
Preface
In the last article Flutter Introduction and actual combat ( eighty-nine ): Use BlocListener Processing state changes , We used BlocListener Monitor the state changes and do some corresponding processing , At the same time BlocBuilder Build a responsive interface . Review the code , We will find that there are redundant parts , We need to BlocBuilder As BlocListener The child components , It's actually a little awkward , because BlocBuilder The built page should not be BlocListener The child components , It's a peer structure . Is there a better way to solve this problem , in fact ,flutter_bloc given BlocConsumer To solve this problem .
BlocConsumer Implementation mechanism
BlocConsumer Like BlocBuilder and BlocListener Aggregate component of , Support building responsive components while listening for state changes . At the same time, it also supports refreshing components according to conditions or listening callback in response to state changes .BlocConsumer The construction method of is as follows :
const BlocConsumer({
Key? key,
required this.builder,
required this.listener,
this.bloc,
this.buildWhen,
this.listenWhen,
}) : super(key: key);
The parameters are as follows :
builder: Responsive component construction method , andBlocBuilderIn the same .listener: Status change processing listening callback function , andBlocListenerThe definitions are the same .bloc: OptionalBlocState object , If not specified , Automatically from the currentBuildContextFind the corresponding type of status object in .buildWhen: Optional parameters , Status objects before and after receiving , You can return... According to the previous and subsequent status objectsboolvalue , iftrueWill refresh the component .listenWhen: Optional parameters , Status objects before and after receiving , You can return... According to the previous and subsequent status objectsboolvalue , iftrueWill calllistenerThe callback method .
Look again. BlocConsumer Of builder Method :
@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;
},
);
}
You can see , The actual implementation is based on BlocBuilder Realization , And in the BlocBuilder Of builderWhen in , Will be based on listenWhen To determine whether to call listener The callback method , So that BlocBuilder and BlocListener The aggregation of .
BlocConsumer application
We modified the code of the previous article , Then take a look at the comparison of the two parts of the code , As shown in the figure below .

You can see , The number of lines of code has not changed much , But the hierarchy of the whole code will be clearer , This makes the code easier to maintain , The complete code has been uploaded to :BLoC Status management code .
summary
This article introduces BlocConsumer Implementation mechanism , And compared BlocBuilder and BlocListener Differences in separate implementations . Through comparison, we can see ,BlocConsumer This kind of aggregation BlocBuilder and BlocListener In this scenario, the code level is clearer , It's also easy to maintain . 
边栏推荐
- EasyUI date control emptying value
- Typescript TS basic knowledge type declaration
- Develop those things: go plus c.free to free memory, and what are the reasons for compilation errors?
- 谈谈制造企业如何制定敏捷的数字化转型策略
- UVA 12230 – crossing rivers (probability) "suggested collection"
- 反爬通杀神器
- SQL injection error report injection function graphic explanation
- Display optimization when the resolution of easycvr configuration center video recording plan page is adjusted
- 为什么Win11不能显示秒数?Win11时间不显示秒怎么解决?
- Description of the difference between character varying and character in PostgreSQL database
猜你喜欢

Ten thousand word summary data storage, three knowledge points

Focusing on safety in 1995, Volvo will focus on safety in the field of intelligent driving and electrification in the future

Win11如何解禁键盘?Win11解禁键盘的方法

Why can't win11 display seconds? How to solve the problem that win11 time does not display seconds?
![Restapi version control strategy [eolink translation]](/img/65/decbc158f467ab8c8923c5947af535.png)
Restapi version control strategy [eolink translation]

EasyCVR配置中心录像计划页面调整分辨率时的显示优化

Open source OA development platform: contract management user manual

大数据开源项目,一站式全自动化全生命周期运维管家ChengYing(承影)走向何方?
![[advanced MySQL] index details (I): index data page structure](/img/e7/fe4591a721a71c3c38d6e4448af6af.png)
[advanced MySQL] index details (I): index data page structure

Using enumeration to realize English to braille
随机推荐
OpenGL super classic learning notes (1) the first triangle "suggestions collection"
UVA 12230 – crossing rivers (probability) "suggested collection"
L2: current situation, prospects and pain points of ZK Rollup
SQL injection error report injection function graphic explanation
Pre sale 179000, hengchi 5 can fire? Product power online depends on how it is sold
Usage of MySQL subquery keywords (exists)
Jerry's initiation of ear pairing, reconnection, and opening of discoverable and connectable cyclic functions [chapter]
Deployment, recall and deletion solutions - stsadm and PowerShell "suggestions collection"
Code of "digital image processing principle and Practice (matlab version)" part2[easy to understand]
[colmap] sparse reconstruction is converted to mvsnet format input
Implementation method of data platform landing
Song list 11111
Contour layout of margin
cv2.resize函数报错:error: (-215:Assertion failed) func != 0 in function ‘cv::hal::resize‘
大数据开源项目,一站式全自动化全生命周期运维管家ChengYing(承影)走向何方?
EasyCVR配置中心录像计划页面调整分辨率时的显示优化
Virtual machine network configuration in VMWare
How to turn on win11 game mode? How to turn on game mode in win11
谈谈制造企业如何制定敏捷的数字化转型策略
Ten thousand word summary data storage, three knowledge points