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,62 +132,73 @@ class _StreamAttachmentPickerState extends State<StreamAttachmentPicker> {
children: [ children: [
Row( Row(
children: [ children: [
IconButton( if (widget.allowedAttachmentTypes
icon: StreamSvgIcon.pictures( .contains(DefaultAttachmentTypes.image))
color: _getIconColor(0), IconButton(
icon: StreamSvgIcon.pictures(
color: _getIconColor(0),
),
onPressed: _attachmentContainsFile &&
messageInputController.attachments.isNotEmpty
? null
: () {
setState(() {
_filePickerIndex = 0;
});
},
), ),
onPressed: _attachmentContainsFile && if (widget.allowedAttachmentTypes
messageInputController.attachments.isNotEmpty .contains(DefaultAttachmentTypes.file))
? null IconButton(
: () { iconSize: 32,
setState(() { icon: StreamSvgIcon.files(
_filePickerIndex = 0; color: _getIconColor(1),
}); ),
}, onPressed: !_attachmentContainsFile &&
), messageInputController.attachments.isNotEmpty
IconButton( ? null
iconSize: 32, : () {
icon: StreamSvgIcon.files( widget
color: _getIconColor(1), .onFilePicked(DefaultAttachmentTypes.file);
},
), ),
onPressed: !_attachmentContainsFile && if (widget.allowedAttachmentTypes
messageInputController.attachments.isNotEmpty .contains(DefaultAttachmentTypes.image))
? null IconButton(
: () { icon: StreamSvgIcon.camera(
widget.onFilePicked(DefaultAttachmentTypes.file); color: _getIconColor(2),
}, ),
), onPressed: attachmentLimitCrossed ||
IconButton( (_attachmentContainsFile &&
icon: StreamSvgIcon.camera( messageInputController
color: _getIconColor(2), .attachments.isNotEmpty)
? null
: () {
widget.onFilePicked(
DefaultAttachmentTypes.image,
camera: true,
);
},
), ),
onPressed: attachmentLimitCrossed || if (widget.allowedAttachmentTypes
(_attachmentContainsFile && .contains(DefaultAttachmentTypes.video))
messageInputController.attachments.isNotEmpty) IconButton(
? null padding: const EdgeInsets.all(0),
: () { icon: StreamSvgIcon.record(
widget.onFilePicked( color: _getIconColor(3),
DefaultAttachmentTypes.image, ),
camera: true, onPressed: attachmentLimitCrossed ||
); (_attachmentContainsFile &&
}, messageInputController
), .attachments.isNotEmpty)
IconButton( ? null
padding: const EdgeInsets.all(0), : () {
icon: StreamSvgIcon.record( widget.onFilePicked(
color: _getIconColor(3), DefaultAttachmentTypes.video,
camera: true,
);
},
), ),
onPressed: attachmentLimitCrossed ||
(_attachmentContainsFile &&
messageInputController.attachments.isNotEmpty)
? null
: () {
widget.onFilePicked(
DefaultAttachmentTypes.video,
camera: true,
);
},
),
], ],
), ),
DecoratedBox( DecoratedBox(
@@ -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);