Merge branch 'develop' into bug/SA-222-back-button-fix

This commit is contained in:
cuqmbr 2023-12-15 22:07:32 +02:00
commit f4a6154b8c
Signed by: cuqmbr
GPG Key ID: 2D72ED98B6CB200F
2 changed files with 68 additions and 23 deletions

View File

@ -31,7 +31,7 @@ class SearchService {
}
bool checkerForSuggestion() {
return type == SearchEventType.product;
return type == SearchEventType.suggestion;
}
String? wishlistId;

View File

@ -78,6 +78,7 @@ class ChatScreenState extends State<ChatScreen> {
SearchService _searchService = SearchService();
List<Message> messages = [];
TextEditingController _messageController = TextEditingController();
List<String> suggestions = [];
bool showBackButton = false;
bool buttonsVisible = true;
bool isSendButtonEnabled = false;
@ -148,19 +149,30 @@ class ChatScreenState extends State<ChatScreen> {
final lastMessage = messages.isNotEmpty ? messages.last : null;
message.isProduct = _searchService.checkerForProduct();
message.isSuggestion = _searchService.checkerForSuggestion();
bool checker = false;
if(message.isSuggestion){
suggestions.add(message.text);
}
logger.d("Product status: ${message.isProduct}");
if (lastMessage != null && lastMessage.role != "User" && message.role != "User") {
logger.d("Suggestion status: ${message.isSuggestion}");
logger.d("Message text: ${message.text}");
if (lastMessage != null && lastMessage.role != "User" && message.role != "User" && !message.isSuggestion) {
String fullMessageText = lastMessage.text + message.text;
fullMessageText = fullMessageText.replaceAll("\\n", "");
logger.d("fullMessageText: $fullMessageText");
final updatedMessage = Message(
text: "${lastMessage.text}${message.text}",
text: fullMessageText,
role: "Application",
isProduct: message.isProduct);
messages.removeLast();
messages.add(updatedMessage);
} else {
messages.add(message);
String messageText = message.text.replaceAll("\\n", "");
if (!message.isSuggestion) {
messages.add(Message(text: messageText, role: message.role, isProduct: message.isProduct, isSuggestion: message.isSuggestion));
}
}
});
setState(() {
isWaitingForResponse = false;
});
@ -223,9 +235,17 @@ class ChatScreenState extends State<ChatScreen> {
}
_messageController.clear();
suggestions.clear();
_scrollToBottom();
}
void _handleSuggestion(String suggestion) {
_messageController.text = suggestion;
_sendMessage();
suggestions.clear();
}
void _scrollToBottom() {
_scrollController.animateTo(
_scrollController.position.maxScrollExtent,
@ -254,6 +274,46 @@ class ChatScreenState extends State<ChatScreen> {
);
}
Widget _generateSuggestionButtons() {
return Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Padding(
padding: EdgeInsets.symmetric(horizontal: 16, vertical: 8),
child: Text(
'Several possible options',
style: TextStyle(fontSize: 16, fontWeight: FontWeight.bold, color: Colors.grey),
),
),
SingleChildScrollView(
scrollDirection: Axis.horizontal,
child: Row(
children: suggestions.map((suggestion) {
return Padding(
padding: EdgeInsets.symmetric(horizontal: 8, vertical: 5),
child: ElevatedButton(
onPressed: () {
_handleSuggestion(suggestion);
},
style: ElevatedButton.styleFrom(
padding: EdgeInsets.symmetric(horizontal: 30, vertical: 16),
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(10),
),
primary: Colors.white,
onPrimary: Colors.blue,
side: BorderSide(color: Colors.blue, width: 2.0),
),
child: Text(suggestion, style: TextStyle(color: Colors.black)),
),
);
}).toList(),
),
),
],
);
}
@override
Widget build(BuildContext context) {
return Scaffold(
@ -389,24 +449,8 @@ class ChatScreenState extends State<ChatScreen> {
color: Colors.blue,
size: 25.0,
),
if (messages.any((message) => message.isSuggestion))
Container(
padding: EdgeInsets.all(8.0),
color: Colors.grey[300],
child: Row(
children: [
Icon(Icons.lightbulb),
SizedBox(width: 8.0),
Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: messages
.where((message) => message.isSuggestion)
.map((message) => Text(message.text))
.toList(),
),
],
),
),
if (suggestions.isNotEmpty)
_generateSuggestionButtons(),
Container(
margin: const EdgeInsets.all(8.0),
child: Row(
@ -426,6 +470,7 @@ class ChatScreenState extends State<ChatScreen> {
contentPadding: EdgeInsets.symmetric(vertical: 20.0),
),
),
),
),
IconButton(