refs #27: add upload image/file function parameters to MessageInput

This commit is contained in:
Salvatore Giordano
2020-04-21 15:35:19 +02:00
parent 4957619707
commit a156f7bdc2
2 changed files with 67 additions and 26 deletions
+65 -25
View File
@@ -16,6 +16,8 @@ import 'package:stream_chat_flutter/src/user_avatar.dart';
import '../stream_chat_flutter.dart'; import '../stream_chat_flutter.dart';
import 'stream_channel.dart'; import 'stream_channel.dart';
typedef FileUploader = Future<String> Function(File, Channel);
/// Inactive state /// Inactive state
/// ![screenshot](https://raw.githubusercontent.com/GetStream/stream-chat-flutter/master/screenshots/message_input.png) /// ![screenshot](https://raw.githubusercontent.com/GetStream/stream-chat-flutter/master/screenshots/message_input.png)
/// ![screenshot](https://raw.githubusercontent.com/GetStream/stream-chat-flutter/master/screenshots/message_input_paint.png) /// ![screenshot](https://raw.githubusercontent.com/GetStream/stream-chat-flutter/master/screenshots/message_input_paint.png)
@@ -67,6 +69,8 @@ class MessageInput extends StatefulWidget {
this.maxHeight = 150, this.maxHeight = 150,
this.keyboardType = TextInputType.multiline, this.keyboardType = TextInputType.multiline,
this.disableAttachments = false, this.disableAttachments = false,
this.doImageUploadRequest,
this.doFileUploadRequest,
}) : super(key: key); }) : super(key: key);
/// Message to edit /// Message to edit
@@ -87,14 +91,25 @@ class MessageInput extends StatefulWidget {
/// If true the attachments button will not be displayed /// If true the attachments button will not be displayed
final bool disableAttachments; final bool disableAttachments;
/// Override image upload request
final FileUploader doImageUploadRequest;
/// Override file upload request
final FileUploader doFileUploadRequest;
@override @override
_MessageInputState createState() => _MessageInputState(); _MessageInputState createState() => _MessageInputState(
doFileUploadRequest: doFileUploadRequest,
doImageUploadRequest: doImageUploadRequest,
);
} }
class _MessageInputState extends State<MessageInput> { class _MessageInputState extends State<MessageInput> {
final List<_SendingAttachment> _attachments = []; final List<_SendingAttachment> _attachments = [];
final _focusNode = FocusNode(); final _focusNode = FocusNode();
final List<User> _mentionedUsers = []; final List<User> _mentionedUsers = [];
FileUploader doImageUploadRequest;
FileUploader doFileUploadRequest;
TextEditingController _textController; TextEditingController _textController;
bool _inputEnabled = true; bool _inputEnabled = true;
@@ -102,6 +117,14 @@ class _MessageInputState extends State<MessageInput> {
bool _typingStarted = false; bool _typingStarted = false;
OverlayEntry _commandsOverlay, _mentionsOverlay; OverlayEntry _commandsOverlay, _mentionsOverlay;
_MessageInputState({
this.doImageUploadRequest,
this.doFileUploadRequest,
}) {
doImageUploadRequest ??= _uploadImage;
doFileUploadRequest ??= _uploadFile;
}
@override @override
Widget build(BuildContext context) { Widget build(BuildContext context) {
return SafeArea( return SafeArea(
@@ -616,8 +639,6 @@ class _MessageInputState extends State<MessageInput> {
final channel = StreamChannel.of(context).channel; final channel = StreamChannel.of(context).channel;
final bytes = await file.readAsBytes();
final attachment = _SendingAttachment( final attachment = _SendingAttachment(
file: file, file: file,
type: type, type: type,
@@ -627,28 +648,7 @@ class _MessageInputState extends State<MessageInput> {
_attachments.add(attachment); _attachments.add(attachment);
}); });
String url; final url = await _uploadAttachment(file, type, channel);
final filename = file.path.split('/').last;
if (type == FileType.image) {
final res = await channel.sendImage(
MultipartFile.fromBytes(
bytes,
filename: filename,
contentType: MediaType.parse(lookupMimeType(filename)),
),
);
url = res.file;
} else {
final res = await channel.sendFile(
MultipartFile.fromBytes(
bytes,
filename: filename,
contentType: MediaType.parse(lookupMimeType(filename)),
),
);
url = res.file;
}
attachment.url = url; attachment.url = url;
@@ -657,6 +657,46 @@ class _MessageInputState extends State<MessageInput> {
}); });
} }
Future<String> _uploadAttachment(
File file,
FileType type,
Channel channel,
) async {
String url;
if (type == FileType.image) {
url = await doImageUploadRequest(file, channel);
} else {
url = await doFileUploadRequest(file, channel);
}
return url;
}
Future<String> _uploadImage(File file, Channel channel) async {
final filename = file.path.split('/').last;
final bytes = await file.readAsBytes();
final res = await channel.sendImage(
MultipartFile.fromBytes(
bytes,
filename: filename,
contentType: MediaType.parse(lookupMimeType(filename)),
),
);
return res.file;
}
Future<String> _uploadFile(File file, Channel channel) async {
final filename = file.path.split('/').last;
final bytes = await file.readAsBytes();
final res = await channel.sendFile(
MultipartFile.fromBytes(
bytes,
filename: filename,
contentType: MediaType.parse(lookupMimeType(filename)),
),
);
return res.file;
}
Widget _buildSendButton(BuildContext context) { Widget _buildSendButton(BuildContext context) {
return IconTheme( return IconTheme(
data: data:
+2 -1
View File
@@ -154,7 +154,8 @@ class _MessageWidgetState extends State<MessageWidget>
UserAvatar(user: widget.message.user), UserAvatar(user: widget.message.user),
if (_isMyMessage && if (_isMyMessage &&
widget.nextMessage == null && widget.nextMessage == null &&
widget.message.status == MessageSendingStatus.SENT) (widget.message.status == MessageSendingStatus.SENT ||
widget.message.status == null))
Padding( Padding(
padding: const EdgeInsets.symmetric( padding: const EdgeInsets.symmetric(
horizontal: 1.0, horizontal: 1.0,