From 5d865a36d939f24e6b49751504066b18af0a35bd Mon Sep 17 00:00:00 2001 From: hunteraraujo Date: Thu, 31 Aug 2023 15:42:33 -0700 Subject: [PATCH] Refactor ChatInputField to Use Callback for Sending Messages This commit brings a key update to the ChatInputField widget, making it more flexible and decoupled: - The onSendPressed callback now takes a string parameter. This string represents the message that the user wishes to send. - The onPressed of the send button (IconButton) is now implemented within the ChatInputField widget. It checks if the TextField has any text before calling onSendPressed. - Added a TextEditingController to manage the TextField's content. --- lib/views/chat/chat_input_field.dart | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/lib/views/chat/chat_input_field.dart b/lib/views/chat/chat_input_field.dart index 9082cbb4..560e9069 100644 --- a/lib/views/chat/chat_input_field.dart +++ b/lib/views/chat/chat_input_field.dart @@ -2,7 +2,7 @@ import 'package:flutter/material.dart'; class ChatInputField extends StatefulWidget { // Callback to be triggered when the send button is pressed - final VoidCallback onSendPressed; + final Function(String) onSendPressed; const ChatInputField({ Key? key, @@ -59,7 +59,13 @@ class _ChatInputFieldState extends State { suffixIcon: IconButton( splashRadius: 0.1, icon: const Icon(Icons.send), - onPressed: widget.onSendPressed, + onPressed: () { + // TODO: We allow empty messages? + if (_controller.text.isNotEmpty) { + widget.onSendPressed(_controller.text); + _controller.clear(); + } + }, ), ), ),