I am integrating a messaging system into my app and ran into an issue with hiding senders' name when previous message was sent by the same person. The way the code works is using a FirebaseAnimatedList like so:

Widget _getMessageList() {
    return FirebaseAnimatedList(
      shrinkWrap: true,
      physics: const BouncingScrollPhysics(),
      controller: _scrollController,
      query: FirebaseDatabase.instance.ref("messages/${widget.placeID}/"),
      itemBuilder: (context, snapshot, animation, index) {
        final json = snapshot.value as Map<dynamic, dynamic>;
        final message = Message.fromJson(json);
        return MessageWidget(
          message.text,
          message.date,
          message.uid,
          message.name,
        );
      },
    );
  }

Each result from the query returns a MessageWidget which looks like this:


class MessageWidget extends StatefulWidget {
  final String message;
  final String id;
  final String name;
  final DateTime date;

  const MessageWidget(this.message, this.date, this.id, this.name, {Key? key})
      : super(key: key);

  @override
  State<MessageWidget> createState() => _MessageWidgetState();
}

class _MessageWidgetState extends State<MessageWidget> {
  bool showDate = false;
  bool sentLastMessage = false;
  @override
  Widget build(BuildContext context) {
    return Padding(
      padding: const EdgeInsets.only(top: 14),
      child: Align(
        alignment: widget.id == FirebaseAuth.instance.currentUser!.uid
            ? Alignment.bottomRight
            : Alignment.bottomLeft,
        child: Padding(
          padding: widget.id == FirebaseAuth.instance.currentUser!.uid
              ? const EdgeInsets.only(right: 20)
              : const EdgeInsets.only(left: 20),
          child: GestureDetector(
            onTap: () {
              setState(() {
                showDate == false ? showDate = true : showDate = false;
              });
            },
            child: Container(
              constraints: const BoxConstraints(maxWidth: 200),
              child: Column(
                crossAxisAlignment:
                    widget.id == FirebaseAuth.instance.currentUser!.uid
                        ? CrossAxisAlignment.end
                        : CrossAxisAlignment.start,
                children: [
                  Text(
                    widget.name,
                    style: const TextStyle(
                      color: Colors.white54,
                      fontSize: 15,
                    ),
                  ),
                  const SizedBox(
                    height: 5,
                  ),
                  Align(
                    alignment:
                        widget.id == FirebaseAuth.instance.currentUser!.uid
                            ? Alignment.bottomRight
                            : Alignment.bottomLeft,
                    child: Container(
                      decoration: BoxDecoration(
                        borderRadius: BorderRadius.circular(10.0),
                        color: const Color(0xff242A2B),
                      ),
                      child: Wrap(
                        children: <Widget>[
                          Padding(
                            padding: const EdgeInsets.all(13.0),
                            child: Text(
                              widget.message,
                              style: const TextStyle(
                                color: Colors.white,
                                fontSize: 15,
                              ),
                            ),
                          ),
                        ],
                      ),
                    ),
                  ),
                  const SizedBox(
                    height: 5,
                  ),
                  showDate
                      ? Align(
                          alignment: widget.id ==
                                  FirebaseAuth.instance.currentUser!.uid
                              ? Alignment.bottomRight
                              : Alignment.bottomLeft,
                          child: Padding(
                            padding: widget.id ==
                                    FirebaseAuth.instance.currentUser!.uid
                                ? const EdgeInsets.only(right: 0)
                                : const EdgeInsets.only(left: 0),
                            child: Text(
                              DateFormat('MM-dd-yyyy, hh:mm a')
                                  .format(widget.date)
                                  .toString(),
                              style: const TextStyle(color: Colors.grey),
                            ),
                          ),
                        )
                      : Container(),
                ],
              ),
            ),
          ),
        ),
      ),
    );
  }
}

It just generates a text bubble and aligns it to the user based on whether its their message or not. The code works fine and looks like this:

enter image description here

My question is how do I hide the name on top of the message if the sender's uid is the same as the previous sender's and if the time is after the previous message's time. How do I access previous widget data in a standalone widget? I am also open to any suggestions on how to redo the messaging system without FirebaseAnimatedList since it doesn't have much documentation and there aren't many examples on how to develop messaging systems using Realtime Database vs. Firestore.

Thanks in advance!