pickFromType method

Future<void> pickFromType(
  1. BuildContext context
)

弹窗选择类型

Implementation

Future<void> pickFromType(BuildContext context) async {
  if (_assetsPicker.maxCount > 1 &&
      allEntity.length >= _assetsPicker.maxCount) {
    FlAssetsPicker.errorCallback?.call(ErrorDes.maxCount);
    return;
  }
  final type = await FlAssetsPicker.showPickerFromType(
      context, _assetsPicker.fromRequestTypes);
  switch (type?.fromType) {
    case PickerFromType.gallery:
      if (!context.mounted) return;
      List<AssetEntity> selectedAssets = [];
      int maxAssets = 1;
      if (_assetsPicker.maxCount > 1) {
        selectedAssets =
            List.from(allEntity.where((element) => element.isLocalData));
        maxAssets = _assetsPicker.maxCount - selectedAssets.length;
      }
      final assetsEntryList = await pickAssets(context,
          pickerConfig: _assetConfig.copyWith(
              maxAssets: maxAssets,
              requestType: type?.requestType,
              selectedAssets: selectedAssets),
          useRootNavigator: _assetsPicker.useRootNavigator,
          pageRouteBuilder: _assetsPicker.pageRouteBuilderForAssetPicker);
      if (assetsEntryList == null) return;
      if (_assetsPicker.maxCount > 1) {
        var videos = allEntity
            .where((element) => element.type == AssetType.video)
            .toList();
        for (var entity in assetsEntryList) {
          if (entity.type == AssetType.video) videos.add(entity);
          if (videos.length > _assetsPicker.maxVideoCount) {
            FlAssetsPicker.errorCallback?.call(ErrorDes.maxVideoCount);
            continue;
          } else {
            allEntity.add(entity);
          }
        }
      } else {
        /// 单资源远着
        allEntity = assetsEntryList;
      }
      notifyListeners();
      break;
    case PickerFromType.camera:
      if (!context.mounted) return;
      final assetsEntry = await pickFromCamera(context,
          pickerConfig: _cameraConfig.copyWith(
              enableRecording: type?.requestType.containsVideo(),
              onlyEnableRecording: type?.requestType == RequestType.video,
              enableAudio: (type?.requestType.containsVideo() ?? false) ||
                  (type?.requestType.containsAudio() ?? false)),
          useRootNavigator: _assetsPicker.useRootNavigator,
          pageRouteBuilder: _assetsPicker.pageRouteBuilderForCameraPicker);
      if (assetsEntry != null) {
        if (_assetsPicker.maxCount > 1) {
          final videos =
              allEntity.where((element) => element.type == AssetType.video);
          if (videos.length >= _assetsPicker.maxVideoCount) {
            FlAssetsPicker.errorCallback?.call(ErrorDes.maxVideoCount);
            return;
          }
          allEntity.add(assetsEntry);
        } else {
          allEntity = [assetsEntry];
        }
        notifyListeners();
      }
      break;
    case PickerFromType.cancel:
      break;
    default:
      break;
  }
}