gpt4 book ai didi

flutter - 使用 Dismissible 和 Provider (NotifyListener) 的笨重动画

转载 作者:行者123 更新时间:2023-12-05 05:34:16 26 4
gpt4 key购买 nike

我正在尝试解决在使用 Dismissible 小部件和 Provider 包 时发生的问题。

当我解散一张卡片时,这叫做:

 Provider.of<NMyProvider>(context, listen: false).toggleIsDone(object.id);

提供者:

Future<void> deleteNotebook(int id) async {
...
final notebookId = _items.indexWhere((notebook) => notebook.id == id);
...
_items.removeAt(notebookId);
notifyListeners();
}

这会使动画变得笨拙并具有明显的延迟,但在移除 NotifyListeners 时不会发生。

最佳答案

很可能整个屏幕都是根据提供商的通知重建的。有一些解决方案:

  1. 移动Consumer<NMyProvider>/context.watch<NMyProvider>(context)/Provider.of<NMyProvider>(context, listen: true)更接近它的用法。 Flutter 只重建使用相同的 widgets context ;
  2. 使用唯一的 key对于每个笔记本。不是索引位置,而是像 notebookId 这样的唯一键. Flutter 将重新使用已构建的笔记本小部件,其中的键在构建之间是相同的;
  3. 拆分NMyProvider :将对该提供者没有意义的所有内容移至其他提供者。如果许多不相关的状态在一起,则该提供程序被根小部件使用的机会会更高。 NMyProvider应该只处理笔记本状态列表;

关于flutter - 使用 Dismissible 和 Provider (NotifyListener) 的笨重动画,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/73657479/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com