assetsGridBuilder method

Widget assetsGridBuilder(
  1. BuildContext context,
  2. ScrollController scrollController,
  3. bool scrollLock,
  4. double footerHeight,
)

The main grid view builder for assets

Implementation

Widget assetsGridBuilder(BuildContext context, ScrollController scrollController, bool scrollLock, double footerHeight){

  return Selector<DefaultAssetPickerProvider, AssetPathEntity?>(
    selector: (_, DefaultAssetPickerProvider p) => p.currentPathEntity,
    builder: (_, AssetPathEntity? path, __) {

      // First, we need the count of the assets.
      int totalCount = path?.assetCount ?? 0;

      // Then we use the [totalCount] to calculate how many placeholders we need.
      int placeholderCount = 0;
      if (totalCount % gridCount != 0) {
        // When there are left items that not filled into one row, filled the row
        // with placeholders.
        placeholderCount = gridCount - totalCount % gridCount;
      } else {
        // Otherwise, we don't need placeholders.
        placeholderCount = 0;
      }

      Widget _sliverGrid(BuildContext c, List<AssetEntity> assets){
        return SliverGrid(
          delegate: SliverChildBuilderDelegate((_, int index) => Builder(
            builder: (BuildContext c){
              if (index >= assets.length) {
                return const SizedBox.shrink();
              }
              return assetItemBuilder(context, provider, index, assets);
              },
            ),
            childCount: assetGridItemCount(
              assets,
              placeholderCount: placeholderCount
            ),
            // findChildIndexCallback: (Key key) {
            //   if (key is ValueKey<String>) {
            //     return findChildIndexBuilder(
            //       key.value,
            //       assets,
            //       placeholderCount: placeholderCount,
            //     );
            //   }
            //   return null;
            // },
          ),
          gridDelegate: SliverGridDelegateWithFixedCrossAxisCount(
            crossAxisCount: gridCount,
            mainAxisSpacing: 1,
            crossAxisSpacing: 1
          )
        );
      }

      return Selector<DefaultAssetPickerProvider, List<AssetEntity>>(
        selector: (_, DefaultAssetPickerProvider provider) => provider.currentAssets,
        builder: (_, List<AssetEntity> assets, __) {
          return AnimationLimiter(
            child: CustomScrollView(
              scrollDirection: Axis.vertical,
              physics: scrollLock ? NeverScrollableScrollPhysics() : BouncingScrollPhysics(parent: AlwaysScrollableScrollPhysics()),
              controller: scrollController,
              slivers: [
                _sliverGrid(_, assets),
                SliverToBoxAdapter(
                  child: Container(height: footerHeight + MediaQueryData.fromWindow(window).viewPadding.bottom),
                )
              ],
            ),
          );
        }
      );
    }
  );
}