refactor entire frontend
This commit is contained in:
1 parent
49df72f2a1
commit
7b3326524e
17 files changed
+546
-96
No files matched your search
@@ -0,0 +1,64 @@
|
||||
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),
|
||||
),
|
||||
);
|
||||
},
|
||||
);
|
||||
},
|
||||
);
|
||||
}
|
||||
},
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user