assetItemBuilder method

Widget assetItemBuilder(
  1. BuildContext context,
  2. DefaultAssetPickerProvider provider,
  3. int index,
  4. List<AssetEntity> currentAssets,
)

Returns an Image or video widget depending on the duration of the AssetEntity

Load more assets when the index reached at third line counting backwards

Implementation

Widget assetItemBuilder(BuildContext context, DefaultAssetPickerProvider provider, int index, List<AssetEntity> currentAssets){

  // length of the currently loaded assets
  int _length = currentAssets.length;

  // If the asset is a video
  bool isVideo = currentAssets[index].videoDuration.inMilliseconds > 0;


  Widget child = isVideo ?
    videoItemBuilder(context, currentAssets[index]) :
    imageItemBuilder(context, currentAssets[index]);

  // load more assets when a offset of 6 is reached and has more to load
  if (index == _length - gridCount * 3 && provider.hasMoreToLoad) {
    provider.loadMoreAssets();
  }

  return Selector<DefaultAssetPickerProvider, int>(
    selector: (_, DefaultAssetPickerProvider p) => p.maxAssets,
    builder: (BuildContext context, int count, Widget? _) {
      return Selector<DefaultAssetPickerProvider, int>(
        selector: (_, DefaultAssetPickerProvider p) => p.selectedAssets.length,
        builder: (BuildContext context, int selectedCount, Widget? _) {
          bool videoLock = false;
          bool lock = false;
          if(provider.selectedAssets.length >= provider.maxAssets){
            lock = true;
          }
          else if(provider.selectedAssets.where((element) => element.duration > 0).isNotEmpty){
            videoLock = true;
          }
          return AnimationConfiguration.staggeredGrid(
            columnCount: (index / gridCount).floor(),
            position: index,
            duration: const Duration(milliseconds: 375),
            child: ScaleAnimation(
              child: FadeInAnimation(
                child: GestureDetector(
                  child: child,
                  onTap: !provider.selectedAssets.contains(currentAssets[index]) && (lock || (videoLock && currentAssets[index].duration > 0)) ? null : (){
                    if(provider.selectedAssets.contains(currentAssets[index])){
                      provider.unSelectAsset(currentAssets[index]);
                    }
                    else{
                      provider.selectAsset(currentAssets[index]);
                    }
                    imagePickerController!.update();
                  }
                )
              ),
            )
          );
        }
      );
    }
  );
}