@@ -2,6 +2,36 @@ import 'package:flutter/material.dart';
22import 'package:provider/provider.dart' ;
33import '../viewModels/chat_view_model.dart' ;
44
5+ /// Returns the new title if user confirms, null if cancelled.
6+ Future <String ?> showRenameDialog (BuildContext context, String currentTitle) async {
7+ final controller = TextEditingController (text: currentTitle);
8+ return showDialog <String >(
9+ context: context,
10+ builder: (context) => AlertDialog (
11+ title: const Text ('Rename chat' ),
12+ content: TextField (
13+ controller: controller,
14+ decoration: const InputDecoration (
15+ labelText: 'Title' ,
16+ border: OutlineInputBorder (),
17+ ),
18+ autofocus: true ,
19+ onSubmitted: (_) => Navigator .of (context).pop (controller.text.trim ()),
20+ ),
21+ actions: [
22+ TextButton (
23+ onPressed: () => Navigator .of (context).pop (),
24+ child: const Text ('Cancel' ),
25+ ),
26+ ElevatedButton (
27+ onPressed: () => Navigator .of (context).pop (controller.text.trim ()),
28+ child: const Text ('Save' ),
29+ ),
30+ ],
31+ ),
32+ );
33+ }
34+
535class ChatList extends StatelessWidget {
636 const ChatList ({Key ? key}) : super (key: key);
737
@@ -46,16 +76,50 @@ class ChatList extends StatelessWidget {
4676 contentPadding: EdgeInsets .only (left: 16 , right: 8 ),
4777 trailing: PopupMenuButton <String >(
4878 padding: EdgeInsets .zero,
49- onSelected: (value) {
79+ onSelected: (value) async {
5080 switch (value) {
51- case 'delete' : chatViewModel.deleteChat (chat);
52- break ;
81+ case 'delete' :
82+ chatViewModel.deleteChat (chat);
83+ break ;
84+ case 'rename' :
85+ final newTitle = await showRenameDialog (context, chat.title);
86+ if (newTitle != null && newTitle.isNotEmpty) {
87+ await chatViewModel.updateChatTitle (chat, newTitle);
88+ }
89+ break ;
5390 }
5491 },
5592 itemBuilder: (context) => [
56- PopupMenuItem (value: 'pin' , child: Text ('Pin' )),
57- PopupMenuItem (value: 'rename' , child: Text ('Rename' )),
58- PopupMenuItem (value: 'delete' , child: Text ('Delete' )),
93+ PopupMenuItem (
94+ value: 'pin' ,
95+ child: Row (
96+ children: const [
97+ Icon (Icons .push_pin, size: 20 ),
98+ SizedBox (width: 8 ),
99+ Text ('Pin' ),
100+ ],
101+ ),
102+ ),
103+ PopupMenuItem (
104+ value: 'rename' ,
105+ child: Row (
106+ children: const [
107+ Icon (Icons .edit, size: 20 ),
108+ SizedBox (width: 8 ),
109+ Text ('Rename' ),
110+ ],
111+ ),
112+ ),
113+ PopupMenuItem (
114+ value: 'delete' ,
115+ child: Row (
116+ children: const [
117+ Icon (Icons .delete, size: 20 ),
118+ SizedBox (width: 8 ),
119+ Text ('Delete' ),
120+ ],
121+ ),
122+ ),
59123 ],
60124 ),
61125 title: Text (
0 commit comments