build method

  1. @override
Widget build(
  1. BuildContext context,
  2. ChatRoomViewModel viewModel
)

Implementation

@override
Widget build(
  BuildContext context,
  ChatRoomViewModel viewModel,
) {
  final bool isCurrentUserMessage = message.authorId == currentUserUID;

  return GestureDetector(
    onLongPress: () {
      if (isCurrentUserMessage) {
        viewModel.showDeleteConfirmation(context, message.id ?? "");
      }
    },
    child: Container(
      margin: const EdgeInsets.symmetric(vertical: 4.0, horizontal: 6),
      child: Align(
        alignment: isCurrentUserMessage
            ? Alignment.centerRight
            : Alignment.centerLeft,
        child: Column(
          children: [
            if (message.url.isNotEmpty &&
                message.url.substring(
                        message.url.length - 4, message.url.length) ==
                    "json")
              Lottie.network(message.url, width: 100),
            Container(
              margin: const EdgeInsets.symmetric(horizontal: 6),
              padding:
                  const EdgeInsets.symmetric(vertical: 6.0, horizontal: 10),
              decoration: BoxDecoration(
                color: isCurrentUserMessage
                    ? ownerBubbleColor
                    : otherBubbleColor,
                borderRadius: BorderRadius.only(
                  topLeft: Radius.circular(isCurrentUserMessage ? 10.0 : 0),
                  topRight: const Radius.circular(10.0),
                  bottomLeft: const Radius.circular(10.0),
                  bottomRight:
                      Radius.circular(isCurrentUserMessage ? 0 : 10.0),
                ),
              ),
              child: Column(
                crossAxisAlignment: isCurrentUserMessage
                    ? CrossAxisAlignment.end
                    : CrossAxisAlignment.start,
                children: [
                  if (message.text.isNotEmpty)
                    Text(
                      message.text.toString(),
                      style: const TextStyle(
                        color: Colors.black,
                      ),
                    ),
                  if (message.url.isNotEmpty && message.type == "image")
                    GestureDetector(
                      onTap: () {
                        Navigator.push(
                          context,
                          MaterialPageRoute(
                            builder: (context) => ImageView(
                              url: message.url,
                              imageDownloadButton: imageDownloadButton,
                            ),
                          ),
                        );
                      },
                      child: Image.network(
                        message.url,
                        width: MediaQuery.of(context).size.width / 2.5,
                      ),
                    ),
                  const SizedBox(height: 1.0),
                  Text(
                    timeAgo(message.createdOn),
                    style: const TextStyle(
                      color: Colors.black,
                      fontSize: 10,
                    ),
                  )
                ],
              ),
            ),
          ],
        ),
      ),
    ),
  );
}