You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 

223 lines
7.0 KiB

import 'package:dating_touchme_app/extension/ex_widget.dart';
import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:flutter_smart_dialog/flutter_smart_dialog.dart';
import 'package:get/get.dart';
import 'package:im_flutter_sdk/im_flutter_sdk.dart';
import '../../../generated/assets.dart';
import '../../../controller/message/voice_player_manager.dart';
class VoiceItem extends StatefulWidget {
final EMVoiceMessageBody voiceBody;
final String messageId; // 消息ID,用作音频的唯一标识
final bool isSentByMe;
final bool showTime;
final String formattedTime;
const VoiceItem({
required this.voiceBody,
required this.messageId,
required this.isSentByMe,
required this.showTime,
required this.formattedTime,
super.key,
});
@override
State<VoiceItem> createState() => _VoiceItemState();
}
class _VoiceItemState extends State<VoiceItem> {
final VoicePlayerManager _playerManager = VoicePlayerManager.instance;
@override
void initState() {
super.initState();
// 监听播放状态变化
ever(_playerManager.currentPlayingId, (audioId) {
if (mounted) {
setState(() {});
}
});
ever(_playerManager.playerState, (state) {
if (mounted) {
setState(() {});
}
});
}
// 处理播放/暂停
Future<void> _handlePlayPause() async {
try {
// 获取音频文件路径
String? filePath;
final localPath = widget.voiceBody.localPath;
final remotePath = widget.voiceBody.remotePath;
if (localPath.isNotEmpty) {
filePath = localPath;
} else if (remotePath != null && remotePath.isNotEmpty) {
// 如果是远程路径,需要先下载(这里简化处理,实际应该先下载到本地)
filePath = remotePath;
}
SmartDialog.showToast('来了$remotePath');
if (filePath != null && filePath.isNotEmpty) {
await _playerManager.play(widget.messageId, filePath);
} else {
print('音频文件路径为空');
}
} catch (e) {
print('播放音频失败: $e');
}
}
@override
Widget build(BuildContext context) {
// 获取语音时长(秒)
final duration = widget.voiceBody.duration;
final durationText = '${duration}s';
// 判断当前音频是否正在播放
final isPlaying = _playerManager.isPlaying(widget.messageId);
return Column(
children: [
// 显示时间
if (widget.showTime) _buildTimeLabel(),
Container(
padding: EdgeInsets.symmetric(horizontal: 16.w, vertical: 8.h),
child: Row(
mainAxisAlignment: widget.isSentByMe
? MainAxisAlignment.end
: MainAxisAlignment.start,
crossAxisAlignment: CrossAxisAlignment.start,
children: [
if (!widget.isSentByMe) _buildAvatar(),
if (!widget.isSentByMe) SizedBox(width: 8.w),
Container(
margin: EdgeInsets.only(top: 10.h),
padding: EdgeInsets.symmetric(horizontal: 12.w, vertical: 10.h),
decoration: BoxDecoration(
color: widget.isSentByMe ? Color(0xff8E7BF6) : Colors.white,
borderRadius: BorderRadius.only(
topLeft: widget.isSentByMe
? Radius.circular(12.w)
: Radius.circular(0),
topRight: widget.isSentByMe
? Radius.circular(0)
: Radius.circular(12.w),
bottomLeft: Radius.circular(12.w),
bottomRight: Radius.circular(12.w),
),
),
child: Row(
mainAxisSize: MainAxisSize.min,
children: [
// 播放按钮
Container(
width: 20.w,
height: 20.w,
decoration: BoxDecoration(
shape: BoxShape.circle,
color: widget.isSentByMe
? Colors.white
: Colors.black,
),
child: Icon(
isPlaying ? Icons.pause : Icons.play_arrow,
color: widget.isSentByMe
? Color(0xff8E7BF6)
: Colors.white,
size: 16.w,
),
),
SizedBox(width: 8.w),
// 时长文本
Text(
durationText,
style: TextStyle(
fontSize: 14.sp,
color: widget.isSentByMe ? Colors.white : Colors.black,
fontWeight: FontWeight.w500,
),
),
SizedBox(width: 8.w),
// 音频波形
_buildWaveform(),
],
),
).onTap(() {
_handlePlayPause();
}),
if (widget.isSentByMe) SizedBox(width: 8.w),
if (widget.isSentByMe) _buildAvatar(),
],
),
),
],
);
}
// 构建时间标签
Widget _buildTimeLabel() {
return Container(
alignment: Alignment.center,
padding: EdgeInsets.symmetric(horizontal: 16.w),
child: Container(
padding: EdgeInsets.symmetric(horizontal: 12.w),
child: Text(
widget.formattedTime,
style: TextStyle(fontSize: 12.sp, color: Colors.grey),
),
),
);
}
// 构建头像
Widget _buildAvatar() {
return Container(
width: 40.w,
height: 40.w,
decoration: BoxDecoration(
borderRadius: BorderRadius.circular(20.w),
image: DecorationImage(
image: AssetImage(Assets.imagesAvatarsExample),
fit: BoxFit.cover,
),
),
);
}
// 构建音频波形
Widget _buildWaveform() {
// 根据时长生成波形条数量(最多20个)
final barCount = (widget.voiceBody.duration / 2).ceil().clamp(5, 20);
return SizedBox(
height: 16.h,
child: Row(
mainAxisSize: MainAxisSize.min,
crossAxisAlignment: CrossAxisAlignment.center,
children: List.generate(barCount, (index) {
// 模拟波形高度变化
final random = (index * 7) % 5;
final baseHeight = 6 + random * 2;
final height = (baseHeight.clamp(4, 16)).h;
return Container(
width: 2.w,
height: height,
margin: EdgeInsets.symmetric(horizontal: 1.w),
decoration: BoxDecoration(
color: widget.isSentByMe
? Colors.white.withOpacity(0.8)
: Colors.grey.withOpacity(0.6),
borderRadius: BorderRadius.circular(1.w),
),
);
}),
),
);
}
}