From e866bd5487a3186b724dbbacbad2d5e6e5cf2bce Mon Sep 17 00:00:00 2001 From: stasex Date: Fri, 8 Dec 2023 15:12:13 +0200 Subject: [PATCH] fixed a bug with the header and made some changes to display the history correctly --- lib/main.dart | 10 +++++++--- lib/screens/chat.dart | 16 ++++++++++------ lib/screens/wishlists.dart | 22 ++++++++++++++-------- 3 files changed, 31 insertions(+), 17 deletions(-) diff --git a/lib/main.dart b/lib/main.dart index f0ddc6a..1edea67 100644 --- a/lib/main.dart +++ b/lib/main.dart @@ -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 _widgetOptions = [ + static List _widgetOptions = [ WishlistsScreen(), - Text(''), + ChatScreen(wishlistId: '', wishlistName: 'New Chat',), Text(''), ]; @@ -37,6 +38,7 @@ class _MyAppState extends State { }); } + @override Widget build(BuildContext context) { return MaterialApp( @@ -51,7 +53,9 @@ class _MyAppState extends State { ), ), home: Scaffold( - appBar: AppBar( + appBar: _selectedIndex == 1 + ? null + : AppBar( title: Text(MyApp._pageNameOptions[_selectedIndex]), centerTitle: true, bottom: PreferredSize( diff --git a/lib/screens/chat.dart b/lib/screens/chat.dart index b933ccb..a506d5d 100644 --- a/lib/screens/chat.dart +++ b/lib/screens/chat.dart @@ -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 { 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 { _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 { Future _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 { 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 { 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")); diff --git a/lib/screens/wishlists.dart b/lib/screens/wishlists.dart index b8ed8d9..1e48c13 100644 --- a/lib/screens/wishlists.dart +++ b/lib/screens/wishlists.dart @@ -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 { ), 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),