Files
zh-chat-flutter/lib/views/conversation/widgets/show_image_message.dart
2022-10-26 14:05:58 +08:00

124 lines
3.6 KiB
Dart

import 'dart:io';
import 'package:cached_network_image/cached_network_image.dart';
import 'package:chat/configs/app_colors.dart';
import 'package:chat/constants/message_constant.dart';
import 'package:chat/views/conversation/preview/image_widget.dart';
import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:tencent_im_sdk_plugin/models/v2_tim_image.dart';
import 'package:tencent_im_sdk_plugin/models/v2_tim_message.dart';
class ShowImageMessage extends StatelessWidget {
final V2TimMessage message;
const ShowImageMessage(this.message, {Key? key}) : super(key: key);
@override
Widget build(BuildContext context) {
return Container(
decoration: const BoxDecoration(
borderRadius: BorderRadius.all(Radius.circular(4)),
),
constraints: BoxConstraints(
maxWidth: Get.width * 0.362,
minWidth: 64,
maxHeight: 192,
),
clipBehavior: Clip.hardEdge,
child: _imageBuilder(),
);
}
Widget _imageBuilder() {
var path = message.imageElem!.path!;
var list = message.imageElem!.imageList!;
V2TimImage? small = list.where((e) => e!.type == 2).first;
V2TimImage? big = list.where((e) => e!.type == 1).first;
V2TimImage? original = list.where((e) => e!.type == 0).first;
/// 如果是从本机发出去的图片消息,并且图片还存在的情况
if (path.isNotEmpty && File(path).existsSync()) {
return _showLocalImageFile(path, original!.localUrl!);
} else if (small != null && File(small.localUrl!).existsSync()) {
return _showLocalImageFile(small.localUrl!, original!.localUrl!);
} else if (big != null && File(big.localUrl!).existsSync()) {
return _showLocalImageFile(big.localUrl!, original!.localUrl!);
} else if (original != null && File(original.localUrl!).existsSync()) {
return _showLocalImageFile(original.localUrl!, original.localUrl!);
} else {
return GestureDetector(
onTap: () {
Get.to(
PreviewImageWidget(
type: IMG_PREVIEW_TYPE.url,
path: big!.url!,
original: original!.url!,
),
transition: Transition.zoom,
);
},
child: CachedNetworkImage(
imageUrl: small!.url!,
cacheKey: 'CHAT_IMAGE',
alignment: Alignment.topCenter,
errorWidget: (context, error, stackTrace) => _errorDisplay(),
),
);
}
}
Widget _showLocalImageFile(String path, String original) {
return GestureDetector(
onTap: () {
Get.to(
PreviewImageWidget(
type: IMG_PREVIEW_TYPE.local,
path: path,
original: original,
),
transition: Transition.zoom,
);
},
child: Image.file(
File(path),
fit: BoxFit.fitWidth,
),
);
}
Widget _errorDisplay() {
return Container(
decoration: BoxDecoration(
borderRadius: BorderRadius.circular(4),
border: Border.all(
width: 0.4,
color: AppColors.unactive,
),
),
height: 96,
child: Center(
child: Row(
mainAxisAlignment: MainAxisAlignment.center,
children: const [
Icon(
Icons.warning_amber_outlined,
size: 20,
color: AppColors.unactive,
),
SizedBox(width: 4),
Text(
'图片加载失败',
style: TextStyle(
color: AppColors.unactive,
fontSize: 12,
),
),
],
),
),
);
}
}