From 2d7ea2d3cd0dede2d26fcf62f211c596ee886638 Mon Sep 17 00:00:00 2001 From: Salvatore Giordano Date: Fri, 28 Feb 2020 13:28:01 +0100 Subject: [PATCH] add custom theme example --- example/lib/custom_theme.dart | 20 ++--- lib/src/channel_preview.dart | 2 + lib/src/message_list_view.dart | 8 +- lib/src/message_widget.dart | 150 ++++++++++++++++++--------------- pubspec.yaml | 3 +- 5 files changed, 96 insertions(+), 87 deletions(-) diff --git a/example/lib/custom_theme.dart b/example/lib/custom_theme.dart index cc65d81f..26b66584 100644 --- a/example/lib/custom_theme.dart +++ b/example/lib/custom_theme.dart @@ -23,27 +23,19 @@ class MyApp extends StatelessWidget { primarySwatch: Colors.green, ); - final streamTheme = StreamChatThemeData.fromTheme(theme); - return MaterialApp( theme: theme, home: Container( child: StreamChat( - streamChatThemeData: streamTheme.copyWith( - channelPreviewTheme: ChannelPreviewTheme( - avatarTheme: AvatarTheme( - borderRadius: BorderRadius.circular(16), - ), - ), + streamChatThemeData: StreamChatThemeData.fromTheme(theme).copyWith( ownMessageTheme: MessageTheme( - replies: streamTheme.ownMessageTheme.replies - .copyWith(fontStyle: FontStyle.italic), + messageBackgroundColor: Colors.black, + messageText: TextStyle( + color: Colors.white, + ), avatarTheme: AvatarTheme( - borderRadius: BorderRadius.circular(12), + borderRadius: BorderRadius.circular(8), ), - ), - otherMessageTheme: MessageTheme( - messageBackgroundColor: Colors.red, ), ), client: client, diff --git a/lib/src/channel_preview.dart b/lib/src/channel_preview.dart index 14ab6980..669c608b 100644 --- a/lib/src/channel_preview.dart +++ b/lib/src/channel_preview.dart @@ -103,6 +103,8 @@ class ChannelPreview extends StatelessWidget { return '📷'; } else if (e.type == 'video') { return '🎬'; + } else if (e.type == 'giphy') { + return 'GIF'; } return null; }) diff --git a/lib/src/message_list_view.dart b/lib/src/message_list_view.dart index 4db16f9d..08830143 100644 --- a/lib/src/message_list_view.dart +++ b/lib/src/message_list_view.dart @@ -218,7 +218,7 @@ class _MessageListViewState extends State { } Widget _buildBottomMessage( - StreamChannelState channel, + StreamChannelState streamChannel, Message previousMessage, Message message, BuildContext context, @@ -243,9 +243,9 @@ class _MessageListViewState extends State { key: ValueKey('BOTTOM-MESSAGE'), onVisibilityChanged: (visibility) { _isBottom = visibility.visibleBounds != Rect.zero; - if (_isBottom) { - if (channel.channel.state.unreadCount > 0) { - channel.channel.markRead(); + if (_isBottom && streamChannel.channel.config.readEvents) { + if (streamChannel.channel.state.unreadCount > 0) { + streamChannel.channel.markRead(); } } }, diff --git a/lib/src/message_widget.dart b/lib/src/message_widget.dart index 5d499f6a..a715c2b6 100644 --- a/lib/src/message_widget.dart +++ b/lib/src/message_widget.dart @@ -42,12 +42,16 @@ class _MessageWidgetState extends State final Map _chuwieControllers = {}; MessageTheme messageTheme; + StreamChatState streamChat; + StreamChannelState streamChannel; @override Widget build(BuildContext context) { super.build(context); - final streamChat = StreamChat.of(context); + streamChat = StreamChat.of(context); + streamChannel = StreamChannel.of(context); + final currentUserId = streamChat.client.state.user.id; final messageUserId = widget.message.user.id; final previousUserId = widget.previousMessage?.user?.id; @@ -73,7 +77,9 @@ class _MessageWidgetState extends State isMyMessage ? CrossAxisAlignment.end : CrossAxisAlignment.start, children: [ _buildBubble(context, isMyMessage, isLastUser), - _buildThreadIndicator(context, isMyMessage), + streamChannel.channel.config.replies + ? _buildThreadIndicator(context, isMyMessage) + : SizedBox(), isNextUser ? SizedBox() : _buildTimestamp(isMyMessage, alignment), ], ), @@ -287,12 +293,14 @@ class _MessageWidgetState extends State ? CrossAxisAlignment.end : CrossAxisAlignment.start, children: [ - Align( - child: _buildReactions(isMyMessage), - alignment: isMyMessage - ? Alignment.centerLeft - : Alignment.centerRight, - ), + streamChannel.channel.config.reactions + ? Align( + child: _buildReactions(isMyMessage), + alignment: isMyMessage + ? Alignment.centerLeft + : Alignment.centerRight, + ) + : SizedBox(), Stack( overflow: Overflow.visible, children: [ @@ -346,6 +354,11 @@ class _MessageWidgetState extends State } void _showMessageBottomSheet(BuildContext context, bool isMyMessage) { + if (!streamChannel.channel.config.reactions && + !streamChannel.channel.config.replies) { + return; + } + showModalBottomSheet( clipBehavior: Clip.hardEdge, shape: RoundedRectangleBorder( @@ -367,47 +380,52 @@ class _MessageWidgetState extends State children: [ Container( color: Colors.black87, - child: Row( - crossAxisAlignment: CrossAxisAlignment.start, - mainAxisAlignment: MainAxisAlignment.center, - mainAxisSize: MainAxisSize.min, - children: reactionToEmoji.keys.map((reactionType) { - final ownReactionIndex = widget.message.ownReactions - .indexWhere( - (reaction) => reaction.type == reactionType); - return Column( - mainAxisSize: MainAxisSize.min, - mainAxisAlignment: MainAxisAlignment.start, - children: [ - IconButton( - iconSize: fontSize + 10, - icon: Text( - reactionToEmoji[reactionType], - style: textStyle, - ), - onPressed: () { - if (ownReactionIndex != -1) { - removeReaction(reactionType); - } else { - sendReaction(reactionType); - } - }, - ), - ownReactionIndex != -1 - ? Padding( - padding: const EdgeInsets.only(bottom: 4.0), - child: Text( - widget.message - .ownReactions[ownReactionIndex].score - .toString(), - style: TextStyle(color: Colors.white), + child: streamChannel.channel.config.reactions + ? Row( + crossAxisAlignment: CrossAxisAlignment.start, + mainAxisAlignment: MainAxisAlignment.center, + mainAxisSize: MainAxisSize.min, + children: reactionToEmoji.keys.map((reactionType) { + final ownReactionIndex = widget.message.ownReactions + .indexWhere((reaction) => + reaction.type == reactionType); + return Column( + mainAxisSize: MainAxisSize.min, + mainAxisAlignment: MainAxisAlignment.start, + children: [ + IconButton( + iconSize: fontSize + 10, + icon: Text( + reactionToEmoji[reactionType], + style: textStyle, ), - ) - : SizedBox(), - ], - ); - }).toList(), - ), + onPressed: () { + if (ownReactionIndex != -1) { + removeReaction(reactionType); + } else { + sendReaction(reactionType); + } + }, + ), + ownReactionIndex != -1 + ? Padding( + padding: + const EdgeInsets.only(bottom: 4.0), + child: Text( + widget + .message + .ownReactions[ownReactionIndex] + .score + .toString(), + style: TextStyle(color: Colors.white), + ), + ) + : SizedBox(), + ], + ); + }).toList(), + ) + : SizedBox(), ), isMyMessage ? FlatButton( @@ -429,19 +447,21 @@ class _MessageWidgetState extends State }, ) : SizedBox(), - FlatButton( - child: Padding( - padding: const EdgeInsets.all(28.0), - child: Text( - 'Start a thread', - style: Theme.of(context).textTheme.headline, - ), - ), - onPressed: () { - Navigator.pop(context); - widget.onThreadTap(widget.message); - }, - ), + streamChannel.channel.config.replies + ? FlatButton( + child: Padding( + padding: const EdgeInsets.all(28.0), + child: Text( + 'Start a thread', + style: Theme.of(context).textTheme.headline, + ), + ), + onPressed: () { + Navigator.pop(context); + widget.onThreadTap(widget.message); + }, + ) + : SizedBox(), ], ), ); @@ -502,16 +522,12 @@ class _MessageWidgetState extends State }; void sendReaction(String reactionType) { - StreamChannel.of(context) - .channel - .sendReaction(widget.message.id, reactionType); + streamChannel.channel.sendReaction(widget.message.id, reactionType); Navigator.of(context).pop(); } void removeReaction(String reactionType) { - StreamChannel.of(context) - .channel - .deleteReaction(widget.message.id, reactionType); + streamChannel.channel.deleteReaction(widget.message.id, reactionType); Navigator.of(context).pop(); } diff --git a/pubspec.yaml b/pubspec.yaml index 3ee0f8b4..3e45ea8b 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -18,8 +18,7 @@ dependencies: url_launcher: ^5.4.1 video_player: ^0.10.7 chewie: ^0.9.8+1 - animations: ^1.0.0+3 - stream_chat: ^0.1.10 + stream_chat: ^0.1.11 dev_dependencies: pedantic: ^1.8.0+1