当前位置:网站首页>使用 BlocConsumer 同时构建响应式组件和监听状态
使用 BlocConsumer 同时构建响应式组件和监听状态
2022-07-04 21:33:00 【InfoQ】
前言
BlocListenerBlocBuilderBlocBuilderBlocListenerBlocBuilderBlocListenerBlocConsumerBlocConsumer实现机制
BlocConsumerBlocBuilderBlocListenerconst 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回调方法。
BlocConsumerbuilder@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;
},
);
}
BlocBuilderBlocBuilderbuilderWhenlistenWhenlistenerBlocBuilderBlocListenerBlocConsumer 应用

总结
BlocConsumerBlocBuilderBlocListenerBlocConsumerBlocBuilderBlocListener
边栏推荐
- 文件读取写入
- 旋变串判断
- [C language] deep understanding of symbols
- At the right time, the Guangzhou station of the city chain science and Technology Strategy Summit was successfully held
- Caduceus从未停止创新,去中心化边缘渲染技术让元宇宙不再遥远
- Drop down selection of Ehlib database records
- Basic structure of PostgreSQL - table
- 时空预测3-graph transformer
- CloudCompare&Open3D DBSCAN聚类(非插件式)
- Redis 排查大 key 的3种方法,优化必备
猜你喜欢
随机推荐
Basic structure of PostgreSQL - table
Enlightenment of maker thinking in Higher Education
更强的 JsonPath 兼容性及性能测试之2022版(Snack3,Fastjson2,jayway.jsonpath)
Flutter 返回按钮的监听
gtest从一无所知到熟练使用(4)如何用gtest写单元测试
Sorting and sharing of selected papers, systems and applications related to the most comprehensive mixed expert (MOE) model in history
Arcgis 10.2.2 | arcgis license server无法启动的解决办法
QT - plot other problems
淘宝商品评价api接口(item_review-获得淘宝商品评论API接口),天猫商品评论API接口
Solve the problem of data disorder caused by slow asynchronous interface
解析互联网时代的创客教育技术
【C语言】符号的深度理解
GTEST from ignorance to proficiency (4) how to write unit tests with GTEST
Operation of adding material schedule in SolidWorks drawing
Which securities company has the lowest Commission for opening an account online? I want to open an account. Is it safe to open an account online
Redis 排查大 key 的3种方法,优化必备
时空预测3-graph transformer
开源之夏专访|Apache IoTDB社区 新晋Committer谢其骏
超详细教程,一文入门Istio架构原理及实战应用
How was MP3 born?









