buildMessageContent method

  1. @override
Widget buildMessageContent(
  1. BuildContext context,
  2. String? refName
)
override

Implementation

@override
Widget buildMessageContent(BuildContext context, String? refName) {
  // 使用配置的文件样式
  final effectiveConfig = config ?? RCKBubbleConfig();
  final fileStyleConfig = effectiveConfig.fileStyleConfig;
  final containerWidth = fileStyleConfig.containerWidth;
  final containerHeight = fileStyleConfig.containerHeight;
  final iconSize = config?.fileStyleConfig.iconSize ?? 40;
  final fileNameStyle = fileStyleConfig.fileNameStyle;
  final fileSizeStyle = fileStyleConfig.fileSizeStyle;
  final fileIconPath =
      fileStyleConfig.customFileIconPath ??
      RCKThemeProvider().themeIcon.file ??
      "";

  final bgColor = effectiveConfig.getBubbleColor(message);
  final iconColor = RCKDesktopColorConfig.fileIcon;

  if (message is RCIMIWFileMessage) {
    final fileMessage = message as RCIMIWFileMessage;

    final key = RCKChatProvider.mediaProgressKey(fileMessage);
    final progressProvider = Provider.of<RCKDownloadProgressProvider?>(
      context,
      listen: true,
    );
    final progress = progressProvider?.getProgress(key) ?? 0;
    if (PlatformCapabilities.current().isDesktop) {
      return _buildDesktopFileContent(
        context,
        fileMessage,
        progressProvider,
        progress,
      );
    }
    return LayoutBuilder(
      builder: (context, constraints) {
        // 气泡宽度不再硬编码:优先使用设计宽度,可用宽度不足时收缩,
        // 避免窄屏下内容被顶到气泡边缘。
        final bubbleWidth = constraints.maxWidth.isFinite
            ? math.min(containerWidth, constraints.maxWidth)
            : containerWidth;
        // 文件名/大小列宽度由实际气泡宽度推导,并预留右侧内边距,
        // 保证大小文案不会贴到气泡右边框。
        final contentWidth =
            (bubbleWidth -
                    _kFileBubbleLeadingWidth -
                    iconSize -
                    _kFileBubbleTrailingPadding)
                .clamp(0.0, double.infinity)
                .toDouble();
        return Stack(
          alignment: Alignment.center,
          children: [
            ConstrainedBox(
              constraints: BoxConstraints(minHeight: containerHeight),
              child: Container(
                decoration: BoxDecoration(
                  color: bgColor,
                  borderRadius: BorderRadius.circular(6.0),
                ),
                width: bubbleWidth,
                padding: const EdgeInsets.symmetric(
                  vertical: _kFileBubbleVerticalPadding,
                ),
                child: Row(
                  children: [
                    const SizedBox(width: 10),
                    ImageUtil.getImageWidget(
                      fileIconPath,
                      fit: BoxFit.contain,
                      width: iconSize,
                      height: iconSize,
                      color: iconColor,
                    ),
                    Expanded(
                      child: Container(
                        padding: const EdgeInsets.only(left: 10),
                        child: Column(
                          mainAxisAlignment: MainAxisAlignment.center,
                          crossAxisAlignment: CrossAxisAlignment.start,
                          children: [
                            SizedBox(
                              width: contentWidth,
                              child: Text(
                                _middleEllipsizeFileName(
                                  context,
                                  fileMessage.name ?? '',
                                  fileNameStyle,
                                  contentWidth,
                                  maxLines: 2,
                                  tailLength: _kFileEllipsizeTailLength,
                                ),
                                overflow: TextOverflow.ellipsis,
                                maxLines: 2,
                                style: fileNameStyle,
                              ),
                            ),
                            SizedBox(
                              width: contentWidth,
                              child: Text(
                                FileUtil.formatFileSize(
                                  fileMessage.size ?? 0,
                                ),
                                maxLines: 1,
                                overflow: TextOverflow.ellipsis,
                                style: fileSizeStyle,
                              ),
                            ),
                          ],
                        ),
                      ),
                    ),
                  ],
                ),
              ),
            ),
            if (progress > 0 && progress < 1)
              Positioned.fill(
                child: ColoredBox(
                  color: Colors.black26,
                  child: Center(
                    child: CircularProgressIndicator(value: progress),
                  ),
                ),
              ),
          ],
        );
      },
    );
  }
  return Container();
}