mobileAttachmentPickerBuilder function

Widget mobileAttachmentPickerBuilder({
  1. required BuildContext context,
  2. required StreamAttachmentPickerController controller,
  3. Iterable<AttachmentPickerOption>? customOptions,
  4. List<AttachmentPickerType> allowedTypes = AttachmentPickerType.values,
  5. ThumbnailSize attachmentThumbnailSize = const ThumbnailSize(400, 400),
  6. ThumbnailFormat attachmentThumbnailFormat = ThumbnailFormat.jpeg,
  7. int attachmentThumbnailQuality = 100,
  8. double attachmentThumbnailScale = 1,
  9. ErrorListener? onError,
})

Returns the mobile version of the attachment picker.

Implementation

Widget mobileAttachmentPickerBuilder({
  required BuildContext context,
  required StreamAttachmentPickerController controller,
  Iterable<AttachmentPickerOption>? customOptions,
  List<AttachmentPickerType> allowedTypes = AttachmentPickerType.values,
  ThumbnailSize attachmentThumbnailSize = const ThumbnailSize(400, 400),
  ThumbnailFormat attachmentThumbnailFormat = ThumbnailFormat.jpeg,
  int attachmentThumbnailQuality = 100,
  double attachmentThumbnailScale = 1,
  ErrorListener? onError,
}) {
  return StreamMobileAttachmentPickerBottomSheet(
    controller: controller,
    onSendAttachments: Navigator.of(context).pop,
    options: {
      ...{
        if (customOptions != null) ...customOptions,
        AttachmentPickerOption(
          key: 'gallery-picker',
          icon: StreamSvgIcon.pictures(size: 36).toIconThemeSvgIcon(),
          supportedTypes: [
            AttachmentPickerType.images,
            AttachmentPickerType.videos,
          ],
          optionViewBuilder: (context, controller) {
            final selectedIds = controller.value.map((it) => it.id);
            return StreamGalleryPicker(
              selectedMediaItems: selectedIds,
              mediaThumbnailSize: attachmentThumbnailSize,
              mediaThumbnailFormat: attachmentThumbnailFormat,
              mediaThumbnailQuality: attachmentThumbnailQuality,
              mediaThumbnailScale: attachmentThumbnailScale,
              onMediaItemSelected: (media) async {
                try {
                  if (selectedIds.contains(media.id)) {
                    return await controller.removeAssetAttachment(media);
                  }
                  return await controller.addAssetAttachment(media);
                } catch (e, stk) {
                  if (onError != null) return onError.call(e, stk);
                  rethrow;
                }
              },
            );
          },
        ),
        AttachmentPickerOption(
          key: 'file-picker',
          icon: StreamSvgIcon.files(size: 36).toIconThemeSvgIcon(),
          supportedTypes: [AttachmentPickerType.files],
          optionViewBuilder: (context, controller) {
            return StreamFilePicker(
              onFilePicked: (file) async {
                try {
                  if (file != null) await controller.addAttachment(file);
                  return Navigator.pop(context, controller.value);
                } catch (e, stk) {
                  Navigator.pop(context, controller.value);
                  if (onError != null) return onError.call(e, stk);

                  rethrow;
                }
              },
            );
          },
        ),
        AttachmentPickerOption(
          key: 'image-picker',
          icon: StreamSvgIcon.camera(size: 36).toIconThemeSvgIcon(),
          supportedTypes: [AttachmentPickerType.images],
          optionViewBuilder: (context, controller) {
            return StreamImagePicker(
              onImagePicked: (image) async {
                try {
                  if (image != null) {
                    await controller.addAttachment(image);
                  }
                  return Navigator.pop(context, controller.value);
                } catch (e, stk) {
                  Navigator.pop(context, controller.value);
                  if (onError != null) return onError.call(e, stk);

                  rethrow;
                }
              },
            );
          },
        ),
        AttachmentPickerOption(
          key: 'video-picker',
          icon: StreamSvgIcon.record(size: 36).toIconThemeSvgIcon(),
          supportedTypes: [AttachmentPickerType.videos],
          optionViewBuilder: (context, controller) {
            return StreamVideoPicker(
              onVideoPicked: (video) async {
                try {
                  if (video != null) {
                    await controller.addAttachment(video);
                  }
                  return Navigator.pop(context, controller.value);
                } catch (e, stk) {
                  Navigator.pop(context, controller.value);
                  if (onError != null) return onError.call(e, stk);

                  rethrow;
                }
              },
            );
          },
        ),
      }.where((option) => option.supportedTypes.every(allowedTypes.contains)),
    },
  );
}