pickAssets static method

Future<List<AssetEntity>?> pickAssets(
  1. BuildContext context, {
  2. Key? key,
  3. bool useRootNavigator = true,
  4. AssetPickerPageRouteBuilder<List<AssetEntity>>? pageRouteBuilder,
  5. dynamic onPermissionDenied(
    1. BuildContext context,
    2. String delegateDescription
    )?,
  6. InstaAssetCropDelegate cropDelegate = const InstaAssetCropDelegate(),
  7. int gridCount = _kGridCount,
  8. ThemeData? pickerTheme,
  9. AssetPickerTextDelegate? textDelegate,
  10. String? title,
  11. bool closeOnComplete = false,
  12. required dynamic onCompleted(
    1. Stream<InstaAssetsExportDetails> exportDetails
    ),
  13. Widget loadingIndicatorBuilder(
    1. BuildContext,
    2. bool
    )?,
  14. List<AssetEntity>? selectedAssets,
  15. int maxAssets = defaultMaxAssetsCount,
  16. int pageSize = defaultAssetsPerPage,
  17. ThumbnailSize pathThumbnailSize = defaultPathThumbnailSize,
  18. SortPathDelegate<AssetPathEntity>? sortPathDelegate = SortPathDelegate.common,
  19. bool sortPathsByModifiedDate = false,
  20. FilterOptionGroup? filterOptions,
  21. Duration initializeDelayDuration = _kInitializeDelayDuration,
})

Pick assets with the given arguments.

Set useRootNavigator to determine whether the picker route should use the root Navigator.

By extending the AssetPickerPageRoute, users can customize the route and use it with the pageRouteBuilder.

Set onPermissionDenied to manually handle the denied permission error. The default behavior is to open a ScaffoldMessenger.

Crop options

  • Set cropDelegate to customize the display and export of crops.

Those arguments are used by InstaAssetPickerBuilder

  • Set gridCount to specifies the number of assets in the cross axis. Defaults to _kGridCount, like instagram.

  • Set pickerTheme to specifies the theme to apply to the picker. It is by default initialized with the primaryColor of the context theme.

  • Set textDelegate to specifies the language to apply to the picker. Default is the locale language from the context.

  • Set title to specifies the text title in the picker AppBar.

  • Set closeOnComplete to specifies if the picker should be closed after assets selection confirmation.

  • The onCompleted callback is called when the assets selection is confirmed. It will as argument a Stream with exportation details InstaAssetsExportDetails.

  • Set loadingIndicatorBuilder to specifies the loader indicator to display in the picker.

Those arguments are used by DefaultAssetPickerProvider

  • Set selectedAssets to specifies which assets to preselect when the picker is opened.

  • Set maxAssets to specifies the maximum of assets that can be selected Defaults to defaultMaxAssetsCount.

  • Set pageSize to specifies the quantity of assets to display in a single page. Defaults to defaultAssetsPerPage.

  • Set pathThumbnailSize to specifies the album thumbnail size in the albums list Defaults to defaultPathThumbnailSize.

  • Set sortPathDelegate to specifies the order of the assets Defaults to SortPathDelegate.common.

  • Set sortPathsByModifiedDate to specifies whether the modified_date can be used in the sort delegate. Defaults to false.

  • Set filterOptions to specifies the rules to include/exclude assets from the list

  • Set initializeDelayDuration to specifies the delay before loading the assets Defaults to _kInitializeDelayDuration.

Implementation

static Future<List<AssetEntity>?> pickAssets(
  BuildContext context, {
  Key? key,
  bool useRootNavigator = true,
  AssetPickerPageRouteBuilder<List<AssetEntity>>? pageRouteBuilder,
  Function(BuildContext context, String delegateDescription)?
      onPermissionDenied,

  /// Crop options
  InstaAssetCropDelegate cropDelegate = const InstaAssetCropDelegate(),

  /// InstaAssetPickerBuilder options
  int gridCount = _kGridCount,
  ThemeData? pickerTheme,
  AssetPickerTextDelegate? textDelegate,
  String? title,
  bool closeOnComplete = false,
  required Function(Stream<InstaAssetsExportDetails> exportDetails)
      onCompleted,
  Widget Function(BuildContext, bool)? loadingIndicatorBuilder,

  /// DefaultAssetPickerProvider options
  List<AssetEntity>? selectedAssets,
  int maxAssets = defaultMaxAssetsCount,
  int pageSize = defaultAssetsPerPage,
  ThumbnailSize pathThumbnailSize = defaultPathThumbnailSize,
  SortPathDelegate<AssetPathEntity>? sortPathDelegate =
      SortPathDelegate.common,
  bool sortPathsByModifiedDate = false,
  FilterOptionGroup? filterOptions,
  Duration initializeDelayDuration = _kInitializeDelayDuration,
}) async {
  final locale = Localizations.maybeLocaleOf(context);
  final text = textDelegate ?? assetPickerTextDelegateFromLocale(locale);

  // must be called before initializing any picker provider to avoid `PlatformException(PERMISSION_REQUESTING)` type exception
  PermissionState? ps;
  try {
    ps = await _permissionCheck();
  } catch (e) {
    _openErrorPermission(context, text, onPermissionDenied);
    return [];
  }

  final DefaultAssetPickerProvider provider = DefaultAssetPickerProvider(
    selectedAssets: selectedAssets,
    maxAssets: maxAssets,
    pageSize: pageSize,
    pathThumbnailSize: pathThumbnailSize,
    requestType: RequestType.image,
    sortPathDelegate: sortPathDelegate,
    sortPathsByModifiedDate: sortPathsByModifiedDate,
    filterOptions: filterOptions,
    initializeDelayDuration: initializeDelayDuration,
  );

  final InstaAssetPickerBuilder builder = InstaAssetPickerBuilder(
    initialPermission: ps,
    provider: provider,
    title: title,
    gridCount: gridCount,
    pickerTheme: pickerTheme ?? themeData(Theme.of(context).primaryColor),
    locale: locale,
    keepScrollOffset: false,
    textDelegate: text,
    loadingIndicatorBuilder: loadingIndicatorBuilder,
    closeOnComplete: closeOnComplete,
    cropDelegate: cropDelegate,
    onCompleted: onCompleted,
  );

  return AssetPicker.pickAssetsWithDelegate(
    context,
    delegate: builder,
    useRootNavigator: useRootNavigator,
    pageRouteBuilder: pageRouteBuilder,
  );
}