From 8e130f7f669dddd208c209f379b532540a32d02c Mon Sep 17 00:00:00 2001 From: Salvatore Giordano Date: Fri, 17 Jun 2022 13:11:00 +0200 Subject: [PATCH] feat(ui): open message list to unread position --- .../lib/src/message_list_view.dart | 21 +++++++++++++++---- 1 file changed, 17 insertions(+), 4 deletions(-) diff --git a/packages/stream_chat_flutter/lib/src/message_list_view.dart b/packages/stream_chat_flutter/lib/src/message_list_view.dart index e73b0330..fe78ee12 100644 --- a/packages/stream_chat_flutter/lib/src/message_list_view.dart +++ b/packages/stream_chat_flutter/lib/src/message_list_view.dart @@ -387,6 +387,11 @@ class _StreamMessageListViewState extends State { if (index != 0) return index + 1; return index; } + + if (unreadCount > 0) { + return unreadCount + 1; + } + return 0; } @@ -932,6 +937,9 @@ class _StreamMessageListViewState extends State { if (unreadCount > 0) { streamChannel!.channel.markRead(); } + + final index = unreadCount > 0 ? unreadCount + 1 : 0; + if (!_upToDate) { _bottomPaginationActive = false; initialAlignment = 0; @@ -939,11 +947,11 @@ class _StreamMessageListViewState extends State { await streamChannel!.reloadChannel(); WidgetsBinding.instance.addPostFrameCallback((_) { - _scrollController!.jumpTo(index: unreadCount); + _scrollController!.jumpTo(index: index); }); } else { _scrollController!.scrollTo( - index: unreadCount, + index: index, duration: const Duration(seconds: 1), curve: Curves.easeInOut, ); @@ -1366,6 +1374,8 @@ class _StreamMessageListViewState extends State { if (newStreamChannel != streamChannel) { streamChannel = newStreamChannel; _messageNewListener?.cancel(); + + unreadCount = streamChannel?.channel.state?.unreadCount ?? 0; initialIndex = _initialIndex; initialAlignment = _initialAlignment; @@ -1376,8 +1386,10 @@ class _StreamMessageListViewState extends State { ); } - _messageNewListener = - streamChannel!.channel.on(EventType.messageNew).listen((event) { + _messageNewListener = streamChannel!.channel + .on(EventType.messageNew) + .skip(1) //skipping the first event + .listen((event) { if (_upToDate) { _bottomPaginationActive = false; } @@ -1395,6 +1407,7 @@ class _StreamMessageListViewState extends State { ); }); } else if (streamChannel?.channel.state?.unreadCount != 0) { + print('increasing unread'); setState(() { unreadCount = unreadCount + 1; });