import 'dart:io'; import 'dart:typed_data'; import 'package:adaptive_action_sheet/adaptive_action_sheet.dart'; import 'package:flutter/material.dart'; import 'package:im_api_example/im/friendSelector.dart'; import 'package:im_api_example/im/groupSelector.dart'; import 'package:im_api_example/utils/sdkResponse.dart'; import 'package:image_picker/image_picker.dart'; import 'package:tencent_im_sdk_plugin/enum/message_priority_enum.dart'; import 'package:tencent_im_sdk_plugin/models/v2_tim_message.dart'; import 'package:tencent_im_sdk_plugin/models/v2_tim_msg_create_info_result.dart'; import 'package:tencent_im_sdk_plugin/models/v2_tim_value_callback.dart'; import 'package:tencent_im_sdk_plugin/tencent_im_sdk_plugin.dart'; import 'package:im_api_example/i18n/i18n_utils.dart'; class SendFileMessage extends StatefulWidget { @override State createState() => SendFileMessageState(); } class SendFileMessageState extends State { Map? resData; List receiver = List.empty(growable: true); List groupID = List.empty(growable: true); MessagePriorityEnum priority = MessagePriorityEnum.V2TIM_PRIORITY_DEFAULT; bool onlineUserOnly = false; bool isExcludedFromUnreadCount = false; File? image; String fileName = ''; Uint8List? fileContent; final picker = ImagePicker(); sendFileMessage() async { V2TimValueCallback createMessage = await TencentImSDKPlugin.v2TIMManager .getMessageManager() .createFileMessage( filePath: image!.path, fileName: fileName, ); String id = createMessage.data!.id!; V2TimValueCallback res = await TencentImSDKPlugin.v2TIMManager .getMessageManager() .sendMessage( id: id, receiver: receiver.length > 0 ? receiver.first : "", groupID: groupID.length > 0 ? groupID.first : "", priority: priority, onlineUserOnly: onlineUserOnly, isExcludedFromUnreadCount: isExcludedFromUnreadCount, localCustomData: imt("自定义localCustomData(File)")); setState(() { resData = res.toJson(); }); } Future getImage() async { final pickedFile = await picker.pickImage(source: ImageSource.gallery); final imageContent = await pickedFile!.readAsBytes(); final iamgeName = pickedFile.name; setState(() { image = File(pickedFile.path); fileContent = imageContent; fileName = iamgeName; }); } @override Widget build(BuildContext context) { return Container( child: Column( children: [ Row( children: [ Container( child: ElevatedButton( onPressed: getImage, child: Text(imt("选择文件")), ), margin: EdgeInsets.only(right: 12), ), image == null ? Text(imt(imt("未选择"))) : Text(fileName), ], ), Row( children: [ FriendSelector( onSelect: (data) { setState(() { receiver = data; }); }, switchSelectType: true, value: receiver, ), Expanded( child: Container( margin: EdgeInsets.only(left: 10), child: Text(receiver.length > 0 ? receiver.toString() : imt("未选择")), ), ) ], ), Row( children: [ GroupSelector( onSelect: (data) { setState(() { groupID = data; }); }, switchSelectType: true, value: groupID, ), Expanded( child: Container( margin: EdgeInsets.only(left: 10), child: Text(groupID.length > 0 ? groupID.toString() : imt("未选择")), ), ) ], ), Container( height: 60, decoration: BoxDecoration( border: Border( bottom: BorderSide( color: Colors.black45, ), ), ), child: Row( crossAxisAlignment: CrossAxisAlignment.center, children: [ Container( child: ElevatedButton( onPressed: () { showAdaptiveActionSheet( context: context, title: Text(imt("优先级")), actions: [ BottomSheetAction( title: const Text('V2TIM_PRIORITY_HIGH'), onPressed: () { setState(() { priority = MessagePriorityEnum.V2TIM_PRIORITY_HIGH; }); Navigator.pop(context); }, ), BottomSheetAction( title: const Text('V2TIM_PRIORITY_DEFAULT'), onPressed: () { setState(() { priority = MessagePriorityEnum.V2TIM_PRIORITY_DEFAULT; }); Navigator.pop(context); }, ), BottomSheetAction( title: const Text('V2TIM_PRIORITY_LOW'), onPressed: () { setState(() { priority = MessagePriorityEnum.V2TIM_PRIORITY_LOW; }); Navigator.pop(context); }, ), BottomSheetAction( title: const Text('V2TIM_PRIORITY_NORMAL'), onPressed: () { setState(() { priority = MessagePriorityEnum.V2TIM_PRIORITY_NORMAL; }); Navigator.pop(context); }, ), ], cancelAction: CancelAction( title: const Text('Cancel'), ), // onPressed parameter is optional by default will dismiss the ActionSheet ); }, child: Text(imt("选择优先级")), ), ), Container( margin: EdgeInsets.only(left: 12), child: Text(imt_para("已选:{{priority}}", "已选:$priority")(priority: priority)), ) ], ), ), Row( children: [ Text(imt("是否仅在线用户接受到消息")), Switch( value: onlineUserOnly, onChanged: (res) { setState(() { onlineUserOnly = res; }); }, ) ], ), Row( children: [ Text(imt("发送消息是否不计入未读数")), Switch( value: isExcludedFromUnreadCount, onChanged: (res) { setState(() { isExcludedFromUnreadCount = res; }); }, ) ], ), Row( children: [ Expanded( child: ElevatedButton( onPressed: sendFileMessage, child: Text(imt("发送文件消息")), ), ) ], ), SDKResponse(resData), ], ), ); } }