Browse Source

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

在输入框有内容时显示发送按钮,否则显示图片选择按钮
newdev_shunjiawei
lxm 1 year ago
parent
commit
5d389d225b
  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 isImageInputActive = false.obs; // 是否处于图片输入模式
final selectedImagePath = Rx<String?>(null); // 当前选择的图片路径 final selectedImagePath = Rx<String?>(null); // 当前选择的图片路径
// 输入状态控制 - 用于动态显示发送按钮
final hasTextInput = false.obs; // 是否有文本输入
// 流事件订阅 // 流事件订阅
StreamSubscription<AgentServiceEvent>? _eventSubscription; StreamSubscription<AgentServiceEvent>? _eventSubscription;
@ -107,6 +110,7 @@ class AgentController extends GetxController {
_subscribeToEvents(); //订阅代理服务事件 _subscribeToEvents(); //订阅代理服务事件
_initMusicStatus(); //初始化音乐状态 _initMusicStatus(); //初始化音乐状态
_setupMediaSessionCallbacks(); //设置媒体会话回调 _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 { Future<void> _loadChatHistory() async {
try { 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), const SizedBox(width: 8),
// 图片选择按钮 // 动态按钮:图片选择按钮 或 发送按钮
Container( Obx(() {
width: 44, final hasText = controller.hasTextInput.value;
height: 44, final hasImage = controller.isImageInputActive.value;
decoration: BoxDecoration( final shouldShowSend = hasText || hasImage;
color: isDarkMode ? Colors.grey[800] : Colors.grey[200],
borderRadius: BorderRadius.circular(22), if (shouldShowSend) {
), // 显示发送按钮
child: PopupMenuButton<String>( return GestureDetector(
icon: Icon( onTap: () => controller.sendMessage(),
Icons.add_photo_alternate_outlined, child: Container(
color: width: 44,
isDarkMode ? Colors.grey[300] : Colors.grey[700], height: 44,
size: 22, decoration: BoxDecoration(
), color: primaryColor,
padding: EdgeInsets.zero, borderRadius: BorderRadius.circular(22),
shape: RoundedRectangleBorder( ),
borderRadius: BorderRadius.circular(12), child: const Icon(
), Icons.send,
onSelected: (value) async { color: Colors.white,
if (value == 'camera') { size: 22,
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', } else {
child: Row( // 显示图片选择按钮
children: [ return Container(
Icon(Icons.photo_library, width: 44,
color: primaryColor, size: 20), height: 44,
const SizedBox(width: 8), decoration: BoxDecoration(
const Text('从相册选择', color: isDarkMode ? Colors.grey[800] : Colors.grey[200],
style: TextStyle(fontSize: 14)), 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