fix predicate checks only for keyUp events.

Signed-off-by: xsahil03x <[email protected]>
This commit is contained in:
Sahil Kumar
2023-04-07 05:56:41 +05:30
committed by xsahil03x
parent 05604f8e77
commit 69ec1d8935
2 changed files with 24 additions and 20 deletions
@@ -117,10 +117,10 @@ class StreamMessageInput extends StatefulWidget {
}); });
/// The predicate used to send a message on desktop/web /// The predicate used to send a message on desktop/web
final RawKeyEventPredicate? sendMessageKeyPredicate; final KeyEventPredicate sendMessageKeyPredicate;
/// The predicate used to clear the quoted message on desktop/web /// The predicate used to clear the quoted message on desktop/web
final RawKeyEventPredicate? clearQuotedMessageKeyPredicate; final KeyEventPredicate clearQuotedMessageKeyPredicate;
/// If true the message input will animate the actions while you type /// If true the message input will animate the actions while you type
final bool enableActionAnimation; final bool enableActionAnimation;
@@ -264,15 +264,19 @@ class StreamMessageInput extends StatefulWidget {
static bool _defaultSendMessageKeyPredicate( static bool _defaultSendMessageKeyPredicate(
FocusNode node, FocusNode node,
RawKeyEvent event, KeyEvent event,
) => ) {
event.logicalKey == LogicalKeyboardKey.enter; // On desktop/web, send the message when the user presses the enter key.
return event is KeyUpEvent && event.logicalKey == LogicalKeyboardKey.enter;
}
static bool _defaultClearQuotedMessageKeyPredicate( static bool _defaultClearQuotedMessageKeyPredicate(
FocusNode node, FocusNode node,
RawKeyEvent event, KeyEvent event,
) => ) {
event.logicalKey == LogicalKeyboardKey.escape; // On desktop/web, clear the quoted message when the user presses the escape key.
return event is KeyUpEvent && event.logicalKey == LogicalKeyboardKey.escape;
}
@override @override
StreamMessageInputState createState() => StreamMessageInputState(); StreamMessageInputState createState() => StreamMessageInputState();
@@ -773,11 +777,11 @@ class StreamMessageInputState extends State<StreamMessageInput>
maxHeight: widget.maxHeight, maxHeight: widget.maxHeight,
child: PlatformWidgetBuilder( child: PlatformWidgetBuilder(
web: (context, child) => Focus( web: (context, child) => Focus(
onKey: _handleKeyPressed, onKeyEvent: _handleKeyPressed,
child: child!, child: child!,
), ),
desktop: (context, child) => Focus( desktop: (context, child) => Focus(
onKey: _handleKeyPressed, onKeyEvent: _handleKeyPressed,
child: child!, child: child!,
), ),
mobile: (context, child) => child, mobile: (context, child) => child,
@@ -808,22 +812,22 @@ class StreamMessageInputState extends State<StreamMessageInput>
); );
} }
KeyEventResult _handleKeyPressed( KeyEventResult _handleKeyPressed(FocusNode node, KeyEvent event) {
FocusNode node, // Check for send message key.
RawKeyEvent event, if (widget.sendMessageKeyPredicate(node, event)) {
) {
if (widget.sendMessageKeyPredicate != null &&
widget.sendMessageKeyPredicate!(node, event)) {
sendMessage(); sendMessage();
return KeyEventResult.handled; return KeyEventResult.handled;
} else if (widget.clearQuotedMessageKeyPredicate != null && }
widget.clearQuotedMessageKeyPredicate!(node, event)) {
// Check for clear quoted message key.
if (widget.clearQuotedMessageKeyPredicate(node, event)) {
if (_hasQuotedMessage && _effectiveController.text.isEmpty) { if (_hasQuotedMessage && _effectiveController.text.isEmpty) {
widget.onQuotedMessageCleared?.call(); widget.onQuotedMessageCleared?.call();
} }
return KeyEventResult.handled; return KeyEventResult.handled;
} }
// Return ignored to allow other key events to be handled.
return KeyEventResult.ignored; return KeyEventResult.ignored;
} }
@@ -341,9 +341,9 @@ typedef DownloadedPathCallback = void Function(String? path);
typedef UserTapCallback = void Function(User, Widget?); typedef UserTapCallback = void Function(User, Widget?);
/// {@template rawKeyEventPredicate} /// {@template rawKeyEventPredicate}
/// Callback called to react to a raw key event /// Callback called to react to a key event
/// {@endtemplate} /// {@endtemplate}
typedef RawKeyEventPredicate = bool Function(FocusNode, RawKeyEvent); typedef KeyEventPredicate = bool Function(FocusNode, KeyEvent);
/// {@template userItemBuilder} /// {@template userItemBuilder}
/// Builder used to create a custom [ListUserItem] from a [User] /// Builder used to create a custom [ListUserItem] from a [User]