当前位置:网站首页>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
- Codemail auto collation code of visual studio plug-in
- Why can't win11 display seconds? How to solve the problem that win11 time does not display seconds?
- Two kinds of updates lost and Solutions
- [open source] Net ORM accessing Firebird database
- Wechat official account oauth2.0 authorizes login and displays user information
- Demon daddy A3 stage near normal speed speech flow initial contact
- Goal: do not exclude yaml syntax. Try to get started quickly
- Nine degree 1201 - traversal of binary sort number - binary sort tree "suggestions collection"
- 海外代理推荐
猜你喜欢

The latest Android interview collection, Android video extraction audio

Cv2.resize function reports an error: error: (-215:assertion failed) func= 0 in function ‘cv::hal::resize‘
Preparing for the interview and sharing experience

The function is really powerful!

Index summary (assault version)

Node:504 error reporting

2022 how to evaluate and select low code development platforms?

Implementation method of data platform landing

Reptile combat (VII): pictures of the king of reptiles' heroes

Solve the problem of uni in uni app Request sent a post request without response.
随机推荐
Can I open a stock account directly online now? Is it safe?
operator
Jerry's initiation of ear pairing, reconnection, and opening of discoverable and connectable cyclic functions [chapter]
Latest Android advanced interview questions summary, Android interview questions and answers
Take the intersection of two sets
[open source] Net ORM accessing Firebird database
EasyUI date control emptying value
Default constraint and zero fill constraint of MySQL constraint
Focusing on safety in 1995, Volvo will focus on safety in the field of intelligent driving and electrification in the future
Use br to back up tidb cluster data to azure blob storage
[advanced MySQL] index details (I): index data page structure
The maximum number of meetings you can attend [greedy + priority queue]
使用 CustomPaint 绘制基本图形
cv2.resize函数报错:error: (-215:Assertion failed) func != 0 in function ‘cv::hal::resize‘
嵌入式开发:如何为项目选择合适的RTOS?
South China x99 platform chicken blood tutorial
三元表达式、各生成式、匿名函数
What is the reason for the abnormal flow consumption of 4G devices accessing the easygbs platform?
Open source OA development platform: contract management user manual
Devil daddy B1 hearing the last barrier, break through with all his strength