Merge branch 'master' into feature/notifications

This commit is contained in:
Salvatore Giordano
2020-04-22 10:53:41 +02:00
6 changed files with 210 additions and 107 deletions
+30 -16
View File
@@ -57,11 +57,19 @@ class ChannelHeader extends StatelessWidget implements PreferredSizeWidget {
/// By default it calls [Navigator.pop] /// By default it calls [Navigator.pop]
final VoidCallback onBackPressed; final VoidCallback onBackPressed;
/// Callback to call when the header is tapped.
final VoidCallback onTitleTap;
/// Callback to call when the image is tapped.
final VoidCallback onImageTap;
/// Creates a channel header /// Creates a channel header
ChannelHeader({ ChannelHeader({
Key key, Key key,
this.showBackButton = true, this.showBackButton = true,
this.onBackPressed, this.onBackPressed,
this.onTitleTap,
this.onImageTap,
}) : preferredSize = Size.fromHeight(kToolbarHeight), }) : preferredSize = Size.fromHeight(kToolbarHeight),
super(key: key); super(key: key);
@@ -76,27 +84,33 @@ class ChannelHeader extends StatelessWidget implements PreferredSizeWidget {
actions: <Widget>[ actions: <Widget>[
Padding( Padding(
padding: const EdgeInsets.only(right: 10.0), padding: const EdgeInsets.only(right: 10.0),
child: Stack( child: Center(
children: <Widget>[ child: ChannelImage(
Center( onTap: onImageTap,
child: ChannelImage(), ),
),
],
), ),
), ),
], ],
centerTitle: true, centerTitle: true,
title: Column( title: InkWell(
crossAxisAlignment: CrossAxisAlignment.center, onTap: onTitleTap,
children: <Widget>[ child: Container(
ChannelName( height: preferredSize.height,
textStyle: StreamChatTheme.of(context) width: preferredSize.width,
.channelTheme child: Column(
.channelHeaderTheme crossAxisAlignment: CrossAxisAlignment.center,
.title, mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
ChannelName(
textStyle: StreamChatTheme.of(context)
.channelTheme
.channelHeaderTheme
.title,
),
_buildLastActive(context, channel),
],
), ),
_buildLastActive(context, channel), ),
],
), ),
); );
} }
+34 -19
View File
@@ -48,6 +48,7 @@ class ChannelImage extends StatelessWidget {
Key key, Key key,
this.channel, this.channel,
this.constraints, this.constraints,
this.onTap,
}) : super(key: key); }) : super(key: key);
/// The channel to show the image of /// The channel to show the image of
@@ -56,6 +57,9 @@ class ChannelImage extends StatelessWidget {
/// The diameter of the image /// The diameter of the image
final BoxConstraints constraints; final BoxConstraints constraints;
/// The function called when the image is tapped
final VoidCallback onTap;
@override @override
Widget build(BuildContext context) { Widget build(BuildContext context) {
final client = StreamChat.of(context); final client = StreamChat.of(context);
@@ -87,25 +91,36 @@ class ChannelImage extends StatelessWidget {
decoration: BoxDecoration( decoration: BoxDecoration(
color: StreamChatTheme.of(context).accentColor, color: StreamChatTheme.of(context).accentColor,
), ),
child: image != null child: Stack(
? CachedNetworkImage( fit: StackFit.expand,
imageUrl: image, children: <Widget>[
errorWidget: (_, __, ___) { image != null
return Center( ? CachedNetworkImage(
child: Text( imageUrl: image,
snapshot.data?.containsKey('name') ?? false errorWidget: (_, __, ___) {
? snapshot.data['name'][0] return Center(
: '', child: Text(
style: TextStyle( snapshot.data?.containsKey('name') ?? false
color: Colors.white, ? snapshot.data['name'][0]
fontWeight: FontWeight.bold, : '',
), style: TextStyle(
), color: Colors.white,
); fontWeight: FontWeight.bold,
}, ),
fit: BoxFit.cover, ),
) );
: SizedBox(), },
fit: BoxFit.cover,
)
: SizedBox(),
Material(
color: Colors.transparent,
child: InkWell(
onTap: onTap,
),
),
],
),
), ),
); );
}); });
+18 -14
View File
@@ -58,6 +58,7 @@ class ChannelListView extends StatefulWidget {
this.channelWidget, this.channelWidget,
this.channelPreviewBuilder, this.channelPreviewBuilder,
this.errorBuilder, this.errorBuilder,
this.onImageTap,
}) : super(key: key); }) : super(key: key);
/// The builder that will be used in case of error /// The builder that will be used in case of error
@@ -97,6 +98,9 @@ class ChannelListView extends StatefulWidget {
/// Builder used to create a custom channel preview /// Builder used to create a custom channel preview
final ChannelPreviewBuilder channelPreviewBuilder; final ChannelPreviewBuilder channelPreviewBuilder;
/// The function called when the image is tapped
final Function(Channel) onImageTap;
@override @override
_ChannelListViewState createState() => _ChannelListViewState(); _ChannelListViewState createState() => _ChannelListViewState();
} }
@@ -219,11 +223,6 @@ class _ChannelListViewState extends State<ChannelListView>
if (i < channels.length) { if (i < channels.length) {
final channel = channels[i]; final channel = channels[i];
final channelClient = channelsProvider.channels.firstWhere(
(c) => c.cid == channel.cid,
orElse: () => null,
);
ChannelTapCallback onTap; ChannelTapCallback onTap;
if (widget.onChannelTap != null) { if (widget.onChannelTap != null) {
onTap = widget.onChannelTap; onTap = widget.onChannelTap;
@@ -244,11 +243,11 @@ class _ChannelListViewState extends State<ChannelListView>
} }
return StreamChannel( return StreamChannel(
key: ValueKey<String>('CHANNEL-${channelClient.id}'), key: ValueKey<String>('CHANNEL-${channel.id}'),
channel: channelClient, channel: channel,
child: StreamBuilder<DateTime>( child: StreamBuilder<DateTime>(
initialData: channelClient.updatedAt, initialData: channel.updatedAt,
stream: channelClient.updatedAtStream, stream: channel.updatedAtStream,
builder: (context, snapshot) { builder: (context, snapshot) {
Widget child; Widget child;
if (widget.channelPreviewBuilder != null) { if (widget.channelPreviewBuilder != null) {
@@ -256,14 +255,14 @@ class _ChannelListViewState extends State<ChannelListView>
children: [ children: [
widget.channelPreviewBuilder( widget.channelPreviewBuilder(
context, context,
channelClient, channel,
), ),
Positioned.fill( Positioned.fill(
child: Material( child: Material(
color: Colors.transparent, color: Colors.transparent,
child: InkWell( child: InkWell(
onTap: () { onTap: () {
onTap(channelClient, widget.channelWidget); onTap(channel, widget.channelWidget);
}, },
), ),
), ),
@@ -272,9 +271,14 @@ class _ChannelListViewState extends State<ChannelListView>
); );
} else { } else {
child = ChannelPreview( child = ChannelPreview(
channel: channelClient, channel: channel,
onTap: (channelClient) { onImageTap: widget.onImageTap != null
onTap(channelClient, widget.channelWidget); ? () {
widget.onImageTap(channel);
}
: null,
onTap: (channel) {
onTap(channel, widget.channelWidget);
}, },
); );
} }
+7 -2
View File
@@ -25,11 +25,14 @@ class ChannelPreview extends StatelessWidget {
/// Channel displayed /// Channel displayed
final Channel channel; final Channel channel;
/// Instantiate a new ChannelPreview /// The function called when the image is tapped
final VoidCallback onImageTap;
ChannelPreview({ ChannelPreview({
@required this.channel, @required this.channel,
Key key, Key key,
this.onTap, this.onTap,
this.onImageTap,
}) : super(key: key); }) : super(key: key);
@override @override
@@ -38,7 +41,9 @@ class ChannelPreview extends StatelessWidget {
onTap: () { onTap: () {
onTap(channel); onTap(channel);
}, },
leading: ChannelImage(), leading: ChannelImage(
onTap: onImageTap,
),
title: ChannelName( title: ChannelName(
textStyle: StreamChatTheme.of(context).channelPreviewTheme.title, textStyle: StreamChatTheme.of(context).channelPreviewTheme.title,
), ),
+105 -55
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)
@@ -68,11 +70,17 @@ 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,
this.initialMessage,
}) : super(key: key); }) : super(key: key);
/// Message to edit /// Message to edit
final Message editMessage; final Message editMessage;
/// Message to start with
final Message initialMessage;
/// Function called after sending the message /// Function called after sending the message
final void Function(Message) onMessageSent; final void Function(Message) onMessageSent;
@@ -88,14 +96,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;
@@ -103,6 +122,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(
@@ -617,8 +644,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,
@@ -628,28 +653,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;
@@ -658,6 +662,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:
@@ -727,7 +771,7 @@ class _MessageInputState extends State<MessageInput> {
channel.cid, channel.cid,
); );
} else { } else {
message = Message( message = (widget.initialMessage ?? Message()).copyWith(
parentId: widget.parentMessage?.id, parentId: widget.parentMessage?.id,
text: text, text: text,
attachments: _getAttachments(attachments).toList(), attachments: _getAttachments(attachments).toList(),
@@ -804,40 +848,46 @@ class _MessageInputState extends State<MessageInput> {
}); });
if (widget.editMessage != null) { if (widget.editMessage != null) {
_textController = TextEditingController(text: widget.editMessage.text); _parseExistingMessage(widget.editMessage);
} else if (widget.initialMessage != null) {
_typingStarted = true; _parseExistingMessage(widget.initialMessage);
_messageIsPresent = true;
widget.editMessage.attachments.forEach((attachment) {
if (attachment.type == 'image') {
_attachments.add(_SendingAttachment(
type: FileType.image,
url: attachment.imageUrl ??
attachment.assetUrl ??
attachment.thumbUrl ??
attachment.ogScrapeUrl,
uploaded: true,
));
} else if (attachment.type == 'video') {
_attachments.add(_SendingAttachment(
type: FileType.video,
url: attachment.assetUrl,
uploaded: true,
));
} else if (attachment.type != 'giphy') {
_attachments.add(_SendingAttachment(
type: FileType.any,
url: attachment.assetUrl,
uploaded: true,
));
}
});
} else { } else {
_textController = TextEditingController(); _textController = TextEditingController();
} }
} }
void _parseExistingMessage(Message message) {
_textController = TextEditingController(text: message.text);
_typingStarted = true;
_messageIsPresent = true;
message.attachments?.forEach((attachment) {
if (attachment.type == 'image') {
_attachments.add(_SendingAttachment(
type: FileType.image,
url: attachment.imageUrl ??
attachment.assetUrl ??
attachment.thumbUrl ??
attachment.ogScrapeUrl,
uploaded: true,
));
} else if (attachment.type == 'video') {
_attachments.add(_SendingAttachment(
type: FileType.video,
url: attachment.assetUrl,
uploaded: true,
));
} else if (attachment.type != 'giphy') {
_attachments.add(_SendingAttachment(
type: FileType.any,
url: attachment.assetUrl,
uploaded: true,
));
}
});
}
@override @override
void dispose() { void dispose() {
_commandsOverlay?.remove(); _commandsOverlay?.remove();
+16 -1
View File
@@ -214,7 +214,7 @@ class _MessageWidgetState extends State<MessageWidget>
var nOfAttachmentWidgets = 0; var nOfAttachmentWidgets = 0;
final column = final column =
List<Widget>.from(widget.message.attachments?.map((attachment) { List<Widget>.from(widget.message.attachments.map((attachment) {
nOfAttachmentWidgets++; nOfAttachmentWidgets++;
Widget attachmentWidget; Widget attachmentWidget;
@@ -223,6 +223,21 @@ class _MessageWidgetState extends State<MessageWidget>
} else if (attachment.type == 'image' || } else if (attachment.type == 'image' ||
attachment.type == 'giphy') { attachment.type == 'giphy') {
attachmentWidget = _buildImage(attachment); attachmentWidget = _buildImage(attachment);
} else if (attachment.type == 'file') {
attachmentWidget = Material(
child: InkWell(
onTap: () {
_launchURL(attachment.assetUrl);
},
child: Container(
width: 100,
height: 100,
child: Center(
child: Icon(Icons.attach_file),
),
),
),
);
} }
if (attachmentWidget != null) { if (attachmentWidget != null) {