165 lines
4.9 KiB
Dart
165 lines
4.9 KiB
Dart
import 'package:flutter/material.dart';
|
|
import '../models/message.dart';
|
|
import '../services/ai_service.dart';
|
|
|
|
class AIChatScreen extends StatefulWidget {
|
|
@override
|
|
State<AIChatScreen> createState() => _AIChatScreenState();
|
|
}
|
|
|
|
class _AIChatScreenState extends State<AIChatScreen> {
|
|
final List<Message> _messages = [
|
|
Message(
|
|
text: '您好!我是伴享智能助手,有什么可以帮您的吗?',
|
|
isUser: false,
|
|
),
|
|
];
|
|
|
|
final _textController = TextEditingController();
|
|
final _scrollController = ScrollController();
|
|
|
|
void _sendMessage() {
|
|
if (_textController.text.trim().isEmpty) return;
|
|
|
|
final newMessages = AIService.mockChat(_textController.text);
|
|
setState(() {
|
|
_messages.addAll(newMessages);
|
|
});
|
|
|
|
_textController.clear();
|
|
|
|
// 滚动到底部
|
|
Future.delayed(Duration(milliseconds: 100), () {
|
|
_scrollController.animateTo(
|
|
_scrollController.position.maxScrollExtent,
|
|
duration: Duration(milliseconds: 300),
|
|
curve: Curves.easeOut,
|
|
);
|
|
});
|
|
}
|
|
|
|
@override
|
|
Widget build(BuildContext context) {
|
|
return Scaffold(
|
|
appBar: AppBar(
|
|
title: Text('AI智能助手'),
|
|
),
|
|
body: Column(
|
|
children: [
|
|
// 快捷指令
|
|
Container(
|
|
height: 60,
|
|
padding: EdgeInsets.symmetric(horizontal: 16, vertical: 8),
|
|
child: ListView(
|
|
scrollDirection: Axis.horizontal,
|
|
children: [
|
|
_buildQuickButton('附近活动'),
|
|
_buildQuickButton('我要挂号'),
|
|
_buildQuickButton('帮我买菜'),
|
|
],
|
|
),
|
|
),
|
|
Divider(height: 1),
|
|
// 消息列表
|
|
Expanded(
|
|
child: ListView.builder(
|
|
controller: _scrollController,
|
|
padding: EdgeInsets.all(16),
|
|
itemCount: _messages.length,
|
|
itemBuilder: (context, index) {
|
|
final message = _messages[index];
|
|
return _buildMessageBubble(message);
|
|
},
|
|
),
|
|
),
|
|
// 输入框
|
|
Container(
|
|
padding: EdgeInsets.all(8),
|
|
decoration: BoxDecoration(
|
|
color: Colors.white,
|
|
boxShadow: [
|
|
BoxShadow(
|
|
color: Colors.black12,
|
|
blurRadius: 4,
|
|
offset: Offset(0, -2),
|
|
),
|
|
],
|
|
),
|
|
child: Row(
|
|
children: [
|
|
// 语音按钮
|
|
IconButton(
|
|
icon: Icon(Icons.mic, color: Color(0xFF333333)),
|
|
onPressed: () {
|
|
ScaffoldMessenger.of(context).showSnackBar(
|
|
SnackBar(content: Text('语音功能开发中')),
|
|
);
|
|
},
|
|
),
|
|
// 文字输入
|
|
Expanded(
|
|
child: TextField(
|
|
controller: _textController,
|
|
decoration: InputDecoration(
|
|
hintText: '输入您的问题...',
|
|
border: OutlineInputBorder(
|
|
borderRadius: BorderRadius.circular(24),
|
|
),
|
|
contentPadding: EdgeInsets.symmetric(horizontal: 16, vertical: 8),
|
|
),
|
|
onSubmitted: (_) => _sendMessage(),
|
|
),
|
|
),
|
|
SizedBox(width: 8),
|
|
// 发送按钮
|
|
IconButton(
|
|
icon: Icon(Icons.send, color: Color(0xFF333333)),
|
|
onPressed: _sendMessage,
|
|
),
|
|
],
|
|
),
|
|
),
|
|
],
|
|
),
|
|
);
|
|
}
|
|
|
|
Widget _buildQuickButton(String text) {
|
|
return Padding(
|
|
padding: EdgeInsets.only(right: 8),
|
|
child: OutlinedButton(
|
|
onPressed: () {
|
|
_textController.text = text;
|
|
_sendMessage();
|
|
},
|
|
style: OutlinedButton.styleFrom(
|
|
side: BorderSide(color: Color(0xFF333333)),
|
|
),
|
|
child: Text(text, style: TextStyle(color: Color(0xFF333333))),
|
|
),
|
|
);
|
|
}
|
|
|
|
Widget _buildMessageBubble(Message message) {
|
|
return Align(
|
|
alignment: message.isUser ? Alignment.centerRight : Alignment.centerLeft,
|
|
child: Container(
|
|
margin: EdgeInsets.only(bottom: 12),
|
|
padding: EdgeInsets.symmetric(horizontal: 16, vertical: 12),
|
|
constraints: BoxConstraints(maxWidth: MediaQuery.of(context).size.width * 0.7),
|
|
decoration: BoxDecoration(
|
|
color: message.isUser ? Color(0xFF333333) : Color(0xFFF0F0F0),
|
|
borderRadius: BorderRadius.circular(16),
|
|
),
|
|
child: Text(
|
|
message.text,
|
|
style: TextStyle(
|
|
color: message.isUser ? Colors.white : Color(0xFF333333),
|
|
fontSize: 16,
|
|
),
|
|
),
|
|
),
|
|
);
|
|
}
|
|
}
|