104 lines
4.5 KiB
Dart
104 lines
4.5 KiB
Dart
import 'package:flutter/material.dart';
|
|
import 'backend.dart';
|
|
import 'chat_screen.dart';
|
|
import 'create_chat_screen.dart';
|
|
|
|
class ChatListScreen extends StatelessWidget {
|
|
const ChatListScreen({super.key});
|
|
|
|
@override
|
|
Widget build(BuildContext context) {
|
|
return Scaffold(
|
|
appBar: AppBar(title: const Text('Chats')),
|
|
body: Column( // Use a Column to stack widgets vertically
|
|
children: [
|
|
ListTile(
|
|
leading: const Icon(Icons.add),
|
|
title: const Text('Create'),
|
|
onTap: () {
|
|
Navigator.push(context, MaterialPageRoute(builder: (context) => CreateChatScreen()));
|
|
},
|
|
),
|
|
const Divider(height: 1),
|
|
Expanded(
|
|
child: FutureBuilder<List<ChatInfo>>(
|
|
future: Backend.getChats(),
|
|
builder: (context, snapshot) {
|
|
if (snapshot.connectionState == ConnectionState.waiting) {
|
|
return const Center(child: CircularProgressIndicator());
|
|
} else if (snapshot.hasError) {
|
|
return Center(child: Text('Error: ${snapshot.error}'));
|
|
} else if (!snapshot.hasData || snapshot.data!.isEmpty) {
|
|
return const Center(child: Text('No chats available.'));
|
|
} else {
|
|
final chats = snapshot.data!;
|
|
return ListView.builder(
|
|
itemCount: chats.length,
|
|
itemBuilder: (context, index) {
|
|
final chat = chats[index];
|
|
return ListTile(
|
|
title: Text(chat.name),
|
|
onTap: () {
|
|
Navigator.push(
|
|
context,
|
|
MaterialPageRoute(
|
|
builder: (context) => ChatScreen(chatId: chat.id),
|
|
),
|
|
);
|
|
},
|
|
trailing: Row(
|
|
mainAxisSize: MainAxisSize.min, // Important to make the row take minimum space
|
|
children: [
|
|
IconButton(
|
|
icon: const Icon(Icons.settings),
|
|
onPressed: () {
|
|
print('Config button pressed for chat: ${chat.name}');
|
|
},
|
|
),
|
|
IconButton(
|
|
icon: const Icon(Icons.delete),
|
|
onPressed: () {
|
|
showDialog(
|
|
context: context,
|
|
builder: (BuildContext context) {
|
|
return AlertDialog(
|
|
title: const Text('Delete Chat'),
|
|
content: Text('Are you sure you want to delete "${chat.name}"?'),
|
|
actions: <Widget>[
|
|
TextButton(
|
|
child: const Text('Cancel'),
|
|
onPressed: () {
|
|
Navigator.of(context).pop();
|
|
},
|
|
),
|
|
TextButton(
|
|
child: const Text('Delete'),
|
|
onPressed: () async {
|
|
await Backend.sendAction('delete-chat', {'chatId': chat.id});
|
|
Backend.invalidateCache(chat: true);
|
|
Navigator.pushReplacement(
|
|
context,
|
|
MaterialPageRoute(builder: (context) => const ChatListScreen()),
|
|
);
|
|
},
|
|
),
|
|
],
|
|
);
|
|
},
|
|
);
|
|
},
|
|
),
|
|
],
|
|
),
|
|
);
|
|
},
|
|
);
|
|
}
|
|
},
|
|
),
|
|
),
|
|
],
|
|
),
|
|
);
|
|
}
|
|
} |