微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

Flutter Provider Selector始终会触发

如何解决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 举报,一经查实,本站将立刻删除。