80 lines
2.6 KiB
Dart
80 lines
2.6 KiB
Dart
import 'package:flutter/material.dart';
|
|
import 'backend.dart';
|
|
import 'chat_screen.dart';
|
|
import 'create_chat_screen.dart';
|
|
|
|
class AgentEntry {
|
|
final int id;
|
|
final String name;
|
|
|
|
AgentEntry({required this.id, required this.name});
|
|
}
|
|
|
|
class AgentListScreen extends StatelessWidget {
|
|
const AgentListScreen({super.key});
|
|
|
|
static Future<List<AgentEntry>> getChats() async {
|
|
final response = await Backend.sendAction("get-agents", {});
|
|
List<dynamic> chatObjects = response['agents'];
|
|
List<AgentEntry> chats = chatObjects
|
|
.map((msg) => AgentEntry(
|
|
id: msg['id'],
|
|
name: msg['name']?.isNotEmpty == true ? msg['name'] : '[Agent ${msg['id']}]',
|
|
))
|
|
.toList();
|
|
return chats;
|
|
}
|
|
|
|
@override
|
|
Widget build(BuildContext context) {
|
|
return Scaffold(
|
|
appBar: AppBar(title: const Text('Agents')),
|
|
body: Column( // Use a Column to stack widgets vertically
|
|
children: [
|
|
ListTile(
|
|
leading: const Icon(Icons.add),
|
|
title: const Text('Create'),
|
|
onTap: () {
|
|
print("TODO: Create new agents");
|
|
},
|
|
),
|
|
const Divider(height: 1),
|
|
Expanded(
|
|
child: FutureBuilder<List<AgentEntry>>(
|
|
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 agents available.'));
|
|
} else {
|
|
final agents = snapshot.data!;
|
|
return ListView.builder(
|
|
itemCount: agents.length,
|
|
itemBuilder: (context, index) {
|
|
final agent = agents[index];
|
|
return ListTile(
|
|
title: Text(agent.name),
|
|
onTap: () {
|
|
print("Navigate to agent ${agent.id}");
|
|
/*Navigator.push(
|
|
context,
|
|
MaterialPageRoute(
|
|
builder: (context) => ChatScreen(agentId: agent.id),
|
|
),
|
|
);*/
|
|
},
|
|
);
|
|
},
|
|
);
|
|
}
|
|
},
|
|
),
|
|
),
|
|
],
|
|
),
|
|
);
|
|
}
|
|
} |