Browse Source

feat(agent): 添加动态发送按钮显示逻辑

在输入框有内容时显示发送按钮,否则显示图片选择按钮
weicu
lxm 1 year ago
parent
commit
3d5d5dd6dc
  1. 12
      lib/modules/agent/controllers/agent_controller.dart
  2. 138
      lib/modules/agent/views/agent_view.dart

12
lib/modules/agent/controllers/agent_controller.dart

@ -68,6 +68,9 @@ class AgentController extends GetxController {
final isImageInputActive = false.obs; // 是否处于图片输入模式
final selectedImagePath = Rx<String?>(null); // 当前选择的图片路径
// 输入状态控制 - 用于动态显示发送按钮
final hasTextInput = false.obs; // 是否有文本输入
// 流事件订阅
StreamSubscription<AgentServiceEvent>? _eventSubscription;
@ -107,6 +110,7 @@ class AgentController extends GetxController {
_subscribeToEvents(); //订阅代理服务事件
_initMusicStatus(); //初始化音乐状态
_setupMediaSessionCallbacks(); //设置媒体会话回调
_setupTextInputListener(); //设置文本输入监听器
}
// 初始化音乐播放状态
@ -178,6 +182,14 @@ class AgentController extends GetxController {
}
}
// 设置文本输入监听器
void _setupTextInputListener() {
textController.addListener(() {
final text = textController.text.trim();
hasTextInput.value = text.isNotEmpty;
});
}
// 加载聊天历史记录
Future<void> _loadChatHistory() async {
try {

138
lib/modules/agent/views/agent_view.dart

@ -390,17 +390,6 @@ class AgentView extends StatelessWidget {
);
}),
),
GestureDetector(
onTap: () => controller.sendMessage(),
child: Container(
padding: const EdgeInsets.all(6),
child: Icon(
Icons.send,
color: primaryColor,
size: 22,
),
),
),
],
),
);
@ -482,60 +471,87 @@ class AgentView extends StatelessWidget {
const SizedBox(width: 8),
// 图片选择按钮
Container(
width: 44,
height: 44,
decoration: BoxDecoration(
color: isDarkMode ? Colors.grey[800] : Colors.grey[200],
borderRadius: BorderRadius.circular(22),
),
child: PopupMenuButton<String>(
icon: Icon(
Icons.add_photo_alternate_outlined,
color:
isDarkMode ? Colors.grey[300] : Colors.grey[700],
size: 22,
),
padding: EdgeInsets.zero,
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(12),
),
onSelected: (value) async {
if (value == 'camera') {
await controller.takePhoto();
} else if (value == 'gallery') {
await controller.pickImage();
}
},
itemBuilder: (context) => [
PopupMenuItem<String>(
value: 'camera',
child: Row(
children: [
Icon(Icons.camera_alt,
color: primaryColor, size: 20),
const SizedBox(width: 8),
const Text('拍照',
style: TextStyle(fontSize: 14)),
],
// 动态按钮:图片选择按钮 或 发送按钮
Obx(() {
final hasText = controller.hasTextInput.value;
final hasImage = controller.isImageInputActive.value;
final shouldShowSend = hasText || hasImage;
if (shouldShowSend) {
// 显示发送按钮
return GestureDetector(
onTap: () => controller.sendMessage(),
child: Container(
width: 44,
height: 44,
decoration: BoxDecoration(
color: primaryColor,
borderRadius: BorderRadius.circular(22),
),
child: const Icon(
Icons.send,
color: Colors.white,
size: 22,
),
),
PopupMenuItem<String>(
value: 'gallery',
child: Row(
children: [
Icon(Icons.photo_library,
color: primaryColor, size: 20),
const SizedBox(width: 8),
const Text('从相册选择',
style: TextStyle(fontSize: 14)),
],
);
} else {
// 显示图片选择按钮
return Container(
width: 44,
height: 44,
decoration: BoxDecoration(
color: isDarkMode ? Colors.grey[800] : Colors.grey[200],
borderRadius: BorderRadius.circular(22),
),
child: PopupMenuButton<String>(
icon: Icon(
Icons.add_photo_alternate_outlined,
color:
isDarkMode ? Colors.grey[300] : Colors.grey[700],
size: 22,
),
padding: EdgeInsets.zero,
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(12),
),
onSelected: (value) async {
if (value == 'camera') {
await controller.takePhoto();
} else if (value == 'gallery') {
await controller.pickImage();
}
},
itemBuilder: (context) => [
PopupMenuItem<String>(
value: 'camera',
child: Row(
children: [
Icon(Icons.camera_alt,
color: primaryColor, size: 20),
const SizedBox(width: 8),
const Text('拍照',
style: TextStyle(fontSize: 14)),
],
),
),
PopupMenuItem<String>(
value: 'gallery',
child: Row(
children: [
Icon(Icons.photo_library,
color: primaryColor, size: 20),
const SizedBox(width: 8),
const Text('从相册选择',
style: TextStyle(fontSize: 14)),
],
),
),
],
),
],
),
),
);
}
}),
],
),
],

Loading…
Cancel
Save