栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

flutter_bloc的BlocBuilder是否可以避免重建未更改的Widget部分?

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

flutter_bloc的BlocBuilder是否可以避免重建未更改的Widget部分?

BlocBuilder
有optinal参数
condition
具有类型
bool Function(State previous, Statecurrent)
,你需要返回
true
,如果你想了Widget调用
builder
函数,
false
如果你不想要的。此参数是可选的,默认情况下为
true

因为在

condition
参数中您具有
previous
状态和状态,所以
current
您可以比较此状态的属性,并
true
在满足比较条件时返回。

请记住,您需要覆盖

==
运算符和
hashCode
状态以及在状态类中使用的所有类。简单的方法是使用equatable。

在您的情况下,您需要

State
像这样:

class MyState extends Equatable {  final bool isPullingState;  final List<MyClass> dataList;  MyState(this.isPullingState, this.dataList)      : super([isPullingState, dataList]);}class MyClass extends Equatable {  final int property1;  final int property2;  MyClass(this.property1, this.property2)      : super([          property1,          property2,        ]);}

然后,您可以在小部件中设置所需的条件:

@override  Widget build(BuildContext context) {    return Column(      children: <Widget>[        BlocBuilder(          bloc: myBloc,          condition: (MyState previous, MyState current) =>   previous.isPullingState != current.isPullingState,          builder: (BuildContext context, MyState state) { // this function is only called when isPullingState change return MyIsPullingWidget();          },        ),        BlocBuilder(          bloc: myBloc,          condition: (MyState previous, MyState current) =>   previous.dataList != current.dataList,          builder: (BuildContext context, MyState state) { // this function is only called when the dataList change return MyListWidget(state.dataList);          },        ),        BlocBuilder(          bloc: myBloc,          builder: (BuildContext context, MyState state) { // this function is called in each state change return MyListWidget(state.dataList);          },        ),      ],    );  }


转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/380006.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号