Skip to content

Commit b487219

Browse files
committed
fix: composer inputClearMode and fix custom editing controller
1 parent 36d627e commit b487219

File tree

2 files changed

+32
-8
lines changed

2 files changed

+32
-8
lines changed

packages/flutter_chat_ui/lib/src/composer.dart

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,10 @@ class Composer extends StatefulWidget {
132132
/// and hide the button. Defaults to `false`.
133133
final bool sendButtonHidden;
134134

135+
/// Controls the behavior of the text input field after a message is sent.
136+
/// Defaults to [InputClearMode.always].
137+
final InputClearMode inputClearMode;
138+
135139
/// Creates a message composer widget.
136140
const Composer({
137141
super.key,
@@ -175,6 +179,7 @@ class Composer extends StatefulWidget {
175179
this.allowEmptyMessage = false,
176180
this.sendButtonDisabled = false,
177181
this.sendButtonHidden = false,
182+
this.inputClearMode = InputClearMode.always,
178183
});
179184

180185
@override
@@ -194,6 +199,7 @@ class _ComposerState extends State<Composer> {
194199
_focusNode = widget.focusNode ?? FocusNode();
195200
_hasTextNotifier = ValueNotifier(_textController.text.trim().isNotEmpty);
196201
_focusNode.onKeyEvent = _handleKeyEvent;
202+
_textController.addListener(_handleTextControllerChange);
197203
WidgetsBinding.instance.addPostFrameCallback((_) => _measure());
198204
}
199205

@@ -211,12 +217,18 @@ class _ComposerState extends State<Composer> {
211217
@override
212218
void didUpdateWidget(covariant Composer oldWidget) {
213219
super.didUpdateWidget(oldWidget);
220+
if (widget.textEditingController != oldWidget.textEditingController) {
221+
_textController.removeListener(_handleTextControllerChange);
222+
_textController = widget.textEditingController ?? TextEditingController();
223+
_textController.addListener(_handleTextControllerChange);
224+
}
214225
WidgetsBinding.instance.addPostFrameCallback((_) => _measure());
215226
}
216227

217228
@override
218229
void dispose() {
219230
_hasTextNotifier.dispose();
231+
_textController.removeListener(_handleTextControllerChange);
220232
// Only try to dispose text controller if it's not provided, let
221233
// user handle disposing it how they want.
222234
if (widget.textEditingController == null) {
@@ -392,11 +404,14 @@ class _ComposerState extends State<Composer> {
392404
}
393405
}
394406

407+
void _handleTextControllerChange() {
408+
_hasTextNotifier.value = _textController.text.trim().isNotEmpty;
409+
}
410+
395411
void _handleSubmitted(String text) {
396-
final trimmed = text.trim();
397-
if (trimmed.isNotEmpty || widget.allowEmptyMessage) {
398-
context.read<OnMessageSendCallback?>()?.call(trimmed);
399-
_hasTextNotifier.value = false;
412+
if (widget.allowEmptyMessage == false && text.trim().isEmpty) return;
413+
context.read<OnMessageSendCallback?>()?.call(text.trim());
414+
if (widget.inputClearMode == InputClearMode.always) {
400415
_textController.clear();
401416
}
402417
}

packages/flutter_chat_ui/lib/src/utils/typedefs.dart

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -44,14 +44,23 @@ typedef PaginationCallback = Future<void> Function();
4444
/// Used for customizing insert/remove animations in the chat list.
4545
typedef MessageAnimationDurationResolver = Duration? Function(Message message);
4646

47-
/// Enum to control the visibility of the send button.
47+
/// Used by [Composer] to control the visibility of the send button.
4848
enum SendButtonVisibilityMode {
49-
/// The send button is always visible and enabled.
49+
/// The send button is always visible.
5050
always,
5151

52-
/// The send button is only visible when the input field is not empty.
52+
/// The send button is only visible when the text field is not empty.
5353
hidden,
5454

55-
/// The send button is disabled when the input field is empty. This is the default.
55+
/// The send button is disabled when the text field is empty. Default.
5656
disabled,
5757
}
58+
59+
/// Used by [Composer] to control the clear behavior of the text input.
60+
enum InputClearMode {
61+
/// Always clears the text input after sending a message.
62+
always,
63+
64+
/// Never clears the text input after sending a message.
65+
never,
66+
}

0 commit comments

Comments
 (0)