Implement ChatView with Agent and User Message Tiles

This commit introduces the ChatView widget, a central component of the application that manages the chat interface. It's responsible for displaying both agent and user messages and includes the following features:

- Fetching chats for a specific task upon initialization.
- A reverse ListView to display chats with the most recent messages at the bottom.
- Dynamic rendering of UserMessageTile and AgentMessageTile widgets based on message type.
- Integration with the ChatInputField for inputting user messages.
- Placeholder logic for handling send action, to be implemented later.

This implementation lays the groundwork for a fully functional chat interface within the application, allowing for a seamless interaction between users and agents.
This commit is contained in:
hunteraraujo
2023-08-24 21:41:53 -07:00
parent 33c900b73f
commit 0fb67241c5
2 changed files with 63 additions and 18 deletions

View File

@@ -0,0 +1,63 @@
import 'package:auto_gpt_flutter_client/models/message_type.dart';
import 'package:auto_gpt_flutter_client/views/chat/agent_message_tile.dart';
import 'package:auto_gpt_flutter_client/views/chat/chat_input_field.dart';
import 'package:auto_gpt_flutter_client/views/chat/user_message_tile.dart';
import 'package:flutter/material.dart';
import 'package:auto_gpt_flutter_client/viewmodels/chat_viewmodel.dart';
class ChatView extends StatefulWidget {
final ChatViewModel viewModel;
const ChatView({Key? key, required this.viewModel}) : super(key: key);
@override
_ChatViewState createState() => _ChatViewState();
}
class _ChatViewState extends State<ChatView> {
@override
void initState() {
super.initState();
// Schedule the fetchTasks call for after the initial build
WidgetsBinding.instance.addPostFrameCallback((_) {
// TODO: Update to actual task id
widget.viewModel.fetchChatsForTask(1);
});
}
@override
Widget build(BuildContext context) {
return Scaffold(
body: Column(
children: [
// Chat messages list
Expanded(
child: ListView.builder(
reverse: true,
itemCount: widget.viewModel.chats.length,
itemBuilder: (context, index) {
final reversedChats = widget.viewModel.chats.reversed.toList();
final chat = reversedChats[index];
if (chat.messageType == MessageType.user) {
return UserMessageTile(message: chat.message);
} else {
return AgentMessageTile(message: chat.message);
}
},
),
),
// Input area
Padding(
padding: const EdgeInsets.all(8.0),
child: ChatInputField(
onSendPressed: () {
// TODO: Implement passing the message back up
},
),
),
],
),
);
}
}

View File

@@ -1,18 +0,0 @@
import 'package:flutter/material.dart';
class ChatView extends StatelessWidget {
const ChatView({super.key});
@override
Widget build(BuildContext context) {
return Container(
color: Colors.green,
child: const Center(
child: Text(
'Chat',
style: TextStyle(fontSize: 24, color: Colors.white),
),
),
);
}
}