tencent_cloud_chat_uikit_fl.../lib/ui/widgets/avatar.dart

211 lines
7.2 KiB
Dart

import 'package:cached_network_image/cached_network_image.dart';
import 'package:flutter/material.dart';
import 'package:tencent_cloud_chat_sdk/models/v2_tim_user_status.dart'
if (dart.library.html) 'package:tencent_cloud_chat_sdk/web/compatible_models/v2_tim_user_status.dart';
import 'package:tencent_cloud_chat_uikit/business_logic/view_models/tui_self_info_view_model.dart';
import 'package:tencent_cloud_chat_uikit/ui/utils/common_utils.dart';
import 'package:tencent_cloud_chat_uikit/ui/widgets/image_screen.dart';
import 'package:tencent_cloud_chat_uikit/base_widgets/tim_ui_kit_statelesswidget.dart';
import 'package:tencent_cloud_chat_uikit/data_services/core/core_services_implements.dart';
import 'package:tencent_cloud_chat_uikit/data_services/services_locatar.dart';
import 'package:tencent_cloud_chat_uikit/base_widgets/tim_ui_kit_base.dart';
import 'package:tencent_cloud_chat_uikit/theme/tui_theme.dart';
class Avatar extends TIMUIKitStatelessWidget {
final String faceUrl;
final String showName;
final bool isFromLocalAsset;
final CoreServicesImpl coreService = serviceLocator<CoreServicesImpl>();
final BorderRadius? borderRadius;
final V2TimUserStatus? onlineStatus;
final int? type; // 1 c2c 2 group
final bool isShowBigWhenClick;
final bool isCircle; // 是否显示为圆形头像
final TUISelfInfoViewModel selfInfoViewModel =
serviceLocator<TUISelfInfoViewModel>();
Avatar(
{Key? key,
required this.faceUrl,
this.onlineStatus,
required this.showName,
this.isShowBigWhenClick = false,
this.isFromLocalAsset = false,
this.borderRadius,
this.isCircle = true,
this.type = 1})
: super(key: key);
Widget getImageWidget(BuildContext context, TUITheme theme) {
Widget defaultAvatar() {
if (type == 1) {
return Image.asset(
TencentUtils.checkString(
selfInfoViewModel.globalConfig?.defaultAvatarAssetPath) ??
'images/default_c2c_head.png',
fit: BoxFit.cover,
package:
selfInfoViewModel.globalConfig?.defaultAvatarAssetPath != null
? null
: 'tencent_cloud_chat_uikit');
} else {
return Image.asset(
TencentUtils.checkString(
selfInfoViewModel.globalConfig?.defaultAvatarAssetPath) ??
'images/default_group_head.png',
fit: BoxFit.cover,
package:
selfInfoViewModel.globalConfig?.defaultAvatarAssetPath != null
? null
: 'tencent_cloud_chat_uikit');
}
}
// final emptyAvatarBuilder = coreService.emptyAvatarBuilder;
if (faceUrl != "" && faceUrl.isNotEmpty) {
if (isFromLocalAsset) {
return Image.asset(
faceUrl,
fit: BoxFit.cover,
);
}
return CachedNetworkImage(
imageUrl: faceUrl,
fadeInDuration: const Duration(milliseconds: 0),
fit: BoxFit.cover,
errorWidget: (BuildContext context, String c, dynamic s) {
return CachedNetworkImage(
imageUrl:
'https://quanxue-oa.oss-cn-chengdu.aliyuncs.com/avatar.png',
fadeInDuration: const Duration(milliseconds: 0),
fit: BoxFit.cover,
errorWidget: (BuildContext context, String c, dynamic s) {
return defaultAvatar();
},
);
},
);
} else {
// return defaultAvatar();
return CachedNetworkImage(
imageUrl: 'https://quanxue-oa.oss-cn-chengdu.aliyuncs.com/avatar.png',
fadeInDuration: const Duration(milliseconds: 0),
fit: BoxFit.cover,
errorWidget: (BuildContext context, String c, dynamic s) {
return defaultAvatar();
},
);
}
}
ImageProvider getImageProvider() {
ImageProvider defaultAvatar() {
if (type == 1) {
return Image.asset(
TencentUtils.checkString(selfInfoViewModel
.globalConfig?.defaultAvatarAssetPath) ??
'images/default_c2c_head.png',
fit: BoxFit.cover,
package:
selfInfoViewModel.globalConfig?.defaultAvatarAssetPath !=
null
? null
: 'tencent_cloud_chat_uikit')
.image;
} else {
return Image.asset(
TencentUtils.checkString(selfInfoViewModel
.globalConfig?.defaultAvatarAssetPath) ??
'images/default_group_head.png',
fit: BoxFit.cover,
package:
selfInfoViewModel.globalConfig?.defaultAvatarAssetPath !=
null
? null
: 'tencent_cloud_chat_uikit')
.image;
}
}
if (faceUrl != "" && faceUrl.isNotEmpty) {
if (isFromLocalAsset) {
return Image.asset(faceUrl).image;
}
return CachedNetworkImageProvider(
faceUrl,
);
} else {
return CachedNetworkImageProvider(
'https://quanxue-oa.oss-cn-chengdu.aliyuncs.com/avatar.png',
);
}
}
@override
Widget tuiBuild(BuildContext context, TUIKitBuildValue value) {
final TUITheme theme = value.theme;
// 根据isCircle参数决定borderRadius
BorderRadius getAvatarBorderRadius() {
if (isCircle) {
return BorderRadius.circular(200); // 使用大圆角值来实现圆形效果
}
return borderRadius ??
selfInfoViewModel.globalConfig?.defaultAvatarBorderRadius ??
BorderRadius.circular(4.8);
}
return Stack(
fit: StackFit.expand,
clipBehavior: Clip.none,
children: [
if (isShowBigWhenClick)
GestureDetector(
onTap: () {
Navigator.of(context).push(
PageRouteBuilder(
opaque: false, // set to false
pageBuilder: (_, __, ___) => ImageScreen(
imageProvider: getImageProvider(), heroTag: faceUrl),
),
);
},
child: Hero(
tag: faceUrl,
child: ClipRRect(
borderRadius: getAvatarBorderRadius(),
child: getImageWidget(context, theme),
),
),
),
if (!isShowBigWhenClick)
ClipRRect(
borderRadius: getAvatarBorderRadius(),
child: getImageWidget(context, theme),
),
if (onlineStatus?.statusType != null && onlineStatus?.statusType != 0)
Positioned(
bottom: -4,
right: -4,
child: Container(
width: 12,
height: 12,
alignment: Alignment.center,
decoration: BoxDecoration(
shape: BoxShape.circle,
border: Border.all(
color: Colors.white,
width: 2.0,
),
color: onlineStatus?.statusType == 1
? theme.conversationItemOnlineStatusBgColor
: theme.conversationItemOfflineStatusBgColor,
),
child: null,
),
),
],
);
}
}