这是indexloc提供的服务,不要输入任何密码
Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions lib/providers/chat_providers.dart
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,16 @@ class ChatSessionsNotifier extends StateNotifier<List<ChatSession>> {
void deleteSession(String sessionId) {
state = state.where((session) => session.id != sessionId).toList();
}

void renameSession(String sessionId, String newTitle) {
state = [
for (final session in state)
if (session.id == sessionId)
session.copyWith(title: newTitle)
else
session,
];
}

ChatSession? getSessionById(String id) {
try {
Expand Down
57 changes: 56 additions & 1 deletion lib/ui/screens/chat_history_screen.dart
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,51 @@ import '../../providers/chat_providers.dart';
class ChatHistoryScreen extends ConsumerWidget {
const ChatHistoryScreen({super.key});

void _showRenameDialog(
BuildContext context, WidgetRef ref, ChatSession session) {
final controller = TextEditingController(text: session.title);
showDialog(
context: context,
builder: (context) {
return AlertDialog(
title: const Text('Rename Chat'),
content: TextField(
controller: controller,
autofocus: true,
decoration: const InputDecoration(labelText: 'Chat name'),
),
actions: [
TextButton(
onPressed: () => Navigator.pop(context),
child: const Text('Cancel'),
),
TextButton(
onPressed: () {
final newName = controller.text.trim();
if (newName.isNotEmpty) {
ref
.read(chatSessionsProvider.notifier)
.renameSession(session.id, newName);
final updated = ref
.read(chatSessionsProvider.notifier)
.getSessionById(session.id);
final active = ref.read(activeChatSessionProvider);
if (updated != null && active?.id == session.id) {
ref
.read(activeChatSessionProvider.notifier)
.setActiveSession(updated);
}
}
Navigator.pop(context);
},
child: const Text('Save'),
),
],
);
},
);
}

@override
Widget build(BuildContext context, WidgetRef ref) {
final sessions = ref.watch(chatSessionsProvider);
Expand All @@ -30,7 +75,17 @@ class ChatHistoryScreen extends ConsumerWidget {
title: Text(session.title),
subtitle: Text('Last updated: '
'${DateFormat.yMd().add_jm().format(session.updatedAt)}'),
trailing: isActive ? const Icon(Icons.check) : null,
trailing: Row(
mainAxisSize: MainAxisSize.min,
children: [
if (isActive) const Icon(Icons.check),
IconButton(
icon: const Icon(Icons.edit),
tooltip: 'Rename',
onPressed: () => _showRenameDialog(context, ref, session),
),
],
),
onTap: () {
ref
.read(activeChatSessionProvider.notifier)
Expand Down