64 lines
2.0 KiB
Dart
64 lines
2.0 KiB
Dart
import 'package:flutter/material.dart';
|
|
import 'backend.dart';
|
|
import 'chat_screen.dart';
|
|
|
|
class ChatEntry {
|
|
final int id;
|
|
final String name;
|
|
|
|
ChatEntry({required this.id, required this.name});
|
|
}
|
|
|
|
class ChatListScreen extends StatelessWidget {
|
|
const ChatListScreen({super.key});
|
|
|
|
static Future<List<ChatEntry>> getChats() async {
|
|
final response = await Backend.sendAction("get-chats", {});
|
|
List<dynamic> chatObjects = response['chats'];
|
|
List<ChatEntry> chats = chatObjects
|
|
.map((msg) => ChatEntry(
|
|
id: msg['id'],
|
|
name: msg['name']?.isNotEmpty == true ? msg['name'] : '[Chat ${msg['id']}]',
|
|
))
|
|
.toList();
|
|
return chats;
|
|
}
|
|
|
|
@override
|
|
Widget build(BuildContext context) {
|
|
return Scaffold(
|
|
appBar: AppBar(title: const Text('Chats')),
|
|
body: FutureBuilder<List<ChatEntry>>(
|
|
future: 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),
|
|
),
|
|
);
|
|
},
|
|
);
|
|
},
|
|
);
|
|
}
|
|
},
|
|
),
|
|
);
|
|
}
|
|
} |