pickAssets static method

Future<List<AssetEntity>?> pickAssets(
  1. BuildContext context, {
  2. List<AssetEntity>? selectedAssets,
  3. int maxAssets = 9,
  4. int pageSize = 80,
  5. int gridThumbSize = Constants.defaultGridThumbSize,
  6. int pathThumbSize = 80,
  7. int gridCount = 4,
  8. RequestType requestType = RequestType.image,
  9. List<int>? previewThumbSize,
  10. SpecialPickerType? specialPickerType,
  11. Color? themeColor,
  12. ThemeData? pickerTheme,
  13. SortPathDelegate? sortPathDelegate,
  14. AssetsPickerTextDelegate? textDelegate,
  15. FilterOptionGroup? filterOptions,
  16. WidgetBuilder? specialItemBuilder,
  17. IndicatorBuilder? loadingIndicatorBuilder,
  18. SpecialItemPosition specialItemPosition = SpecialItemPosition.none,
  19. bool allowSpecialItemWhenEmpty = false,
  20. bool useRootNavigator = true,
  21. Curve routeCurve = Curves.easeIn,
  22. Duration routeDuration = const Duration(milliseconds: 300),
})

Static method to push with the navigator. 跳转至选择器的静态方法

Implementation

static Future<List<AssetEntity>?> pickAssets(
  BuildContext context, {
  List<AssetEntity>? selectedAssets,
  int maxAssets = 9,
  int pageSize = 80,
  int gridThumbSize = Constants.defaultGridThumbSize,
  int pathThumbSize = 80,
  int gridCount = 4,
  RequestType requestType = RequestType.image,
  List<int>? previewThumbSize,
  SpecialPickerType? specialPickerType,
  Color? themeColor,
  ThemeData? pickerTheme,
  SortPathDelegate? sortPathDelegate,
  AssetsPickerTextDelegate? textDelegate,
  FilterOptionGroup? filterOptions,
  WidgetBuilder? specialItemBuilder,
  IndicatorBuilder? loadingIndicatorBuilder,
  SpecialItemPosition specialItemPosition = SpecialItemPosition.none,
  bool allowSpecialItemWhenEmpty = false,
  bool useRootNavigator = true,
  Curve routeCurve = Curves.easeIn,
  Duration routeDuration = const Duration(milliseconds: 300),
}) async {
  if (maxAssets < 1) {
    throw ArgumentError(
      'maxAssets must be greater than 1.',
    );
  }
  if (pageSize % gridCount != 0) {
    throw ArgumentError(
      'pageSize must be a multiple of gridCount.',
    );
  }
  if (pickerTheme != null && themeColor != null) {
    throw ArgumentError(
      'Theme and theme color cannot be set at the same time.',
    );
  }
  if (specialPickerType == SpecialPickerType.wechatMoment) {
    if (requestType != RequestType.image) {
      throw ArgumentError(
        'SpecialPickerType.wechatMoment and requestType cannot be set at the same time.',
      );
    }
    requestType = RequestType.common;
  }
  if ((specialItemBuilder == null &&
          specialItemPosition != SpecialItemPosition.none) ||
      (specialItemBuilder != null &&
          specialItemPosition == SpecialItemPosition.none)) {
    throw ArgumentError('Custom item did not set properly.');
  }

  try {
    final PermissionState _ps = await PhotoManager.requestPermissionExtend();
    if (_ps == PermissionState.authorized || _ps == PermissionState.limited) {
      final DefaultAssetPickerProvider provider = DefaultAssetPickerProvider(
        maxAssets: maxAssets,
        pageSize: pageSize,
        pathThumbSize: pathThumbSize,
        selectedAssets: selectedAssets,
        requestType: requestType,
        sortPathDelegate: sortPathDelegate,
        filterOptions: filterOptions,
        routeDuration: routeDuration,
      );
      final Widget picker =
          ChangeNotifierProvider<DefaultAssetPickerProvider>.value(
        value: provider,
        child: AssetPicker<AssetEntity, AssetPathEntity>(
          key: Constants.pickerKey,
          builder: DefaultAssetPickerBuilderDelegate(
            provider: provider,
            gridCount: gridCount,
            textDelegate: textDelegate,
            themeColor: themeColor,
            pickerTheme: pickerTheme,
            gridThumbSize: gridThumbSize,
            previewThumbSize: previewThumbSize,
            specialPickerType: specialPickerType,
            specialItemPosition: specialItemPosition,
            specialItemBuilder: specialItemBuilder,
            loadingIndicatorBuilder: loadingIndicatorBuilder,
            allowSpecialItemWhenEmpty: allowSpecialItemWhenEmpty,
          ),
        ),
      );
      final List<AssetEntity>? result = await Navigator.of(
        context,
        rootNavigator: useRootNavigator,
      ).push<List<AssetEntity>>(
        AssetPickerPageRoute<List<AssetEntity>>(
          builder: picker,
          transitionCurve: routeCurve,
          transitionDuration: routeDuration,
        ),
      );
      return result;
    }
    return null;
  } catch (e) {
    realDebugPrint('Error when calling assets picker: $e');
    return null;
  }
}