当前位置:网站首页>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 , andBlocBuilder
In the same .listener
: Status change processing listening callback function , andBlocListener
The definitions are the same .bloc
: OptionalBloc
State object , If not specified , Automatically from the currentBuildContext
Find 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 objectsbool
value , iftrue
Will refresh the component .listenWhen
: Optional parameters , Status objects before and after receiving , You can return... According to the previous and subsequent status objectsbool
value , iftrue
Will calllistener
The 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 .
边栏推荐
- Code of "digital image processing principle and Practice (matlab version)" part2[easy to understand]
- Jerry's test box configuration channel [chapter]
- Description of the difference between character varying and character in PostgreSQL database
- Wechat official account oauth2.0 authorizes login and displays user information
- Addition, deletion, modification and query of sqlhelper
- An in-depth understanding of fp/fn/precision/recall
- 双塔模型的最强出装,谷歌又开始玩起“老古董”了?
- 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?
- Matplotlib drawing interface settings
- Demon daddy B3 read extensively in a small amount, and completed 20000 vocabulary+
猜你喜欢
反爬通杀神器
ISO 26262 - considerations other than requirements based testing
你可曾迷茫?曾经的测试/开发程序员,懵懂的小菜C鸟升级......
L'enregistreur de disque dur NVR est connecté à easycvr par le Protocole GB 28181. Quelle est la raison pour laquelle l'information sur le canal de l'appareil n'est pas affichée?
Embedded development: how to choose the right RTOS for the project?
Jerry's about TWS pairing mode configuration [chapter]
双塔模型的最强出装,谷歌又开始玩起“老古董”了?
Where is the big data open source project, one-stop fully automated full life cycle operation and maintenance steward Chengying (background)?
Preparing for the interview and sharing experience
大数据开源项目,一站式全自动化全生命周期运维管家ChengYing(承影)走向何方?
随机推荐
Index summary (assault version)
Virtual machine network configuration in VMWare
Which financial products will yield high returns in 2022?
使用 CustomPaint 绘制基本图形
Jerry's about TWS pairing mode configuration [chapter]
Pre sale 179000, hengchi 5 can fire? Product power online depends on how it is sold
Demon daddy C
The new version of onespin 360 DV has been released, refreshing the experience of FPGA formal verification function
Tcp/ip protocol stack
The difference between NPM uninstall and RM direct deletion
Implementation method of data platform landing
Tupu digital twin coal mining system to create "hard power" of coal mining
Which futures company is the safest to open a futures account?
Win11如何解禁键盘?Win11解禁键盘的方法
South China x99 platform chicken blood tutorial
Two kinds of updates lost and Solutions
Cv2.resize function reports an error: error: (-215:assertion failed) func= 0 in function ‘cv::hal::resize‘
Default constraint and zero fill constraint of MySQL constraint
Matplotlib drawing interface settings
Tsconfig of typescript TS basics JSON configuration options