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

View File

@ -63,6 +63,11 @@ class MessageBubble extends StatelessWidget {
} }
class ChatScreen extends StatefulWidget { class ChatScreen extends StatefulWidget {
String wishlistId;
String wishlistName;
ChatScreen({Key? key, required this.wishlistId, required this.wishlistName}) : super(key: key);
@override @override
State createState() => ChatScreenState(); State createState() => ChatScreenState();
} }
@ -79,8 +84,6 @@ class ChatScreenState extends State<ChatScreen> {
final ScrollController _scrollController = ScrollController(); final ScrollController _scrollController = ScrollController();
late Widget appBarTitle; late Widget appBarTitle;
String wishlistId = '';
void initState() { void initState() {
super.initState(); super.initState();
appBarTitle = Text('New Chat', style: TextStyle(fontSize: 18.0)); appBarTitle = Text('New Chat', style: TextStyle(fontSize: 18.0));
@ -88,7 +91,7 @@ class ChatScreenState extends State<ChatScreen> {
_handleSSEMessage(Message(text: '${event.data}')); _handleSSEMessage(Message(text: '${event.data}'));
}); });
Future.delayed(Duration(milliseconds: 2000)); Future.delayed(Duration(milliseconds: 2000));
if(!wishlistId.isEmpty) if(!widget.wishlistId.isEmpty)
{ {
_loadPreviousMessages(); _loadPreviousMessages();
showButtonsContainer = false; showButtonsContainer = false;
@ -99,8 +102,9 @@ class ChatScreenState extends State<ChatScreen> {
Future<void> _loadPreviousMessages() async { Future<void> _loadPreviousMessages() async {
final pageNumber = 1; final pageNumber = 1;
final pageSize = 200; final pageSize = 200;
appBarTitle = Text(widget.wishlistName, style: TextStyle(fontSize: 18.0));
try { try {
final previousMessages = await _searchService.getMessagesFromPersonalWishlist("6560b4c210686c50ed4b9fec", pageNumber, pageSize); final previousMessages = await _searchService.getMessagesFromPersonalWishlist(widget.wishlistId, pageNumber, pageSize);
final reversedMessages = previousMessages.reversed.toList(); final reversedMessages = previousMessages.reversed.toList();
setState(() { setState(() {
messages.addAll(reversedMessages); messages.addAll(reversedMessages);
@ -156,7 +160,7 @@ class ChatScreenState extends State<ChatScreen> {
showButtonsContainer = false; showButtonsContainer = false;
isWaitingForResponse = true; isWaitingForResponse = true;
}); });
wishlistId = await _searchService.startPersonalWishlist(message); widget.wishlistId = await _searchService.startPersonalWishlist(message);
await _sendMessageToAPI(message); await _sendMessageToAPI(message);
await updateChatTitle(_searchService.wishlistId.toString()); await updateChatTitle(_searchService.wishlistId.toString());
_scrollToBottom(); _scrollToBottom();
@ -183,7 +187,7 @@ class ChatScreenState extends State<ChatScreen> {
void _sendMessage() { void _sendMessage() {
final message = _messageController.text; final message = _messageController.text;
if (wishlistId.isEmpty) { if (widget.wishlistId.isEmpty) {
setState(() { setState(() {
messages.add(Message(text: "What are you looking for?", role: "Application")); messages.add(Message(text: "What are you looking for?", role: "Application"));
messages.add(Message(text: message, role: "User")); 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/models/wishlist.dart';
import 'package:shopping_assistant_mobile_client/network/api_client.dart'; import 'package:shopping_assistant_mobile_client/network/api_client.dart';
import 'chat.dart';
class WishlistsScreen extends StatefulWidget { class WishlistsScreen extends StatefulWidget {
const WishlistsScreen({super.key}); const WishlistsScreen({super.key});
@ -211,16 +213,20 @@ class _WishlistItemState extends State<WishlistItem> {
), ),
Positioned( Positioned(
child: GestureDetector( child: GestureDetector(
onTap: () => print(Navigator.push( onTap: () {
Navigator.push(
context, context,
MaterialPageRoute( MaterialPageRoute(
builder: (context) => builder: (context) => ChatScreen(wishlistId: widget._wishlist.id, wishlistName: widget._wishlist.name),
Text('Chat ' + widget._wishlist.id)))), ),
onHorizontalDragUpdate: (DragUpdateDetails details) => { );
if (details.delta.dx < -1) },
{_transformLeft()} onHorizontalDragUpdate: (DragUpdateDetails details) {
else if (details.delta.dx > 1) if (details.delta.dx < -1) {
{_transformRight()} _transformLeft();
} else if (details.delta.dx > 1) {
_transformRight();
}
}, },
child: AnimatedContainer( child: AnimatedContainer(
transform: Matrix4.translationValues(_xOffset, 0, 0), transform: Matrix4.translationValues(_xOffset, 0, 0),