@@ -16,26 +16,33 @@ class FloatingDateDivider extends StatelessWidget {
16
16
required this .reverse,
17
17
required this .messages,
18
18
required this .itemCount,
19
+ @Deprecated ('No longer used, Will be removed in future versions.' )
19
20
this .isThreadConversation = false ,
20
21
this .dateDividerBuilder,
21
22
});
22
23
23
24
/// true if this is a thread conversation
25
+ @Deprecated ('No longer used, Will be removed in future versions.' )
24
26
final bool isThreadConversation;
25
27
26
- // ignore: public_member_api_docs
28
+ /// A [ValueListenable] that provides the positions of items in the list view.
27
29
final ValueListenable <Iterable <ItemPosition >> itemPositionListener;
28
30
29
- // ignore: public_member_api_docs
31
+ /// Whether the list is reversed or not.
30
32
final bool reverse;
31
33
32
- // ignore: public_member_api_docs
34
+ /// The list of messages which are displayed in the list view.
33
35
final List <Message > messages;
34
36
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.
36
39
final int itemCount;
37
40
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.
39
46
final Widget Function (DateTime )? dateDividerBuilder;
40
47
41
48
@override
@@ -47,29 +54,37 @@ class FloatingDateDivider extends StatelessWidget {
47
54
return const Empty ();
48
55
}
49
56
50
- var index = switch (reverse) {
57
+ final index = switch (reverse) {
51
58
true => getBottomElementIndex (positions),
52
59
false => getTopElementIndex (positions),
53
60
};
54
61
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 ();
60
69
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 ());
67
72
}
68
73
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 ());
72
75
},
73
76
);
74
77
}
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
+ }
75
90
}
0 commit comments