From eb20b31c825ae430f1cdf367f40fe40e635a5cfc Mon Sep 17 00:00:00 2001 From: Salvatore Giordano Date: Fri, 11 Jun 2021 13:12:52 +0200 Subject: [PATCH] feat: add flag for floating date divider --- .../lib/src/message_list_view.dart | 80 ++++++++++--------- 1 file changed, 43 insertions(+), 37 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 82b64c273..d4885c86e 100644 --- a/packages/stream_chat_flutter/lib/src/message_list_view.dart +++ b/packages/stream_chat_flutter/lib/src/message_list_view.dart @@ -161,6 +161,7 @@ class MessageListView extends StatefulWidget { this.pinPermissions = const [], this.textBuilder, this.usernameBuilder, + this.showFloatingDateDivider = true, }) : super(key: key); /// Function used to build a custom message widget @@ -227,6 +228,9 @@ class MessageListView extends StatefulWidget { /// Flag for showing tile on header final bool showConnectionStateTile; + /// Flag for showing the floating date divider + final bool showFloatingDateDivider; + /// Function called when messages are fetched final Widget Function(BuildContext, List)? messageListBuilder; @@ -567,48 +571,50 @@ class _MessageListViewState extends State { }, ), if (widget.showScrollToBottom) _buildScrollToBottom(), - Positioned( - top: 20, - child: BetterStreamBuilder>( - initialData: _itemPositionListener.itemPositions.value, - stream: _itemPositionStream, - comparator: (a, b) { - if (a == null) { - return false; - } - final aTop = _getTopElement(a)?.index; - final bTop = _getTopElement(b)?.index; - return aTop == bTop; - }, - builder: (context, values) { - final items = _itemPositionListener.itemPositions.value; - if (items.isEmpty || messages.isEmpty) { - return const SizedBox(); - } + if (widget.showFloatingDateDivider) _buildFloatingDateDivider(), + ], + ); + } - var index = _getTopElement(values)?.index; + Positioned _buildFloatingDateDivider() => Positioned( + top: 20, + child: BetterStreamBuilder>( + initialData: _itemPositionListener.itemPositions.value, + stream: _itemPositionStream, + comparator: (a, b) { + if (a == null) { + return false; + } + final aTop = _getTopElement(a)?.index; + final bTop = _getTopElement(b)?.index; + return aTop == bTop; + }, + builder: (context, values) { + final items = _itemPositionListener.itemPositions.value; + if (items.isEmpty || messages.isEmpty) { + return const SizedBox(); + } - if (index == null || index > messages.length) { - return const SizedBox(); - } + var index = _getTopElement(values)?.index; - if (index == messages.length) { - index = max(index - 1, 0); - } + if (index == null || index > messages.length) { + return const SizedBox(); + } - return widget.dateDividerBuilder != null - ? widget.dateDividerBuilder!( - messages[index].createdAt.toLocal(), - ) - : DateDivider( - dateTime: messages[index].createdAt.toLocal(), - ); - }, - ), + if (index == messages.length) { + index = max(index - 1, 0); + } + + return widget.dateDividerBuilder != null + ? widget.dateDividerBuilder!( + messages[index].createdAt.toLocal(), + ) + : DateDivider( + dateTime: messages[index].createdAt.toLocal(), + ); + }, ), - ], - ); - } + ); Future _paginateData( StreamChannelState? channel, QueryDirection direction) =>