141 lines
4.3 KiB
Dart
141 lines
4.3 KiB
Dart
import 'package:flutter/material.dart';
|
|
import 'backend.dart';
|
|
import 'chat_screen.dart';
|
|
|
|
class AgentEntry {
|
|
final int id;
|
|
final String name;
|
|
|
|
AgentEntry({required this.id, required this.name});
|
|
}
|
|
|
|
class CreateChatScreen extends StatefulWidget {
|
|
@override
|
|
_CreateChatScreenState createState() => _CreateChatScreenState();
|
|
}
|
|
|
|
class _CreateChatScreenState extends State<CreateChatScreen> {
|
|
final TextEditingController nameController = TextEditingController();
|
|
|
|
// Track selected participants
|
|
final Set<int> selectedParticipants = {};
|
|
|
|
void _submit() async {
|
|
Backend.invalidateCache(chat: true);
|
|
final response = await Backend.sendAction("create-chat", {
|
|
'name': nameController.text,
|
|
'members': selectedParticipants.toList(),
|
|
});
|
|
Navigator.push(
|
|
context,
|
|
MaterialPageRoute(
|
|
builder: (context) => ChatScreen(chatId: response['chatId']),
|
|
),
|
|
);
|
|
}
|
|
|
|
void _toggleParticipant(int id) {
|
|
setState(() {
|
|
if (selectedParticipants.contains(id)) {
|
|
selectedParticipants.remove(id);
|
|
} else {
|
|
selectedParticipants.add(id);
|
|
}
|
|
});
|
|
}
|
|
|
|
// TODO: Agents should only be retrieved once, then the same future is reused in `build`
|
|
static Future<List<AgentEntry>> getAgents() 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'] : '[Chat ${msg['id']}]',
|
|
))
|
|
.toList();
|
|
return chats;
|
|
}
|
|
|
|
Widget _buildParticipantTile(AgentEntry entry) {
|
|
final isSelected = selectedParticipants.contains(entry.id);
|
|
return GestureDetector(
|
|
onTap: () => _toggleParticipant(entry.id),
|
|
child: Container(
|
|
padding: EdgeInsets.symmetric(vertical: 12, horizontal: 16),
|
|
margin: EdgeInsets.symmetric(vertical: 4),
|
|
decoration: BoxDecoration(
|
|
color: isSelected ? Colors.blue.shade100 : Colors.grey.shade200,
|
|
border: Border.all(
|
|
color: isSelected ? Colors.blue : Colors.transparent,
|
|
width: 2,
|
|
),
|
|
borderRadius: BorderRadius.circular(8),
|
|
),
|
|
child: Text(
|
|
entry.name,
|
|
style: TextStyle(
|
|
fontWeight: FontWeight.w600,
|
|
color: isSelected ? Colors.blue.shade900 : Colors.black87,
|
|
),
|
|
),
|
|
),
|
|
);
|
|
}
|
|
|
|
@override
|
|
Widget build(BuildContext context) {
|
|
return Scaffold(
|
|
appBar: AppBar(title: Text('Create Chat')),
|
|
body: Center(
|
|
child: Padding(
|
|
padding: const EdgeInsets.all(16.0),
|
|
child: Column(
|
|
children: [
|
|
TextField(
|
|
controller: nameController,
|
|
decoration: InputDecoration(labelText: 'Title'),
|
|
),
|
|
SizedBox(height: 20),
|
|
Align(
|
|
alignment: Alignment.centerLeft,
|
|
child: Text('Select Participants:',
|
|
style: TextStyle(
|
|
fontSize: 16, fontWeight: FontWeight.bold)),
|
|
),
|
|
SizedBox(height: 8),
|
|
Expanded(
|
|
child: FutureBuilder<List<AgentEntry>>(
|
|
future: getAgents(),
|
|
builder: (context, snapshot) {
|
|
if (snapshot.hasData) {
|
|
final agents = snapshot.data!;
|
|
return ListView.builder(
|
|
itemCount: agents.length,
|
|
itemBuilder: (context, index) {
|
|
return _buildParticipantTile(agents[index]);
|
|
},
|
|
);
|
|
} else {
|
|
return ListView.builder(
|
|
itemCount: 0,
|
|
itemBuilder: (context, index) {
|
|
return ListTile();
|
|
},
|
|
);
|
|
}
|
|
},
|
|
),
|
|
),
|
|
ElevatedButton(
|
|
onPressed: _submit,
|
|
child: Text('Create Chat'),
|
|
),
|
|
],
|
|
),
|
|
),
|
|
),
|
|
);
|
|
}
|
|
}
|