我想使用ListView.custom
来创建一个以日期为分隔符的聊天列表。我使用了ListView.separated
,但这会在重建聊天消息时拖慢性能,而只使用ListView.custom
就可以完美地工作。我只需要现在能够添加在日期每半小时。下面是我的ListView.separated
代码:
ListView.separated(
controller: messageScrollController,
reverse: true,
itemCount: messages.length + 1,
separatorBuilder: (context, index) {
if (index == messages.length - 1) {
return DateLabel(date: messages[index].createdAt);
}
if (messages.length == 1) {
return const SizedBox.shrink();
} else if (index >= messages.length - 1) {
return const SizedBox.shrink();
} else if (index <= messages.length) {
final message = messages[index];
final nextMessage = messages[index + 1];
if (!Jiffy.parseFromDateTime(
message.createdAt.toLocal())
.isSame(
Jiffy.parseFromDateTime(
nextMessage.createdAt.toLocal()),
unit: Unit.day)) {
return DateLabel(date: message.createdAt);
} else {
return const SizedBox.shrink();
}
} else {
return const SizedBox.shrink();
}
},
findChildIndexCallback: (key) {
final valueKey = key as ValueKey<String>;
final val = messages.indexWhere(
(element) => element.id == valueKey.value);
if (val < messages.length) {
return val;
} else {
return val + 1;
}
},
itemBuilder: (context, index) {
if (index < messages.length) {
final message = messages[index];
return AutoScrollTag(
key: ValueKey(message.id),
controller: messageScrollController,
index: index,
child: Padding(
padding: index == 0
? const EdgeInsets.only(bottom: 20)
: EdgeInsets.zero,
child: SwipeTo(
onRightSwipe:
widget.currentUser.id == message.user.id
? null
: () {
onSwipedMessage(message);
},
onLeftSwipe:
widget.currentUser.id != message.user.id
? null
: () {
onSwipedMessage(message);
},
child: MessageBubble(
key: ValueKey(message.id),
message: message,
userProfile: widget.currentUser,
nextMessage: getNext(index),
previousMessage: getPrevious(index),
hasEmoji:
hasOnlyEmojis(message.messageText ?? ''),
isMe:
widget.currentUser.id == message.user.id,
toMessage: scrollToMessage,
),
),
),
);
} else {
return const SizedBox.shrink();
}
},
)
1条答案
按热度按时间ibrsph3r1#
我发现这样做的方法是使用索引的~/ 2,与SliverList.separated的方法相同: