selectIndicator method

  1. @override
Widget selectIndicator(
  1. BuildContext context,
  2. AssetEntity asset
)
override

Indicator for assets selected status. 资源是否已选的指示器

Implementation

@override
Widget selectIndicator(BuildContext context, AssetEntity asset) {
  return Selector<DefaultAssetPickerProvider, String>(
    selector: (_, DefaultAssetPickerProvider p) => p.selectedDescriptions,
    builder: (BuildContext context, _, __) {
      final List<AssetEntity> selectedAssets =
          context.select<DefaultAssetPickerProvider, List<AssetEntity>>(
        (DefaultAssetPickerProvider p) => p.selectedAssets,
      );
      final bool selected = selectedAssets.contains(asset);
      final double indicatorSize = Screens.width / gridCount / 3;
      final Widget innerSelector = AnimatedContainer(
        duration: switchingPathDuration,
        width: indicatorSize / (isAppleOS ? 1.25 : 1.5),
        height: indicatorSize / (isAppleOS ? 1.25 : 1.5),
        decoration: BoxDecoration(
          border:
              !selected ? Border.all(color: Colors.white, width: 2.0) : null,
          color: selected ? themeColor : null,
          shape: BoxShape.circle,
        ),
        child: AnimatedSwitcher(
          duration: switchingPathDuration,
          reverseDuration: switchingPathDuration,
          child: selected
              ? isSingleAssetMode
                  ? const Icon(Icons.check, size: 18.0)
                  : Text(
                      '${selectedAssets.indexOf(asset) + 1}',
                      style: TextStyle(
                        color: selected
                            ? theme.textTheme.bodyText1?.color
                            : null,
                        fontSize: isAppleOS ? 16.0 : 14.0,
                        fontWeight:
                            isAppleOS ? FontWeight.w600 : FontWeight.bold,
                      ),
                    )
              : const SizedBox.shrink(),
        ),
      );
      final GestureDetector selectorWidget = GestureDetector(
        behavior: HitTestBehavior.opaque,
        onTap: () {
          if (selected) {
            provider.unSelectAsset(asset);
            return;
          }
          if (isSingleAssetMode) {
            provider.selectedAssets.clear();
          }
          provider.selectAsset(asset);
          if (isSingleAssetMode && !isPreviewEnabled) {
            Navigator.of(context).pop(provider.selectedAssets);
          }
        },
        child: Container(
          margin: EdgeInsets.all(
            Screens.width / gridCount / (isAppleOS ? 12.0 : 15.0),
          ),
          width: isPreviewEnabled ? indicatorSize : null,
          height: isPreviewEnabled ? indicatorSize : null,
          alignment: AlignmentDirectional.topEnd,
          child: (!isPreviewEnabled && isSingleAssetMode && !selected)
              ? const SizedBox.shrink()
              : innerSelector,
        ),
      );
      if (isPreviewEnabled) {
        return PositionedDirectional(
          top: 0.0,
          end: 0.0,
          child: selectorWidget,
        );
      }
      return selectorWidget;
    },
  );
}