Merge pull request #1608 from GetStream/feat/allowed-attachment-picker-types

This commit is contained in:
Sahil Kumar
2023-06-13 19:00:35 +05:30
committed by GitHub
4 changed files with 120 additions and 88 deletions
+15
View File
@@ -7,6 +7,21 @@
- [[#1605]](https://github.com/GetStream/stream-chat-flutter/issues/1605) Fixed Null exception is thrown on message list
for unread messages when `ScrollToBottomButton` is pressed.
✅ Added
- Added support for `StreamMessageInput.allowedAttachmentPickerTypes` to specify the allowed attachment picker types.
[#1601](https://github.com/GetStream/stream-chat-flutter/issues/1376)
```dart
StreamMessageInput(
...,
allowedAttachmentPickerTypes: const [
AttachmentPickerType.files,
AttachmentPickerType.images,
],
)
```
## 6.3.0
🐞 Fixed
@@ -693,6 +693,7 @@ 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,
@@ -702,74 +703,76 @@ Widget mobileAttachmentPickerBuilder({
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 {
if (selectedIds.contains(media.id)) {
return controller.removeAssetAttachment(media);
}
return controller.addAssetAttachment(media);
},
);
},
),
AttachmentPickerOption(
key: 'file-picker',
icon: StreamSvgIcon.files(size: 36).toIconThemeSvgIcon(),
supportedTypes: [AttachmentPickerType.files],
optionViewBuilder: (context, controller) {
return StreamFilePicker(
onFilePicked: (file) async {
if (file != null) await controller.addAttachment(file);
return Navigator.pop(context, controller.value);
},
);
},
),
AttachmentPickerOption(
key: 'image-picker',
icon: StreamSvgIcon.camera(size: 36).toIconThemeSvgIcon(),
supportedTypes: [AttachmentPickerType.images],
optionViewBuilder: (context, controller) {
return StreamImagePicker(
onImagePicked: (image) async {
if (image != null) {
await controller.addAttachment(image);
}
return Navigator.pop(context, controller.value);
},
);
},
),
AttachmentPickerOption(
key: 'video-picker',
icon: StreamSvgIcon.record(size: 36).toIconThemeSvgIcon(),
supportedTypes: [AttachmentPickerType.videos],
optionViewBuilder: (context, controller) {
return StreamVideoPicker(
onVideoPicked: (video) async {
if (video != null) {
await controller.addAttachment(video);
}
return Navigator.pop(context, controller.value);
},
);
},
),
...{
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 {
if (selectedIds.contains(media.id)) {
return controller.removeAssetAttachment(media);
}
return controller.addAssetAttachment(media);
},
);
},
),
AttachmentPickerOption(
key: 'file-picker',
icon: StreamSvgIcon.files(size: 36).toIconThemeSvgIcon(),
supportedTypes: [AttachmentPickerType.files],
optionViewBuilder: (context, controller) {
return StreamFilePicker(
onFilePicked: (file) async {
if (file != null) await controller.addAttachment(file);
return Navigator.pop(context, controller.value);
},
);
},
),
AttachmentPickerOption(
key: 'image-picker',
icon: StreamSvgIcon.camera(size: 36).toIconThemeSvgIcon(),
supportedTypes: [AttachmentPickerType.images],
optionViewBuilder: (context, controller) {
return StreamImagePicker(
onImagePicked: (image) async {
if (image != null) {
await controller.addAttachment(image);
}
return Navigator.pop(context, controller.value);
},
);
},
),
AttachmentPickerOption(
key: 'video-picker',
icon: StreamSvgIcon.record(size: 36).toIconThemeSvgIcon(),
supportedTypes: [AttachmentPickerType.videos],
optionViewBuilder: (context, controller) {
return StreamVideoPicker(
onVideoPicked: (video) async {
if (video != null) {
await controller.addAttachment(video);
}
return Navigator.pop(context, controller.value);
},
);
},
),
}..where((option) => option.supportedTypes.every(allowedTypes.contains)),
},
);
}
@@ -779,6 +782,7 @@ Widget webOrDesktopAttachmentPickerBuilder({
required BuildContext context,
required StreamAttachmentPickerController controller,
Iterable<WebOrDesktopAttachmentPickerOption>? customOptions,
List<AttachmentPickerType> allowedTypes = AttachmentPickerType.values,
ThumbnailSize attachmentThumbnailSize = const ThumbnailSize(400, 400),
ThumbnailFormat attachmentThumbnailFormat = ThumbnailFormat.jpeg,
int attachmentThumbnailQuality = 100,
@@ -787,25 +791,27 @@ Widget webOrDesktopAttachmentPickerBuilder({
return StreamWebOrDesktopAttachmentPickerBottomSheet(
controller: controller,
options: {
if (customOptions != null) ...customOptions,
WebOrDesktopAttachmentPickerOption(
key: 'image-picker',
type: AttachmentPickerType.images,
icon: StreamSvgIcon.pictures(size: 36).toIconThemeSvgIcon(),
title: context.translations.uploadAPhotoLabel,
),
WebOrDesktopAttachmentPickerOption(
key: 'video-picker',
type: AttachmentPickerType.videos,
icon: StreamSvgIcon.record(size: 36).toIconThemeSvgIcon(),
title: context.translations.uploadAVideoLabel,
),
WebOrDesktopAttachmentPickerOption(
key: 'file-picker',
type: AttachmentPickerType.files,
icon: StreamSvgIcon.files(size: 36).toIconThemeSvgIcon(),
title: context.translations.uploadAFileLabel,
),
...{
if (customOptions != null) ...customOptions,
WebOrDesktopAttachmentPickerOption(
key: 'image-picker',
type: AttachmentPickerType.images,
icon: StreamSvgIcon.pictures(size: 36).toIconThemeSvgIcon(),
title: context.translations.uploadAPhotoLabel,
),
WebOrDesktopAttachmentPickerOption(
key: 'video-picker',
type: AttachmentPickerType.videos,
icon: StreamSvgIcon.record(size: 36).toIconThemeSvgIcon(),
title: context.translations.uploadAVideoLabel,
),
WebOrDesktopAttachmentPickerOption(
key: 'file-picker',
type: AttachmentPickerType.files,
icon: StreamSvgIcon.files(size: 36).toIconThemeSvgIcon(),
title: context.translations.uploadAFileLabel,
),
}.where((option) => option.supportedTypes.every(allowedTypes.contains)),
},
onOptionTap: (context, controller, option) async {
final attachment = await StreamAttachmentHandler.instance.pickFile(
@@ -66,6 +66,7 @@ import 'package:stream_chat_flutter/stream_chat_flutter.dart';
Future<T?> showStreamAttachmentPickerModalBottomSheet<T>({
required BuildContext context,
Iterable<AttachmentPickerOption>? customOptions,
List<AttachmentPickerType> allowedTypes = AttachmentPickerType.values,
List<Attachment>? initialAttachments,
StreamAttachmentPickerController? controller,
Color? backgroundColor,
@@ -117,6 +118,7 @@ Future<T?> showStreamAttachmentPickerModalBottomSheet<T>({
return webOrDesktopAttachmentPickerBuilder.call(
context: context,
controller: controller,
allowedTypes: allowedTypes,
customOptions: customOptions?.map(
WebOrDesktopAttachmentPickerOption.fromAttachmentPickerOption,
),
@@ -130,6 +132,7 @@ Future<T?> showStreamAttachmentPickerModalBottomSheet<T>({
return mobileAttachmentPickerBuilder.call(
context: context,
controller: controller,
allowedTypes: allowedTypes,
customOptions: customOptions,
attachmentThumbnailSize: attachmentThumbnailSize,
attachmentThumbnailFormat: attachmentThumbnailFormat,
@@ -122,6 +122,7 @@ class StreamMessageInput extends StatefulWidget {
this.maxAttachmentSize = kDefaultMaxAttachmentSize,
this.onError,
this.attachmentLimit = 10,
this.allowedAttachmentPickerTypes = AttachmentPickerType.values,
this.onAttachmentLimitExceed,
this.attachmentButtonBuilder,
this.commandButtonBuilder,
@@ -233,6 +234,12 @@ class StreamMessageInput extends StatefulWidget {
/// A limit for the no. of attachments that can be sent with a single message.
final int attachmentLimit;
/// The list of allowed attachment types which can be picked using the
/// attachment button.
///
/// By default, all the attachment types are allowed.
final List<AttachmentPickerType> allowedAttachmentPickerTypes;
/// A callback for when the [attachmentLimit] is exceeded.
///
/// This will override the default error alert behaviour.
@@ -771,8 +778,9 @@ class StreamMessageInputState extends State<StreamMessageInput>
Future<void> _onAttachmentButtonPressed() async {
final attachments = await showStreamAttachmentPickerModalBottomSheet(
context: context,
initialAttachments: _effectiveController.attachments,
useRootNavigator: true,
allowedTypes: widget.allowedAttachmentPickerTypes,
initialAttachments: _effectiveController.attachments,
);
if (attachments != null) {