feat: Added allowed attachment types

This commit is contained in:
Deven Joshi
2021-11-04 16:38:26 +05:30
parent 1e01db318b
commit e973f98308
@@ -36,6 +36,8 @@ class StreamAttachmentPicker extends StatefulWidget {
/// do not set it if you're using our default CDN /// do not set it if you're using our default CDN
final int maxAttachmentSize; final int maxAttachmentSize;
final List<DefaultAttachmentTypes> allowedAttachmentTypes;
const StreamAttachmentPicker({ const StreamAttachmentPicker({
Key? key, Key? key,
required this.messageInputController, required this.messageInputController,
@@ -49,6 +51,11 @@ class StreamAttachmentPicker extends StatefulWidget {
this.compressedVideoFrameRate = 30, this.compressedVideoFrameRate = 30,
this.onChangeInputState, this.onChangeInputState,
this.onError, this.onError,
this.allowedAttachmentTypes = const [
DefaultAttachmentTypes.image,
DefaultAttachmentTypes.file,
DefaultAttachmentTypes.video,
],
}) : super(key: key); }) : super(key: key);
@override @override
@@ -125,6 +132,8 @@ class _StreamAttachmentPickerState extends State<StreamAttachmentPicker> {
children: [ children: [
Row( Row(
children: [ children: [
if (widget.allowedAttachmentTypes
.contains(DefaultAttachmentTypes.image))
IconButton( IconButton(
icon: StreamSvgIcon.pictures( icon: StreamSvgIcon.pictures(
color: _getIconColor(0), color: _getIconColor(0),
@@ -138,6 +147,8 @@ class _StreamAttachmentPickerState extends State<StreamAttachmentPicker> {
}); });
}, },
), ),
if (widget.allowedAttachmentTypes
.contains(DefaultAttachmentTypes.file))
IconButton( IconButton(
iconSize: 32, iconSize: 32,
icon: StreamSvgIcon.files( icon: StreamSvgIcon.files(
@@ -147,16 +158,20 @@ class _StreamAttachmentPickerState extends State<StreamAttachmentPicker> {
messageInputController.attachments.isNotEmpty messageInputController.attachments.isNotEmpty
? null ? null
: () { : () {
widget.onFilePicked(DefaultAttachmentTypes.file); widget
.onFilePicked(DefaultAttachmentTypes.file);
}, },
), ),
if (widget.allowedAttachmentTypes
.contains(DefaultAttachmentTypes.image))
IconButton( IconButton(
icon: StreamSvgIcon.camera( icon: StreamSvgIcon.camera(
color: _getIconColor(2), color: _getIconColor(2),
), ),
onPressed: attachmentLimitCrossed || onPressed: attachmentLimitCrossed ||
(_attachmentContainsFile && (_attachmentContainsFile &&
messageInputController.attachments.isNotEmpty) messageInputController
.attachments.isNotEmpty)
? null ? null
: () { : () {
widget.onFilePicked( widget.onFilePicked(
@@ -165,6 +180,8 @@ class _StreamAttachmentPickerState extends State<StreamAttachmentPicker> {
); );
}, },
), ),
if (widget.allowedAttachmentTypes
.contains(DefaultAttachmentTypes.video))
IconButton( IconButton(
padding: const EdgeInsets.all(0), padding: const EdgeInsets.all(0),
icon: StreamSvgIcon.record( icon: StreamSvgIcon.record(
@@ -172,7 +189,8 @@ class _StreamAttachmentPickerState extends State<StreamAttachmentPicker> {
), ),
onPressed: attachmentLimitCrossed || onPressed: attachmentLimitCrossed ||
(_attachmentContainsFile && (_attachmentContainsFile &&
messageInputController.attachments.isNotEmpty) messageInputController
.attachments.isNotEmpty)
? null ? null
: () { : () {
widget.onFilePicked( widget.onFilePicked(
@@ -205,7 +223,11 @@ class _StreamAttachmentPickerState extends State<StreamAttachmentPicker> {
), ),
), ),
), ),
if (widget.isOpen) if (widget.isOpen &&
(widget.allowedAttachmentTypes
.contains(DefaultAttachmentTypes.image) ||
(widget.allowedAttachmentTypes
.contains(DefaultAttachmentTypes.file))))
Expanded( Expanded(
child: DecoratedBox( child: DecoratedBox(
decoration: BoxDecoration( decoration: BoxDecoration(
@@ -229,6 +251,7 @@ class _StreamAttachmentPickerState extends State<StreamAttachmentPicker> {
_addAssetAttachment(media); _addAssetAttachment(media);
} }
}, },
allowedAttachmentTypes: widget.allowedAttachmentTypes,
), ),
), ),
), ),
@@ -326,6 +349,7 @@ class _PickerWidget extends StatefulWidget {
required this.onAddMoreFilesClick, required this.onAddMoreFilesClick,
required this.onMediaSelected, required this.onMediaSelected,
required this.streamChatTheme, required this.streamChatTheme,
required this.allowedAttachmentTypes,
}) : super(key: key); }) : super(key: key);
final int filePickerIndex; final int filePickerIndex;
@@ -334,6 +358,7 @@ class _PickerWidget extends StatefulWidget {
final void Function(DefaultAttachmentTypes) onAddMoreFilesClick; final void Function(DefaultAttachmentTypes) onAddMoreFilesClick;
final void Function(AssetEntity) onMediaSelected; final void Function(AssetEntity) onMediaSelected;
final StreamChatThemeData streamChatTheme; final StreamChatThemeData streamChatTheme;
final List<DefaultAttachmentTypes> allowedAttachmentTypes;
@override @override
_PickerWidgetState createState() => _PickerWidgetState(); _PickerWidgetState createState() => _PickerWidgetState();
@@ -361,7 +386,9 @@ class _PickerWidgetState extends State<_PickerWidget> {
} }
if (snapshot.data!) { if (snapshot.data!) {
if (widget.containsFile) { if (widget.containsFile ||
!widget.allowedAttachmentTypes
.contains(DefaultAttachmentTypes.image)) {
return GestureDetector( return GestureDetector(
onTap: () { onTap: () {
widget.onAddMoreFilesClick(DefaultAttachmentTypes.file); widget.onAddMoreFilesClick(DefaultAttachmentTypes.file);