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.
This commit is contained in:
hunteraraujo
2023-08-31 15:42:33 -07:00
parent deb84cc804
commit 5d865a36d9

View File

@@ -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<ChatInputField> {
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();
}
},
),
),
),