我正在使随机聊天应用使用FirestoreAnimatedList
(对于Firestore,是FirebaseAnimatedList
)。
我需要实现:如果user1发送许多连续消息,则每条消息都没有显示用户头像。在最后一条消息旁边仅显示一次。但是,如果其他用户2发送消息,则下次用户1发送消息时,将再次显示化身。
我尝试但无法解决!到目前为止,我的代码还不好,因为在重建列表时,有时会在错误消息旁边显示头像,有时又会在正确位置显示错误消息。
class MainState extends State<Main> {
…
final Map<int, dynamic> map = {};
body: FirestoreAnimatedList(
query: firestore.collection('messages').snapshots(),
itemBuilder: (
BuildContext context,
DocumentSnapshot snapshot,
Animation<double> animation,
int index,
) {
map[index] = snapshot;
return FadeTransition(
opacity: animation,
child: MessageItem(
index: index,
document: snapshot,
map: map,
),
);
},
class MessageItem extends StatelessWidget {
…
@override
Widget build(BuildContext context) {
return Container(
child: new Row(
children: <Widget>[
new Container(
child:
_sameUser() ?
new Icon(
Icons.account_circle
)
: Container()
bool _sameUser () {
assert(index >= 0);
assert(map != null);
return map[index + 1] != null && map[index + 1]['fromUser'] == map[index]['fromUser'];
}
从我的测试问题来看,仅出现在第一条和第二条连续消息中。对于> 2个连续的消息代码,请按预期执行:消息旁边没有头像。 (但是,如果连续的链中断,则重新开始发布:例如其他user2发送消息。然后针对user1的连续消息再次发布)
有人帮忙吗? 谢谢!