如何重建提供者选择器但不重建其中的 child
how to rebuild provider Selector but not rebuild child of them
我知道消费者可以通过将 child 小部件作为参数传递给构建器的 child 来实现,但我不知道选择器
这是一些文档 https://github.com/rrousselGit/provider#my-widget-rebuilds-too-often-what-can-i-do
避免不必要重建的一种方法是使用提供程序包中的 Selector class。可能存在这样一种情况,您只需要从 ViewModel 调用方法但不一定需要监听更改。您可以通过将构造函数中的可选 shouldRebuild 参数设为 return false 来执行此操作。所以永远不会重建选择器下的小部件。
代码示例:
Selector<HomeModel, HomeModel>(
shouldRebuild: (prev, next) => false,
selector: (_, model) => model,
builder: (_, data, child) {
return CurvedNavigationBar(
backgroundColor: Theme.of(context).accentColor,
items: <Widget>[
Icon(Icons.list, size: 30),
Icon(Icons.account_circle, size: 30),
],
onTap: (index) {
data.currentIndex = index;
},
);
},
);
更新。选择器也有一个 child。我可以像 Consumer 一样使用它。
我知道消费者可以通过将 child 小部件作为参数传递给构建器的 child 来实现,但我不知道选择器 这是一些文档 https://github.com/rrousselGit/provider#my-widget-rebuilds-too-often-what-can-i-do
避免不必要重建的一种方法是使用提供程序包中的 Selector class。可能存在这样一种情况,您只需要从 ViewModel 调用方法但不一定需要监听更改。您可以通过将构造函数中的可选 shouldRebuild 参数设为 return false 来执行此操作。所以永远不会重建选择器下的小部件。
代码示例:
Selector<HomeModel, HomeModel>(
shouldRebuild: (prev, next) => false,
selector: (_, model) => model,
builder: (_, data, child) {
return CurvedNavigationBar(
backgroundColor: Theme.of(context).accentColor,
items: <Widget>[
Icon(Icons.list, size: 30),
Icon(Icons.account_circle, size: 30),
],
onTap: (index) {
data.currentIndex = index;
},
);
},
);
更新。选择器也有一个 child。我可以像 Consumer 一样使用它。