add DefaultAttachmentTypes

This commit is contained in:
Salvatore Giordano
2020-04-29 12:17:22 +02:00
parent ff77d571c5
commit cc876a9b2d
+47 -31
View File
@@ -18,11 +18,19 @@ import 'stream_channel.dart';
typedef FileUploader = Future<String> Function(File, Channel); typedef FileUploader = Future<String> Function(File, Channel);
typedef AttachmentThumbnailBuilder = Widget Function( typedef AttachmentThumbnailBuilder = Widget Function(
BuildContext, _SendingAttachment); BuildContext,
_SendingAttachment,
);
enum ActionsLocation { enum ActionsLocation {
LEFT, left,
RIGHT, right,
}
enum DefaultAttachmentTypes {
image,
video,
file,
} }
/// Inactive state /// Inactive state
@@ -83,7 +91,7 @@ class MessageInput extends StatefulWidget {
this.initialMessage, this.initialMessage,
this.textEditingController, this.textEditingController,
this.actions, this.actions,
this.actionsLocation = ActionsLocation.LEFT, this.actionsLocation = ActionsLocation.left,
this.attachmentThumbnailBuilder, this.attachmentThumbnailBuilder,
}) : super(key: key); }) : super(key: key);
@@ -207,11 +215,11 @@ class MessageInputState extends State<MessageInput> {
crossAxisAlignment: CrossAxisAlignment.end, crossAxisAlignment: CrossAxisAlignment.end,
children: <Widget>[ children: <Widget>[
if (!widget.disableAttachments) _buildAttachmentButton(), if (!widget.disableAttachments) _buildAttachmentButton(),
if (widget.actionsLocation == ActionsLocation.LEFT) if (widget.actionsLocation == ActionsLocation.left)
...widget.actions ?? [], ...widget.actions ?? [],
_buildTextInput(context), _buildTextInput(context),
_animateSendButton(context), _animateSendButton(context),
if (widget.actionsLocation == ActionsLocation.RIGHT) if (widget.actionsLocation == ActionsLocation.right)
...widget.actions ?? [], ...widget.actions ?? [],
], ],
); );
@@ -558,15 +566,18 @@ class MessageInputState extends State<MessageInput> {
attachment, attachment,
); );
} }
switch (attachment.attachment.type) { switch (attachment.attachment.type) {
case 'image': case 'image':
case 'giphy':
return attachment.file != null return attachment.file != null
? Image.file( ? Image.file(
attachment.file, attachment.file,
fit: BoxFit.cover, fit: BoxFit.cover,
) )
: Image.network( : Image.network(
attachment.attachment.imageUrl, attachment.attachment.imageUrl ??
attachment.attachment.thumbUrl,
fit: BoxFit.cover, fit: BoxFit.cover,
); );
break; break;
@@ -633,7 +644,7 @@ class MessageInputState extends State<MessageInput> {
leading: Icon(Icons.image), leading: Icon(Icons.image),
title: Text('Upload a photo'), title: Text('Upload a photo'),
onTap: () { onTap: () {
pickFile('image', false); pickFile(DefaultAttachmentTypes.image, false);
Navigator.pop(context); Navigator.pop(context);
}, },
), ),
@@ -641,7 +652,7 @@ class MessageInputState extends State<MessageInput> {
leading: Icon(Icons.video_library), leading: Icon(Icons.video_library),
title: Text('Upload a video'), title: Text('Upload a video'),
onTap: () { onTap: () {
pickFile('video', false); pickFile(DefaultAttachmentTypes.video, false);
Navigator.pop(context); Navigator.pop(context);
}, },
), ),
@@ -649,7 +660,7 @@ class MessageInputState extends State<MessageInput> {
leading: Icon(Icons.camera_alt), leading: Icon(Icons.camera_alt),
title: Text('Photo from camera'), title: Text('Photo from camera'),
onTap: () { onTap: () {
pickFile('image', true); pickFile(DefaultAttachmentTypes.image, true);
Navigator.pop(context); Navigator.pop(context);
}, },
), ),
@@ -657,7 +668,7 @@ class MessageInputState extends State<MessageInput> {
leading: Icon(Icons.videocam), leading: Icon(Icons.videocam),
title: Text('Video from camera'), title: Text('Video from camera'),
onTap: () { onTap: () {
pickFile('video', true); pickFile(DefaultAttachmentTypes.video, true);
Navigator.pop(context); Navigator.pop(context);
}, },
), ),
@@ -665,7 +676,7 @@ class MessageInputState extends State<MessageInput> {
leading: Icon(Icons.insert_drive_file), leading: Icon(Icons.insert_drive_file),
title: Text('Upload a file'), title: Text('Upload a file'),
onTap: () { onTap: () {
pickFile('file', false); pickFile(DefaultAttachmentTypes.file, false);
Navigator.pop(context); Navigator.pop(context);
}, },
), ),
@@ -686,30 +697,38 @@ class MessageInputState extends State<MessageInput> {
} }
/// Pick a file from the device /// Pick a file from the device
/// The [attachmentType] should be one of 'image', 'video' or 'file'
/// If [camera] is true then the camera will open /// If [camera] is true then the camera will open
void pickFile(String attachmentType, [bool camera = false]) async { void pickFile(DefaultAttachmentTypes fileType, [bool camera = false]) async {
setState(() { setState(() {
_inputEnabled = false; _inputEnabled = false;
}); });
File file; File file;
FileType type; String attachmentType;
if (attachmentType == 'image') {
type = FileType.image; if (fileType == DefaultAttachmentTypes.image) {
} else if (attachmentType == 'video') { attachmentType = 'image';
type = FileType.video; } else if (fileType == DefaultAttachmentTypes.video) {
} else if (attachmentType == 'file') { attachmentType = 'video';
type = FileType.any; } else if (fileType == DefaultAttachmentTypes.file) {
attachmentType = 'file';
} }
if (camera) { if (camera) {
if (type == FileType.image) { if (fileType == DefaultAttachmentTypes.image) {
file = await ImagePicker.pickImage(source: ImageSource.camera); file = await ImagePicker.pickImage(source: ImageSource.camera);
} else if (type == FileType.video) { } else if (fileType == DefaultAttachmentTypes.video) {
file = await ImagePicker.pickVideo(source: ImageSource.camera); file = await ImagePicker.pickVideo(source: ImageSource.camera);
} }
} else { } else {
FileType type;
if (fileType == DefaultAttachmentTypes.image) {
type = FileType.image;
} else if (fileType == DefaultAttachmentTypes.video) {
type = FileType.video;
} else if (fileType == DefaultAttachmentTypes.file) {
type = FileType.any;
}
file = await FilePicker.getFile(type: type); file = await FilePicker.getFile(type: type);
} }
@@ -735,9 +754,9 @@ class MessageInputState extends State<MessageInput> {
_attachments.add(attachment); _attachments.add(attachment);
}); });
final url = await _uploadAttachment(file, type, channel); final url = await _uploadAttachment(file, fileType, channel);
if (attachmentType == 'image') { if (fileType == DefaultAttachmentTypes.image) {
attachment.attachment = attachment.attachment.copyWith( attachment.attachment = attachment.attachment.copyWith(
imageUrl: url, imageUrl: url,
); );
@@ -754,11 +773,11 @@ class MessageInputState extends State<MessageInput> {
Future<String> _uploadAttachment( Future<String> _uploadAttachment(
File file, File file,
FileType type, DefaultAttachmentTypes type,
Channel channel, Channel channel,
) async { ) async {
String url; String url;
if (type == FileType.image) { if (type == DefaultAttachmentTypes.image) {
url = await doImageUploadRequest(file, channel); url = await doImageUploadRequest(file, channel);
} else { } else {
url = await doFileUploadRequest(file, channel); url = await doFileUploadRequest(file, channel);
@@ -846,10 +865,7 @@ class MessageInputState extends State<MessageInput> {
message = widget.editMessage.copyWith( message = widget.editMessage.copyWith(
parentId: widget.parentMessage?.id, parentId: widget.parentMessage?.id,
text: text, text: text,
attachments: widget.editMessage.attachments attachments: _getAttachments(attachments).toList(),
.where((attachment) => attachment.type == 'giphy')
.toList() +
_getAttachments(attachments).toList(),
mentionedUsers: mentionedUsers:
_mentionedUsers.where((u) => text.contains('@${u.name}')).toList(), _mentionedUsers.where((u) => text.contains('@${u.name}')).toList(),
); );