@@ -16,26 +16,33 @@ class FloatingDateDivider extends StatelessWidget {
1616 required this .reverse,
1717 required this .messages,
1818 required this .itemCount,
19+ @Deprecated ('No longer used, Will be removed in future versions.' )
1920 this .isThreadConversation = false ,
2021 this .dateDividerBuilder,
2122 });
2223
2324 /// true if this is a thread conversation
25+ @Deprecated ('No longer used, Will be removed in future versions.' )
2426 final bool isThreadConversation;
2527
26- // ignore: public_member_api_docs
28+ /// A [ValueListenable] that provides the positions of items in the list view.
2729 final ValueListenable <Iterable <ItemPosition >> itemPositionListener;
2830
29- // ignore: public_member_api_docs
31+ /// Whether the list is reversed or not.
3032 final bool reverse;
3133
32- // ignore: public_member_api_docs
34+ /// The list of messages which are displayed in the list view.
3335 final List <Message > messages;
3436
35- // ignore: public_member_api_docs
37+ /// The total number of items in the list view, including special items like
38+ /// loaders, headers, and footers.
3639 final int itemCount;
3740
38- // ignore: public_member_api_docs
41+ /// A optional builder function that creates a widget to display the date
42+ /// divider.
43+ ///
44+ /// If provided, this function will be called with the date of the message
45+ /// to create the date divider widget.
3946 final Widget Function (DateTime )? dateDividerBuilder;
4047
4148 @override
@@ -47,29 +54,37 @@ class FloatingDateDivider extends StatelessWidget {
4754 return const Empty ();
4855 }
4956
50- var index = switch (reverse) {
57+ final index = switch (reverse) {
5158 true => getBottomElementIndex (positions),
5259 false => getTopElementIndex (positions),
5360 };
5461
55- if ((index == null ) ||
56- (! isThreadConversation && index == itemCount - 2 ) ||
57- (isThreadConversation && index == itemCount - 1 )) {
58- return const Empty ();
59- }
62+ if (index == null ) return const Empty ();
63+ if (! _isValidMessageIndex (index)) return const Empty ();
64+
65+ // Offset the index to account for two extra items
66+ // (loader and footer) at the bottom of the ListView.
67+ final message = messages.elementAtOrNull (index - 2 );
68+ if (message == null ) return const Empty ();
6069
61- if (index <= 2 || index >= itemCount - 3 ) {
62- if (reverse) {
63- index = itemCount - 4 ;
64- } else {
65- index = 2 ;
66- }
70+ if (dateDividerBuilder case final builder? ) {
71+ return builder.call (message.createdAt.toLocal ());
6772 }
6873
69- final message = messages[index - 2 ];
70- return dateDividerBuilder? .call (message.createdAt.toLocal ()) ??
71- StreamDateDivider (dateTime: message.createdAt.toLocal ());
74+ return StreamDateDivider (dateTime: message.createdAt.toLocal ());
7275 },
7376 );
7477 }
78+
79+ // Returns True if the item index is a valid message index and not one of the
80+ // special items (like header, footer, loaders, etc.).
81+ bool _isValidMessageIndex (int index) {
82+ if (index == itemCount - 1 ) return false ; // Parent Message
83+ if (index == itemCount - 2 ) return false ; // Header Builder
84+ if (index == itemCount - 3 ) return false ; // Top Loader Builder
85+ if (index == 1 ) return false ; // Bottom Loader Builder
86+ if (index == 0 ) return false ; // Footer Builder
87+
88+ return true ;
89+ }
7590}
0 commit comments