From c3a068a4b47de313eb7b454ef5fdc42a74de54d3 Mon Sep 17 00:00:00 2001 From: Salvatore Giordano Date: Fri, 24 Apr 2020 16:12:11 +0200 Subject: [PATCH] add unread indicator --- lib/src/channel_preview.dart | 13 +++---------- lib/src/stream_chat.dart | 1 + lib/src/stream_chat_theme.dart | 5 +++++ lib/src/unread_indicator.dart | 28 ++++++++++++++++++++++++++++ 4 files changed, 37 insertions(+), 10 deletions(-) create mode 100644 lib/src/unread_indicator.dart diff --git a/lib/src/channel_preview.dart b/lib/src/channel_preview.dart index 487df6bc..ff81cc7c 100644 --- a/lib/src/channel_preview.dart +++ b/lib/src/channel_preview.dart @@ -2,6 +2,7 @@ import 'package:flutter/material.dart'; import 'package:flutter/widgets.dart'; import 'package:jiffy/jiffy.dart'; import 'package:stream_chat/stream_chat.dart'; +import 'package:stream_chat_flutter/src/unread_indicator.dart'; import '../stream_chat_flutter.dart'; import 'channel_name.dart'; @@ -53,16 +54,8 @@ class ChannelPreview extends StatelessWidget { children: [ _buildDate(context), if (channel.state.unreadCount > 0) - Padding( - padding: const EdgeInsets.only(left: 8.0), - child: CircleAvatar( - backgroundColor: Color(0xffd0021B), - radius: 6, - child: Text( - '${channel.state.unreadCount}', - style: TextStyle(fontSize: 8), - ), - ), + UnreadIndicator( + channel: channel, ), ], ), diff --git a/lib/src/stream_chat.dart b/lib/src/stream_chat.dart index be0afda7..1e92ad1e 100644 --- a/lib/src/stream_chat.dart +++ b/lib/src/stream_chat.dart @@ -172,6 +172,7 @@ class StreamChatState extends State with WidgetsBindingObserver { title: themeData?.channelPreviewTheme?.title, lastMessageAt: themeData?.channelPreviewTheme?.lastMessageAt, subtitle: themeData?.channelPreviewTheme?.subtitle, + unreadCounterColor: themeData?.channelPreviewTheme?.unreadCounterColor, ), ); return theme; diff --git a/lib/src/stream_chat_theme.dart b/lib/src/stream_chat_theme.dart index 289f477d..058d1baf 100644 --- a/lib/src/stream_chat_theme.dart +++ b/lib/src/stream_chat_theme.dart @@ -191,6 +191,7 @@ class StreamChatThemeData { ), ), channelPreviewTheme: ChannelPreviewTheme( + unreadCounterColor: Color(0xffd0021B), avatarTheme: AvatarTheme( borderRadius: BorderRadius.circular(20), constraints: BoxConstraints.tightFor( @@ -415,12 +416,14 @@ class ChannelPreviewTheme { final TextStyle subtitle; final TextStyle lastMessageAt; final AvatarTheme avatarTheme; + final Color unreadCounterColor; const ChannelPreviewTheme({ this.title, this.subtitle, this.lastMessageAt, this.avatarTheme, + this.unreadCounterColor, }); ChannelPreviewTheme copyWith({ @@ -428,12 +431,14 @@ class ChannelPreviewTheme { TextStyle subtitle, TextStyle lastMessageAt, AvatarTheme avatarTheme, + Color unreadCounterColor, }) => ChannelPreviewTheme( title: title ?? this.title, subtitle: subtitle ?? this.subtitle, lastMessageAt: lastMessageAt ?? this.lastMessageAt, avatarTheme: avatarTheme ?? this.avatarTheme, + unreadCounterColor: unreadCounterColor ?? this.unreadCounterColor, ); } diff --git a/lib/src/unread_indicator.dart b/lib/src/unread_indicator.dart new file mode 100644 index 00000000..73f57d05 --- /dev/null +++ b/lib/src/unread_indicator.dart @@ -0,0 +1,28 @@ +import 'package:flutter/material.dart'; +import 'package:stream_chat/stream_chat.dart'; +import 'package:stream_chat_flutter/src/stream_chat_theme.dart'; + +class UnreadIndicator extends StatelessWidget { + const UnreadIndicator({ + Key key, + @required this.channel, + }) : super(key: key); + + final Channel channel; + + @override + Widget build(BuildContext context) { + return Padding( + padding: const EdgeInsets.only(left: 8.0), + child: CircleAvatar( + backgroundColor: + StreamChatTheme.of(context).channelPreviewTheme.unreadCounterColor, + radius: 6, + child: Text( + '${channel.state.unreadCount}', + style: TextStyle(fontSize: 8), + ), + ), + ); + } +}