fixed a bug with the header and made some changes to display the history correctly

This commit is contained in:
stasex 2023-12-08 15:12:13 +02:00
parent 6fb847780b
commit e866bd5487
3 changed files with 31 additions and 17 deletions

View File

@ -1,5 +1,6 @@
import 'package:flutter/material.dart';
import 'package:flutter_svg/flutter_svg.dart';
import 'package:shopping_assistant_mobile_client/screens/chat.dart';
import 'package:shopping_assistant_mobile_client/screens/wishlists.dart';
void main() {
@ -15,9 +16,9 @@ class MyApp extends StatefulWidget {
'Settings',
];
static const List<Widget> _widgetOptions = <Widget>[
static List<Widget> _widgetOptions = <Widget>[
WishlistsScreen(),
Text(''),
ChatScreen(wishlistId: '', wishlistName: 'New Chat',),
Text(''),
];
@ -37,6 +38,7 @@ class _MyAppState extends State<MyApp> {
});
}
@override
Widget build(BuildContext context) {
return MaterialApp(
@ -51,7 +53,9 @@ class _MyAppState extends State<MyApp> {
),
),
home: Scaffold(
appBar: AppBar(
appBar: _selectedIndex == 1
? null
: AppBar(
title: Text(MyApp._pageNameOptions[_selectedIndex]),
centerTitle: true,
bottom: PreferredSize(

View File

@ -63,6 +63,11 @@ class MessageBubble extends StatelessWidget {
}
class ChatScreen extends StatefulWidget {
String wishlistId;
String wishlistName;
ChatScreen({Key? key, required this.wishlistId, required this.wishlistName}) : super(key: key);
@override
State createState() => ChatScreenState();
}
@ -79,8 +84,6 @@ class ChatScreenState extends State<ChatScreen> {
final ScrollController _scrollController = ScrollController();
late Widget appBarTitle;
String wishlistId = '';
void initState() {
super.initState();
appBarTitle = Text('New Chat', style: TextStyle(fontSize: 18.0));
@ -88,7 +91,7 @@ class ChatScreenState extends State<ChatScreen> {
_handleSSEMessage(Message(text: '${event.data}'));
});
Future.delayed(Duration(milliseconds: 2000));
if(!wishlistId.isEmpty)
if(!widget.wishlistId.isEmpty)
{
_loadPreviousMessages();
showButtonsContainer = false;
@ -99,8 +102,9 @@ class ChatScreenState extends State<ChatScreen> {
Future<void> _loadPreviousMessages() async {
final pageNumber = 1;
final pageSize = 200;
appBarTitle = Text(widget.wishlistName, style: TextStyle(fontSize: 18.0));
try {
final previousMessages = await _searchService.getMessagesFromPersonalWishlist("6560b4c210686c50ed4b9fec", pageNumber, pageSize);
final previousMessages = await _searchService.getMessagesFromPersonalWishlist(widget.wishlistId, pageNumber, pageSize);
final reversedMessages = previousMessages.reversed.toList();
setState(() {
messages.addAll(reversedMessages);
@ -156,7 +160,7 @@ class ChatScreenState extends State<ChatScreen> {
showButtonsContainer = false;
isWaitingForResponse = true;
});
wishlistId = await _searchService.startPersonalWishlist(message);
widget.wishlistId = await _searchService.startPersonalWishlist(message);
await _sendMessageToAPI(message);
await updateChatTitle(_searchService.wishlistId.toString());
_scrollToBottom();
@ -183,7 +187,7 @@ class ChatScreenState extends State<ChatScreen> {
void _sendMessage() {
final message = _messageController.text;
if (wishlistId.isEmpty) {
if (widget.wishlistId.isEmpty) {
setState(() {
messages.add(Message(text: "What are you looking for?", role: "Application"));
messages.add(Message(text: message, role: "User"));

View File

@ -4,6 +4,8 @@ import 'package:graphql/client.dart';
import 'package:shopping_assistant_mobile_client/models/wishlist.dart';
import 'package:shopping_assistant_mobile_client/network/api_client.dart';
import 'chat.dart';
class WishlistsScreen extends StatefulWidget {
const WishlistsScreen({super.key});
@ -211,16 +213,20 @@ class _WishlistItemState extends State<WishlistItem> {
),
Positioned(
child: GestureDetector(
onTap: () => print(Navigator.push(
onTap: () {
Navigator.push(
context,
MaterialPageRoute(
builder: (context) =>
Text('Chat ' + widget._wishlist.id)))),
onHorizontalDragUpdate: (DragUpdateDetails details) => {
if (details.delta.dx < -1)
{_transformLeft()}
else if (details.delta.dx > 1)
{_transformRight()}
builder: (context) => ChatScreen(wishlistId: widget._wishlist.id, wishlistName: widget._wishlist.name),
),
);
},
onHorizontalDragUpdate: (DragUpdateDetails details) {
if (details.delta.dx < -1) {
_transformLeft();
} else if (details.delta.dx > 1) {
_transformRight();
}
},
child: AnimatedContainer(
transform: Matrix4.translationValues(_xOffset, 0, 0),