0812
This commit is contained in:
@@ -1,210 +0,0 @@
|
||||
import 'package:flutter/material.dart';
|
||||
import '../widgets/ai_avatar.dart';
|
||||
import '../widgets/chat_bubble.dart';
|
||||
import '../widgets/chat_input.dart';
|
||||
import '../widgets/gradient_background.dart';
|
||||
import '../models/chat_message.dart';
|
||||
//import 'dart:convert';
|
||||
import '../config/database_helper.dart';
|
||||
import '../models/message_model.dart';
|
||||
|
||||
class ChatScreen extends StatefulWidget {
|
||||
const ChatScreen({super.key});
|
||||
|
||||
@override
|
||||
State<ChatScreen> createState() => _ChatScreenState();
|
||||
}
|
||||
|
||||
class _ChatScreenState extends State<ChatScreen> {
|
||||
final List<ChatMessage> _messages = [];
|
||||
int? _currentStreamingMessageIndex;
|
||||
int _lastUserMessageCount = 0; // 添加这个变量跟踪用户消息数量
|
||||
|
||||
final DatabaseHelper _dbHelper = DatabaseHelper();
|
||||
List<Message> _messagesDB = [];
|
||||
|
||||
void _handleSendMessage(String text) {
|
||||
if (text.trim().isEmpty) return;
|
||||
setState(() {
|
||||
_messages.add(ChatMessage(
|
||||
text: text,
|
||||
isUser: true,
|
||||
timestamp: DateTime.now(),
|
||||
));
|
||||
_lastUserMessageCount = _getUserMessageCount(); // 更新用户消息计数
|
||||
_currentStreamingMessageIndex = null; // 重置流索引,强制创建新气泡
|
||||
});
|
||||
}
|
||||
|
||||
// 处理语音识别后的文本
|
||||
void _handleAIResponse(String text) {
|
||||
setState(() {
|
||||
_messages.add(ChatMessage(
|
||||
text: text,
|
||||
isUser: true, // 用户的消息
|
||||
timestamp: DateTime.now(),
|
||||
));
|
||||
_lastUserMessageCount = _getUserMessageCount(); // 更新用户消息计数
|
||||
_currentStreamingMessageIndex = null; // 重置流索引,强制创建新气泡
|
||||
|
||||
_addMessage(Message(
|
||||
role: 'user',
|
||||
message: text,
|
||||
));
|
||||
|
||||
});
|
||||
}
|
||||
|
||||
// 计算用户消息总数
|
||||
int _getUserMessageCount() {
|
||||
return _messages.where((msg) => msg.isUser).length;
|
||||
}
|
||||
|
||||
// 处理 AI 流式响应
|
||||
void _handleAIStreamResponse(String text, bool isComplete) {
|
||||
// 检查是否有新的用户消息,如果有,应该创建新的AI响应
|
||||
final currentUserMessageCount = _getUserMessageCount();
|
||||
final hasNewUserMessage = currentUserMessageCount > _lastUserMessageCount;
|
||||
|
||||
setState(() {
|
||||
if (hasNewUserMessage || _currentStreamingMessageIndex == null) {
|
||||
// 有新用户消息或首次响应,创建新气泡
|
||||
_messages.add(ChatMessage(
|
||||
text: text,
|
||||
isUser: false,
|
||||
timestamp: DateTime.now(),
|
||||
));
|
||||
_currentStreamingMessageIndex = _messages.length - 1;
|
||||
_lastUserMessageCount = currentUserMessageCount; // 更新追踪
|
||||
} else {
|
||||
// 更新现有气泡
|
||||
if (_currentStreamingMessageIndex! < _messages.length) {
|
||||
_messages[_currentStreamingMessageIndex!] = ChatMessage(
|
||||
text: text,
|
||||
isUser: false,
|
||||
timestamp: _messages[_currentStreamingMessageIndex!].timestamp,
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
_addMessage(Message(
|
||||
role: 'assistant',
|
||||
message: text,
|
||||
));
|
||||
|
||||
// 如果完成了,重置索引
|
||||
if (isComplete) {
|
||||
_currentStreamingMessageIndex = null;
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
Future<void> _loadMessages() async {
|
||||
List<Map<String, dynamic>> records = await _dbHelper.getMessages();
|
||||
setState(() {
|
||||
_messagesDB = records.map((record) => Message(
|
||||
role: record['role'],
|
||||
message: record['message'],
|
||||
)).toList();
|
||||
});
|
||||
}
|
||||
|
||||
Future<void> _addMessage(Message message) async {
|
||||
await _dbHelper.insertMessage(message.toMap());
|
||||
}
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return Scaffold(
|
||||
body: GradientBackground(
|
||||
child: SafeArea(
|
||||
child: Column(
|
||||
children: [
|
||||
// Header with AI avatar
|
||||
SizedBox(
|
||||
height: 140,
|
||||
child: Stack(
|
||||
children: [
|
||||
// 背景渐变已由外层GradientBackground提供
|
||||
Row(
|
||||
crossAxisAlignment: CrossAxisAlignment.start,
|
||||
children: [
|
||||
// AI贴图
|
||||
const Padding(
|
||||
padding: EdgeInsets.only(left: 16, top: 0),
|
||||
child: AIAvatar(width: 120, height: 140),
|
||||
),
|
||||
const SizedBox(width: 8),
|
||||
// 文字整体下移
|
||||
Padding(
|
||||
padding: const EdgeInsets.only(top: 48),
|
||||
child: Column(
|
||||
crossAxisAlignment: CrossAxisAlignment.start,
|
||||
children: [
|
||||
const Text(
|
||||
'Hi, 我是众众!',
|
||||
style: TextStyle(
|
||||
fontSize: 22,
|
||||
fontWeight: FontWeight.bold,
|
||||
color: Colors.white,
|
||||
),
|
||||
),
|
||||
const SizedBox(height: 8),
|
||||
Text(
|
||||
'您的专属看、选、买、用车助手',
|
||||
style: TextStyle(
|
||||
fontSize: 14,
|
||||
color: Colors.white.withOpacity(0.8),
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
// 可选:右上角关闭按钮
|
||||
const Spacer(),
|
||||
Padding(
|
||||
padding: const EdgeInsets.only(top: 16, right: 16),
|
||||
child: Icon(Icons.close, color: Colors.white),
|
||||
),
|
||||
],
|
||||
),
|
||||
// 底部分割线
|
||||
Positioned(
|
||||
left: 0,
|
||||
right: 0,
|
||||
bottom: 0,
|
||||
child: Container(
|
||||
height: 1,
|
||||
color: Colors.white.withOpacity(0.15),
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
|
||||
// Chat messages
|
||||
Expanded(
|
||||
child: ListView.builder(
|
||||
padding: const EdgeInsets.symmetric(horizontal: 16),
|
||||
itemCount: _messages.length,
|
||||
reverse: true,
|
||||
itemBuilder: (context, index) {
|
||||
final message = _messages[_messages.length - 1 - index];
|
||||
return ChatBubble(message: message);
|
||||
},
|
||||
),
|
||||
),
|
||||
|
||||
// Input area
|
||||
ChatInput(
|
||||
onSendMessage: _handleSendMessage,
|
||||
onAIResponse: _handleAIResponse,
|
||||
onAIStreamResponse: _handleAIStreamResponse, // 添加新回调
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
88
lib/screens/full_screen.dart
Normal file
88
lib/screens/full_screen.dart
Normal file
@@ -0,0 +1,88 @@
|
||||
import 'package:flutter/material.dart';
|
||||
import '../widgets/chat_box.dart';
|
||||
import '../widgets/gradient_background.dart';
|
||||
import '../services/message_service.dart';
|
||||
import 'package:provider/provider.dart';
|
||||
import '../widgets/chat_header.dart';
|
||||
import '../widgets/chat_footer.dart';
|
||||
|
||||
class FullScreen extends StatefulWidget {
|
||||
const FullScreen({super.key});
|
||||
|
||||
@override
|
||||
State<FullScreen> createState() => _FullScreenState();
|
||||
}
|
||||
|
||||
class _FullScreenState extends State<FullScreen> {
|
||||
final ScrollController _scrollController = ScrollController();
|
||||
|
||||
@override
|
||||
void initState() {
|
||||
super.initState();
|
||||
}
|
||||
|
||||
@override
|
||||
void dispose() {
|
||||
_scrollController.dispose();
|
||||
super.dispose();
|
||||
}
|
||||
|
||||
void _scrollToBottom() {
|
||||
if (_scrollController.hasClients) {
|
||||
_scrollController.animateTo(
|
||||
0.0,
|
||||
duration: const Duration(milliseconds: 200),
|
||||
curve: Curves.easeOut,
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return Scaffold(
|
||||
body: GradientBackground(
|
||||
colors: const [
|
||||
Color(0XFF3B0A3F),
|
||||
Color(0xFF0E0E24),
|
||||
Color(0xFF0C0B33),
|
||||
],
|
||||
child: SafeArea(
|
||||
child: Column(
|
||||
children: [
|
||||
ChatHeader(
|
||||
onClose: () {
|
||||
Provider.of<MessageService>(context, listen: false)
|
||||
.abortReply();
|
||||
Navigator.pop(context);
|
||||
},
|
||||
),
|
||||
const SizedBox(height: 12),
|
||||
Expanded(
|
||||
child: Consumer<MessageService>(
|
||||
builder: (context, handler, child) {
|
||||
WidgetsBinding.instance.addPostFrameCallback((_) {
|
||||
_scrollToBottom();
|
||||
});
|
||||
return ChatBox(
|
||||
scrollController: _scrollController,
|
||||
messages: handler.messages,
|
||||
);
|
||||
},
|
||||
),
|
||||
),
|
||||
const SizedBox(height: 12),
|
||||
Consumer<MessageService>(
|
||||
builder: (context, messageService, child) => ChatFooter(
|
||||
onClear: () {
|
||||
messageService.abortReply();
|
||||
messageService.clearMessages();
|
||||
},
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
41
lib/screens/main_screen.dart
Normal file
41
lib/screens/main_screen.dart
Normal file
@@ -0,0 +1,41 @@
|
||||
import 'package:flutter/material.dart';
|
||||
import '../widgets/floating_icon.dart';
|
||||
|
||||
class MainScreen extends StatefulWidget {
|
||||
const MainScreen({super.key});
|
||||
|
||||
@override
|
||||
State<MainScreen> createState() => _MainScreenState();
|
||||
}
|
||||
|
||||
class _MainScreenState extends State<MainScreen> {
|
||||
@override
|
||||
void initState() {
|
||||
super.initState();
|
||||
}
|
||||
|
||||
@override
|
||||
void dispose() {
|
||||
super.dispose();
|
||||
}
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return Scaffold(
|
||||
backgroundColor: Colors.transparent,
|
||||
body: Stack(
|
||||
children: [
|
||||
Container(
|
||||
decoration: const BoxDecoration(
|
||||
image: DecorationImage(
|
||||
image: AssetImage('assets/images/bg.jpg'),
|
||||
fit: BoxFit.cover,
|
||||
),
|
||||
),
|
||||
),
|
||||
FloatingIcon(),
|
||||
],
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
171
lib/screens/part_screen.dart
Normal file
171
lib/screens/part_screen.dart
Normal file
@@ -0,0 +1,171 @@
|
||||
import 'package:flutter/material.dart';
|
||||
import 'dart:ui';
|
||||
import '../widgets/chat_box.dart';
|
||||
import '../screens/full_screen.dart';
|
||||
import 'package:provider/provider.dart';
|
||||
import '../services/message_service.dart';
|
||||
import '../widgets/gradient_background.dart';
|
||||
|
||||
class PartScreen extends StatefulWidget {
|
||||
final VoidCallback? onHide;
|
||||
|
||||
const PartScreen({super.key, this.onHide});
|
||||
|
||||
@override
|
||||
State<PartScreen> createState() => _PartScreenState();
|
||||
}
|
||||
|
||||
class _PartScreenState extends State<PartScreen> {
|
||||
final ScrollController _scrollController = ScrollController();
|
||||
|
||||
@override
|
||||
void initState() {
|
||||
super.initState();
|
||||
}
|
||||
|
||||
@override
|
||||
void dispose() {
|
||||
_scrollController.dispose();
|
||||
super.dispose();
|
||||
}
|
||||
|
||||
void _scrollToBottom() {
|
||||
if (_scrollController.hasClients) {
|
||||
_scrollController.animateTo(
|
||||
0.0,
|
||||
duration: const Duration(milliseconds: 200),
|
||||
curve: Curves.easeOut,
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
void _openFullScreen() async {
|
||||
widget.onHide?.call();
|
||||
await Navigator.of(context).push(
|
||||
MaterialPageRoute(
|
||||
builder: (context) => const FullScreen(),
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return Scaffold(
|
||||
backgroundColor: Colors.transparent,
|
||||
body: LayoutBuilder(
|
||||
builder: (context, constraints) {
|
||||
final double minHeight = constraints.maxHeight * 0.18;
|
||||
final double maxHeight = constraints.maxHeight * 0.4;
|
||||
final double chatWidth = constraints.maxWidth * 0.94;
|
||||
final int messageCount =
|
||||
context.watch<MessageService>().messages.length;
|
||||
double chatHeight;
|
||||
if (messageCount <= 1) {
|
||||
chatHeight = minHeight;
|
||||
} else {
|
||||
final height = minHeight + (messageCount * 78);
|
||||
chatHeight = height < maxHeight ? height : maxHeight;
|
||||
}
|
||||
return Stack(
|
||||
children: [
|
||||
Positioned.fill(
|
||||
child: IgnorePointer(
|
||||
ignoring: false,
|
||||
child: GestureDetector(
|
||||
behavior: HitTestBehavior.translucent,
|
||||
onTap: () {
|
||||
Provider.of<MessageService>(context, listen: false)
|
||||
.abortReply();
|
||||
if (widget.onHide != null) widget.onHide!();
|
||||
},
|
||||
child: Container(
|
||||
color: Colors.transparent,
|
||||
),
|
||||
),
|
||||
),
|
||||
),
|
||||
Padding(
|
||||
padding: EdgeInsets.only(bottom: constraints.maxHeight * 0.22),
|
||||
child: Align(
|
||||
alignment: Alignment.bottomCenter,
|
||||
child: Selector<MessageService, int>(
|
||||
selector: (_, service) => service.messages.length,
|
||||
builder: (context, messageCount, child) {
|
||||
WidgetsBinding.instance.addPostFrameCallback((_) {
|
||||
_scrollToBottom();
|
||||
});
|
||||
return Consumer<MessageService>(
|
||||
builder: (context, handler, child) {
|
||||
final messages = handler.messages;
|
||||
return AnimatedContainer(
|
||||
duration: const Duration(milliseconds: 180),
|
||||
curve: Curves.easeInOut,
|
||||
width: chatWidth,
|
||||
height: chatHeight,
|
||||
child: ClipRRect(
|
||||
borderRadius: BorderRadius.circular(24),
|
||||
child: BackdropFilter(
|
||||
filter: ImageFilter.blur(sigmaX: 12, sigmaY: 12),
|
||||
child: GradientBackground(
|
||||
colors: const [
|
||||
Color(0XBF3B0A3F),
|
||||
Color(0xBF0E0E24),
|
||||
Color(0xBF0C0B33),
|
||||
],
|
||||
borderRadius: BorderRadius.circular(6),
|
||||
child: Stack(
|
||||
children: [
|
||||
Padding(
|
||||
padding: const EdgeInsets.only(
|
||||
top: 42,
|
||||
left: 6,
|
||||
right: 6,
|
||||
bottom: 6),
|
||||
child: Column(
|
||||
children: [
|
||||
Expanded(
|
||||
child: Padding(
|
||||
padding: EdgeInsets.only(
|
||||
top: 12, bottom: 12),
|
||||
child: ChatBox(
|
||||
scrollController:
|
||||
_scrollController,
|
||||
messages: messages,
|
||||
),
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
Positioned(
|
||||
top: 6,
|
||||
right: 6,
|
||||
child: IconButton(
|
||||
icon: const Icon(
|
||||
Icons.open_in_full,
|
||||
color: Colors.white,
|
||||
size: 24,
|
||||
),
|
||||
onPressed: _openFullScreen,
|
||||
padding: EdgeInsets.zero,
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
),
|
||||
),
|
||||
);
|
||||
},
|
||||
);
|
||||
},
|
||||
),
|
||||
),
|
||||
),
|
||||
],
|
||||
);
|
||||
},
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user