Flutter Provider Selector始终会触发

问题描述

我使用提供程序包和选择器。

据我了解,它可以更精确地控制重建。

我使用两个嵌套的选择器来构建GridView并在磁贴内部。

Expanded(
  child: MediaQuery.removePadding(
    context: context,removetop: true,child: Selector<ImageShareProvider,UnmodifiableListView<File>>(
      selector: (BuildContext context,ImageShareProvider provider) => provider.imageFiles,builder: (BuildContext context,imageFiles,_) {
        return GridView.builder(
            shrinkWrap: true,gridDelegate: SliverGridDelegateWithFixedCrossAxisCount(
              crossAxisspacing: 15,mainAxisspacing: 15,crossAxisCount: 3
            ),itemCount: imageFiles.length,itemBuilder: (BuildContext context,int index) {
              File imageFile = imageFiles[index];
              var path = imageFiles[index].path;
              var fileName = p.split(path).last;
              return Selector<ImageShareProvider,bool>(
                selector: (BuildContext context,ImageShareProvider provider) => provider.isSelected(imageFile),bool selected,_) {
                  return SelectableImageThumb(
                    key: ValueKey(path),width: 150,height: 150,imageFile: imageFile,imageSelectionIconSize: 24,selected: selected,onSelect: () {
                      provider.select(imageFile);
                    },);
                },);
            }
        );
      },),)

让我感到困惑的是,如果外部Selector一直在触发,则内部Selector总是会触发。

ImageShareProvider很简单,就像这样:

class ImageShareProvider extends ChangeNotifier {
  final String folderPath;

  List<File> _imageFiles = List();
  
  Set<File> _selectedFiles = Set();

  UnmodifiableListView<File> get imageFiles => UnmodifiableListView(_imageFiles);

  UnmodifiableListView<File> get selectedFiles => UnmodifiableListView(_selectedFiles.toList());

  int get numSelected => _selectedFiles.length;

  bool isSelected(File f) {
    return _selectedFiles.contains(f);
  }
  
  void select(File f) {
    if (_selectedFiles.contains(f))
      _selectedFiles.remove(f);
    else
      _selectedFiles.add(f);
    notifyListeners();
  }

  Future<void> getimageFiles() async {
    var path = await getApplicationDocumentsDirectory();
    var entries = Directory(folderPath).listSync(recursive: false,followLinks: false);
    _imageFiles = entries.where((entry) => entry is File).map((entry) => entry as File).toList();
    _imageFiles.sort((a,b) => b.lastModifiedSync().compareto(a.lastModifiedSync()));
    notifyListeners();
  }

 .....

什么可以是每当选择了单个SelectableImageThumb所有其他也重建的问题?

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)