【问题标题】:how to rebuild provider Selector but not rebuild child of them如何重建提供者选择器但不重建它们的孩子
【发布时间】:2020-02-24 20:31:51
【问题描述】:

我知道消费者可以通过将子小部件作为参数传递给构建器的子小部件来做到这一点,但我不知道在选择器中 这是一些文档https://github.com/rrousselGit/provider#my-widget-rebuilds-too-often-what-can-i-do

【问题讨论】:

    标签: flutter flutter-provider


    【解决方案1】:

    避免不必要的重建的一种方法是使用提供程序包中的Selector class。可能存在这样一种情况,您只需要从 ViewModel 调用一个方法,但不一定需要监听更改。您可以使用构造函数中的可选 shouldRebuild 参数来返回 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;
              },
            );
          },
        );
    

    【讨论】:

      【解决方案2】:

      更新。选择器也有一个孩子。我可以像 Consumer 一样使用它。

      【讨论】:

        猜你喜欢
        • 2021-01-15
        • 2020-10-18
        • 2017-03-17
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2022-01-15
        • 1970-01-01
        相关资源
        最近更新 更多