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.
107 lines
2.8 KiB
107 lines
2.8 KiB
import 'package:dating_touchme_app/config/emoji_config.dart';
|
|
import 'package:flutter/material.dart';
|
|
import 'package:flutter_screenutil/flutter_screenutil.dart';
|
|
import 'package:get/get.dart';
|
|
|
|
class SendTimelineController extends GetxController {
|
|
final title = "".obs;
|
|
final message = ''.obs;
|
|
final TextEditingController messageController = TextEditingController();
|
|
|
|
final focusNode = FocusNode().obs;
|
|
|
|
final isEmojiVisible = false.obs;
|
|
|
|
@override
|
|
void onInit() {
|
|
super.onInit();
|
|
focusNode.value.addListener(() {
|
|
if (focusNode.value.hasFocus) {
|
|
// 输入框获得焦点(键盘弹起),关闭所有控制面板
|
|
isEmojiVisible.value = false;
|
|
}
|
|
});
|
|
}
|
|
|
|
@override
|
|
void onClose() {
|
|
super.onClose();
|
|
focusNode.value.dispose();
|
|
}
|
|
|
|
|
|
void toggleEmojiPanel() {
|
|
isEmojiVisible.value = !isEmojiVisible.value;
|
|
FocusManager.instance.primaryFocus?.unfocus();
|
|
}
|
|
|
|
void handleEmojiSelected(EmojiItem emoji) {
|
|
// 将表情添加到输入框
|
|
final currentText = messageController.text;
|
|
final emojiText = '[emoji:${emoji.id}]';
|
|
messageController.text = currentText + emojiText;
|
|
// 将光标移到末尾
|
|
messageController.selection = TextSelection.fromPosition(
|
|
TextPosition(offset: messageController.text.length),
|
|
);
|
|
}
|
|
|
|
|
|
/// 构建输入框内容(文本+表情)
|
|
List<Widget> buildInputContentWidgets() {
|
|
final List<Widget> widgets = [];
|
|
final text = messageController.value.text;
|
|
final RegExp emojiRegex = RegExp(r'\[emoji:(\d+)\]');
|
|
|
|
int lastMatchEnd = 0;
|
|
final matches = emojiRegex.allMatches(text);
|
|
|
|
for (final match in matches) {
|
|
// 添加表情之前的文本
|
|
if (match.start > lastMatchEnd) {
|
|
final textPart = text.substring(lastMatchEnd, match.start);
|
|
widgets.add(
|
|
Text(
|
|
textPart,
|
|
style: TextStyle(fontSize: 14.sp, color: Colors.black),
|
|
),
|
|
);
|
|
}
|
|
|
|
// 添加表情图片
|
|
final emojiId = match.group(1);
|
|
if (emojiId != null) {
|
|
final emoji = EmojiConfig.getEmojiById(emojiId);
|
|
if (emoji != null) {
|
|
widgets.add(
|
|
Padding(
|
|
padding: EdgeInsets.symmetric(horizontal: 2.w),
|
|
child: Image.asset(
|
|
emoji.path,
|
|
width: 24.w,
|
|
height: 24.w,
|
|
fit: BoxFit.contain,
|
|
),
|
|
),
|
|
);
|
|
}
|
|
}
|
|
|
|
lastMatchEnd = match.end;
|
|
}
|
|
|
|
// 添加剩余的文本
|
|
if (lastMatchEnd < text.length) {
|
|
final textPart = text.substring(lastMatchEnd);
|
|
widgets.add(
|
|
Text(
|
|
textPart,
|
|
style: TextStyle(fontSize: 14.sp, color: Colors.black),
|
|
),
|
|
);
|
|
}
|
|
|
|
return widgets;
|
|
}
|
|
|
|
}
|