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.
165 lines
5.2 KiB
165 lines
5.2 KiB
import 'package:flutter/material.dart';
|
|
import 'package:flutter_screenutil/flutter_screenutil.dart';
|
|
import 'package:im_flutter_sdk/im_flutter_sdk.dart';
|
|
|
|
import '../../../generated/assets.dart';
|
|
|
|
class VoiceItem extends StatelessWidget {
|
|
final EMVoiceMessageBody voiceBody;
|
|
final bool isSentByMe;
|
|
final bool showTime;
|
|
final String formattedTime;
|
|
|
|
const VoiceItem({
|
|
required this.voiceBody,
|
|
required this.isSentByMe,
|
|
required this.showTime,
|
|
required this.formattedTime,
|
|
super.key,
|
|
});
|
|
|
|
@override
|
|
Widget build(BuildContext context) {
|
|
// 获取语音时长(秒)
|
|
final duration = voiceBody.duration;
|
|
final durationText = '${duration}s';
|
|
|
|
return Column(
|
|
children: [
|
|
// 显示时间
|
|
if (showTime) _buildTimeLabel(),
|
|
Container(
|
|
padding: EdgeInsets.symmetric(horizontal: 16.w, vertical: 8.h),
|
|
child: Row(
|
|
mainAxisAlignment: isSentByMe
|
|
? MainAxisAlignment.end
|
|
: MainAxisAlignment.start,
|
|
crossAxisAlignment: CrossAxisAlignment.start,
|
|
children: [
|
|
if (!isSentByMe) _buildAvatar(),
|
|
if (!isSentByMe) SizedBox(width: 8.w),
|
|
Container(
|
|
margin: EdgeInsets.only(top: 10.h),
|
|
padding: EdgeInsets.symmetric(horizontal: 12.w, vertical: 10.h),
|
|
decoration: BoxDecoration(
|
|
color: isSentByMe ? Color(0xff8E7BF6) : Colors.white,
|
|
borderRadius: BorderRadius.only(
|
|
topLeft: isSentByMe
|
|
? Radius.circular(12.w)
|
|
: Radius.circular(0),
|
|
topRight: isSentByMe
|
|
? Radius.circular(0)
|
|
: Radius.circular(12.w),
|
|
bottomLeft: Radius.circular(12.w),
|
|
bottomRight: Radius.circular(12.w),
|
|
),
|
|
),
|
|
child: Row(
|
|
mainAxisSize: MainAxisSize.min,
|
|
children: [
|
|
// 播放按钮
|
|
GestureDetector(
|
|
onTap: () {
|
|
// TODO: 处理播放/暂停逻辑
|
|
},
|
|
child: Container(
|
|
width: 20.w,
|
|
height: 20.w,
|
|
decoration: BoxDecoration(
|
|
shape: BoxShape.circle,
|
|
color: isSentByMe ? Colors.white : Colors.black,
|
|
),
|
|
child: Icon(
|
|
Icons.play_arrow,
|
|
color: isSentByMe ? Color(0xff8E7BF6) : Colors.white,
|
|
size: 16.w,
|
|
),
|
|
),
|
|
),
|
|
SizedBox(width: 8.w),
|
|
// 时长文本
|
|
Text(
|
|
durationText,
|
|
style: TextStyle(
|
|
fontSize: 14.sp,
|
|
color: isSentByMe ? Colors.white : Colors.black,
|
|
fontWeight: FontWeight.w500,
|
|
),
|
|
),
|
|
SizedBox(width: 8.w),
|
|
// 音频波形
|
|
_buildWaveform(),
|
|
],
|
|
),
|
|
),
|
|
if (isSentByMe) SizedBox(width: 8.w),
|
|
if (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(
|
|
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 = (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: isSentByMe
|
|
? Colors.white.withOpacity(0.8)
|
|
: Colors.grey.withOpacity(0.6),
|
|
borderRadius: BorderRadius.circular(1.w),
|
|
),
|
|
);
|
|
}),
|
|
),
|
|
);
|
|
}
|
|
}
|