flutter/lib/utils/notification_banner.dart

163 lines
5.4 KiB
Dart
Raw Normal View History

2025-07-21 15:46:30 +08:00
import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:loopin/IM/im_service.dart';
2025-09-13 17:01:01 +08:00
import 'package:loopin/components/network_or_asset_image.dart';
2025-09-17 15:32:18 +08:00
import 'package:loopin/styles/index.dart';
2025-07-21 15:46:30 +08:00
import 'package:loopin/utils/parse_message_summary.dart';
import 'package:shirne_dialog/shirne_dialog.dart';
2025-09-13 17:01:01 +08:00
import 'package:tencent_cloud_chat_sdk/models/v2_tim_group_info.dart';
2025-07-21 15:46:30 +08:00
import 'package:tencent_cloud_chat_sdk/models/v2_tim_message.dart';
2025-08-21 10:50:38 +08:00
import 'package:tencent_cloud_chat_sdk/models/v2_tim_user_full_info.dart';
2025-07-21 15:46:30 +08:00
class NotificationBanner {
2025-09-13 17:01:01 +08:00
static void show(V2TimMessage msg, bool isGroup) async {
String name = '';
String avatar = '';
if (isGroup) {
final res = await ImService.instance.getGroupsInfo(groupIDList: [msg.groupID!]);
if (res.success && res.data != null) {
V2TimGroupInfo? gpInfo = res.data!.first.groupInfo;
name = gpInfo?.groupName ?? "未知群名";
avatar = gpInfo?.faceUrl ?? "";
} else {
name = '获取群名称失败';
}
} else {
name = msg.nameCard ?? msg.nickName ?? msg.senderProfile?.nickName ?? msg.sender ?? '未知用户';
avatar = msg.faceUrl ?? msg.senderProfile?.faceUrl ?? '';
}
2025-07-21 15:46:30 +08:00
final text = parseMessageSummary(msg);
Get.snackbar(
2025-09-17 15:32:18 +08:00
'',
'',
duration: const Duration(minutes: 1),
2025-07-21 15:46:30 +08:00
margin: const EdgeInsets.all(12),
2025-09-17 15:32:18 +08:00
backgroundColor: FStyle.primaryColor.withAlpha(220),
titleText: Row(
children: [
// 头像
ClipOval(
child: NetworkOrAssetImage(
imageUrl: avatar,
placeholderAsset: isGroup ? 'assets/images/group.png' : 'assets/images/avatar/default.png',
width: 50,
height: 50,
),
),
const SizedBox(width: 8),
// 文本
Expanded(
child: Text(
name,
style: const TextStyle(
color: Colors.white,
fontWeight: FontWeight.bold,
fontSize: 16,
),
overflow: TextOverflow.ellipsis,
maxLines: 1,
),
),
],
),
messageText: Text(
text,
style: const TextStyle(
color: Colors.white,
fontSize: 14,
2025-09-13 17:01:01 +08:00
),
2025-09-17 15:32:18 +08:00
overflow: TextOverflow.ellipsis,
maxLines: 2,
2025-09-13 17:01:01 +08:00
),
2025-07-21 15:46:30 +08:00
onTap: (_) async {
Get.closeCurrentSnackbar();
String? conversationID;
if (msg.groupID != null && msg.groupID!.isNotEmpty) {
conversationID = 'group_${msg.groupID}';
} else if (msg.userID != null && msg.userID!.isNotEmpty) {
conversationID = 'c2c_${msg.userID}';
}
final cRes = await ImService.instance.getConversation(conversationID: conversationID!);
if (cRes.success) {
if (msg.userID != null) {
Get.toNamed('/chat', arguments: cRes.data);
} else if (msg.groupID != null) {
2025-09-13 17:01:01 +08:00
Get.toNamed('/chatGroup', arguments: cRes.data);
2025-07-21 15:46:30 +08:00
}
} else {
2025-09-17 15:32:18 +08:00
MyDialog.toast(
cRes.desc,
icon: const Icon(Icons.warning),
style: ToastStyle(backgroundColor: Colors.red.withAlpha(200)),
);
2025-07-21 15:46:30 +08:00
}
},
);
2025-09-17 15:32:18 +08:00
// Get.snackbar(
// name,
// text,
// duration: Duration(minutes: 1),
// margin: const EdgeInsets.all(12),
// backgroundColor: FStyle.primaryColor,
// colorText: Colors.white,
// icon: ClipOval(
// child: NetworkOrAssetImage(
// imageUrl: avatar,
// placeholderAsset: isGroup ? 'assets/images/group.png' : 'assets/images/avatar/default.png',
// ),
// ),
// onTap: (_) async {
// // 点击后立刻关闭
// Get.closeCurrentSnackbar();
// String? conversationID;
// if (msg.groupID != null && msg.groupID!.isNotEmpty) {
// conversationID = 'group_${msg.groupID}';
// } else if (msg.userID != null && msg.userID!.isNotEmpty) {
// conversationID = 'c2c_${msg.userID}';
// }
// final cRes = await ImService.instance.getConversation(conversationID: conversationID!);
// if (cRes.success) {
// if (msg.userID != null) {
// // 单聊消息
// Get.toNamed('/chat', arguments: cRes.data);
// } else if (msg.groupID != null) {
// Get.toNamed('/chatGroup', arguments: cRes.data);
// }
// } else {
// // 异常
// MyDialog.toast(cRes.desc, icon: Icon(Icons.warning), style: ToastStyle(backgroundColor: Colors.red.withAlpha(200)));
// }
// },
// );
2025-07-21 15:46:30 +08:00
}
2025-08-21 10:50:38 +08:00
/// 被关注通知
static void foucs(V2TimUserFullInfo msg) {
final nickname = msg.nickName ?? '未知用户';
final avatar = msg.faceUrl ?? '';
final text = '$nickname:关注了你';
Get.snackbar(
'新的关注',
text,
duration: const Duration(seconds: 5),
snackPosition: SnackPosition.TOP,
margin: const EdgeInsets.all(12),
backgroundColor: Get.theme.cardColor,
colorText: Get.theme.textTheme.bodyLarge?.color,
icon: avatar.isNotEmpty
? CircleAvatar(
backgroundImage: NetworkImage(avatar),
radius: 16,
)
: null,
onTap: (_) async {
// 点击后立刻关闭
Get.closeCurrentSnackbar();
// 跳转到新关注我的页面
},
);
}
2025-07-21 15:46:30 +08:00
}