import 'package:dating_touchme_app/extension/ex_widget.dart'; import 'package:extended_text/extended_text.dart'; import 'package:extended_text_field/extended_text_field.dart'; import 'package:flutter/material.dart'; import 'package:flutter/services.dart'; import 'package:flutter_screenutil/flutter_screenutil.dart'; import '../../generated/assets.dart'; import '../../config/emoji_config.dart'; import '../emoji_panel.dart'; import 'more_options_view.dart'; import 'voice_input_view.dart'; class ChatInputBar extends StatefulWidget { final ValueChanged onSendMessage; final ValueChanged>? onImageSelected; final Function(String filePath, int seconds)? onVoiceRecorded; final VoidCallback? onVoiceCall; // 语音通话回调 final Future Function()? onVideoCall; // 视频通话回调 final VoidCallback? onGiftTap; // 礼物按钮回调 const ChatInputBar({ required this.onSendMessage, this.onImageSelected, this.onVoiceRecorded, this.onVoiceCall, this.onVideoCall, this.onGiftTap, super.key, }); @override State createState() => _ChatInputBarState(); } class _ChatInputBarState extends State { final TextEditingController _textController = TextEditingController(); final FocusNode _focusNode = FocusNode(); bool _isMoreOptionsVisible = false; bool _isVoiceVisible = false; bool _isEmojiVisible = false; void _handleSendMessage() { if (_textController.text.isNotEmpty) { widget.onSendMessage(_textController.text); _textController.clear(); } } // 切换更多选项的显示状态 void _toggleMoreOptions() { print('📷 [ChatInputBar] 更多选项(图片)按钮被点击'); setState(() { _isMoreOptionsVisible = !_isMoreOptionsVisible; if (_isMoreOptionsVisible) { _isVoiceVisible = false; _isEmojiVisible = false; } // 收起键盘 FocusManager.instance.primaryFocus?.unfocus(); }); } void _handleImageTap(List imagePaths) { // 将图片路径列表传递给父组件 if (widget.onImageSelected != null) { widget.onImageSelected!(imagePaths); } } void _handleCameraTap(String imagePath) { // 将单个图片路径包装成列表传递给父组件 if (widget.onImageSelected != null) { widget.onImageSelected!([imagePath]); } } void _toggleVoiceOptions() { setState(() { _isVoiceVisible = !_isVoiceVisible; if (_isVoiceVisible) { _isMoreOptionsVisible = false; _isEmojiVisible = false; } FocusManager.instance.primaryFocus?.unfocus(); }); } void _toggleEmojiPanel() { setState(() { _isEmojiVisible = !_isEmojiVisible; if (_isEmojiVisible) { _isMoreOptionsVisible = false; _isVoiceVisible = false; } FocusManager.instance.primaryFocus?.unfocus(); }); } // 关闭所有控制面板 void _closeAllPanels() { if (!mounted) return; if (_isMoreOptionsVisible || _isVoiceVisible || _isEmojiVisible) { setState(() { _isMoreOptionsVisible = false; _isVoiceVisible = false; _isEmojiVisible = false; }); } } @override void initState() { super.initState(); // 监听输入框焦点变化 _focusNode.addListener(() { if (_focusNode.hasFocus && mounted) { // 输入框获得焦点(键盘弹起),关闭所有控制面板 _closeAllPanels(); } }); } @override void dispose() { _focusNode.dispose(); _textController.dispose(); super.dispose(); } void _handleEmojiSelected(EmojiItem emoji) { // 将表情添加到输入框 final currentText = _textController.text; final emojiText = '[emoji:${emoji.id}]'; _textController.text = currentText + emojiText; // 将光标移到末尾 _textController.selection = TextSelection.fromPosition( TextPosition(offset: _textController.text.length), ); setState(() {}); // 刷新显示 } @override Widget build(BuildContext context) { return Column( mainAxisSize: MainAxisSize.min, children: [ Container( padding: EdgeInsets.only(left: 16.w, right: 16.w, bottom: 16.w), color: Colors.white, child: Column( children: [ SizedBox(height: 10.h), Row( children: [ Expanded( child: Container( height: 40.h, decoration: BoxDecoration( color: Color(0xffF5F5F5), borderRadius: BorderRadius.circular(5.h), ), padding: EdgeInsets.symmetric(horizontal: 16.w), child: Stack( children: [ // 真实的输入框 ExtendedTextField( controller: _textController, focusNode: _focusNode, decoration: InputDecoration( border: InputBorder.none, hintText: "请输入聊天内容~", hintStyle: TextStyle( fontSize: 14.sp, color: Colors.grey, ), ), inputFormatters: [ // 可以添加其他格式化器,但不要添加过滤Unicode的规则 FilteringTextInputFormatter.deny(RegExp(r'[\u200B]')), // 仅示例:过滤零宽空格 ], specialTextSpanBuilder: MySpecialTextSpanBuilder(), style: TextStyle( fontSize: 14.sp, color: Colors.black, // 文字始终显示为黑色,specialTextSpanBuilder 会处理表情 ), onChanged: (value) { setState(() {}); // 刷新以更新表情显示 }, ), ], ), ), ), SizedBox(width: 12.w), // 发送按钮 Container( padding: EdgeInsets.symmetric( horizontal: 16.w, vertical: 8.h, ), decoration: BoxDecoration( color: Color.fromRGBO(117, 98, 249, 1), borderRadius: BorderRadius.circular(5.h), ), child: Text( "发送", style: TextStyle( fontSize: 14.sp, color: Colors.white, fontWeight: FontWeight.w500, ), ), ).onTap(_handleSendMessage), ], ), SizedBox(height: 12.h), // 底部工具栏 Row( mainAxisAlignment: MainAxisAlignment.spaceAround, children: [ // 语音消息按钮 Image.asset( Assets.imagesAudio, width: 24.w, height: 24.w, ).onTap(_toggleVoiceOptions), // 语音通话按钮(暂时隐藏) // Image.asset( // Assets.imagesSendCall, // width: 24.w, // height: 24.w, // ).onTap(() { // widget.onVoiceCall?.call(); // }), // 视频通话按钮(暂时隐藏) Image.asset( Assets.imagesVideoCall, width: 24.w, height: 24.w, ).onTap(() async { await widget.onVideoCall?.call(); }), // 礼物按钮 Image.asset(Assets.imagesGift, width: 24.w, height: 24.w).onTap(() { widget.onGiftTap?.call(); }), // 表情按钮 Image.asset(Assets.imagesEmoji, width: 24.w, height: 24.w).onTap(_toggleEmojiPanel), // 更多按钮 Image.asset( Assets.imagesAdd, width: 24.w, height: 24.w, ).onTap(_toggleMoreOptions), ], ), ], ), ), // 更多选项展开视图(支持图片) MoreOptionsView( isVisible: _isMoreOptionsVisible, onImageSelected: _handleImageTap, onCameraSelected: _handleCameraTap, ), // 语音输入展开视图(与 MoreOptionsView 相同的展开方式) VoiceInputView( isVisible: _isVoiceVisible, onVoiceRecorded: widget.onVoiceRecorded, ), // 表情面板 EmojiPanel( isVisible: _isEmojiVisible, onEmojiSelected: _handleEmojiSelected, ), ], ); } } /// 表情特殊文本构建器 - 参考 TIMUIKitTextField 的实现 class MySpecialTextSpanBuilder extends SpecialTextSpanBuilder { @override SpecialText? createSpecialText( String flag, { TextStyle? textStyle, SpecialTextGestureTapCallback? onTap, required int index, }) { if (flag.isEmpty) { return null; } // index 是 start flag 的结束位置,所以文本开始位置应该是 index - (flag.length - 1) // 使用 '[' 作为 startFlag,参考 EmojiText.flag = '[' if (isStart(flag, MyEmojiText.flag)) { return MyEmojiText( textStyle, start: index - (MyEmojiText.flag.length - 1), ); } return null; } } /// 表情特殊文本类 - 参考 TIMUIKitTextField 的 EmojiText 实现 class MyEmojiText extends SpecialText { static const String flag = '['; final int start; MyEmojiText( TextStyle? textStyle, { required this.start, }) : super(MyEmojiText.flag, ']', textStyle); @override InlineSpan finishText() { // toString() 返回完整的匹配文本,例如 "[emoji:11]" final String key = toString(); // 检查是否是我们的表情格式 [emoji:数字] final RegExp emojiPattern = RegExp(r'^\[emoji:(\d+)\]$'); final match = emojiPattern.firstMatch(key); if (match != null && match.groupCount > 0) { final emojiId = match.group(1); if (emojiId != null && emojiId.isNotEmpty) { final emoji = EmojiConfig.getEmojiById(emojiId); if (emoji != null && emoji.path.isNotEmpty) { // 使用 ImageSpan,完全参考 TIMUIKitTextField 的实现 double size = 16; final TextStyle ts = textStyle!; if (ts.fontSize != null) { // 参考 TIMUIKitTextField: size = ts.fontSize! * 1.44 // 但我们使用 flutter_screenutil,所以需要适配 // 如果 fontSize 已经是适配后的值(如 14.sp),则直接计算 size = ts.fontSize! * 1.44; // 将 sp 单位转换为实际像素值,因为 ImageSpan 需要像素值 // flutter_screenutil 的 .sp 会返回像素值,所以这里直接使用 } else { // 如果没有设置 fontSize,使用默认值 14.sp * 1.44 size = 14.w * 1.44; } return ImageSpan( AssetImage(emoji.path), actualText: key, imageWidth: size, imageHeight: size, start: start, margin: const EdgeInsets.all(0), // 零边距避免空格问题 ); } } } // 如果匹配失败或表情不存在,显示原始文本 return TextSpan(text: key, style: textStyle); } }