我知道消费者可以通过将子小部件作为参数传递给构建器的孩子来做到这一点,但我不知道在选择器中这是一些文档https://github.com/rrousselGit/provider#my-widget-rebuilds-too -经常-我能做什么
问问题
547 次
2 回答
1
One way to avoid unnecessary rebuild is to use the Selector class from the provider package. There might be a scenario where you only need to call a method from your ViewModel but not necessarily need to listen to the changes. You can do this with the optional shouldRebuild argument in the constructor to return false. So the the widget under the selector is never rebuilt.
A code example:
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;
},
);
},
);
于 2020-09-21T16:13:29.823 回答
0
更新。选择器也有一个孩子。我可以像消费者一样使用它。
于 2021-06-10T03:16:34.783 回答