Skip to content

Commit 28a4968

Browse files
authored
fix(keyboard): improve keyboard hiding logic with focus check
Refactored _hideKeyboard method to use FocusScope.of(context).unfocus() only when there's an active focus that is not the primary focus. This is more precise than calling requestFocus(FocusNode()), which can cause issues like: Attaching a focus node without context may throw runtime exceptions. It introduces unnecessary focus traversal, which may lead to unexpected behavior in complex focus trees. unfocus() is a cleaner, Flutter-recommended way to dismiss the keyboard. This approach safely checks focus state and dismisses the keyboard only when appropriate, avoiding crashes and unwanted side effects.
1 parent e88925e commit 28a4968

File tree

1 file changed

+4
-3
lines changed

1 file changed

+4
-3
lines changed

feedback/lib/src/feedback_widget.dart

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -353,10 +353,11 @@ class FeedbackWidgetState extends State<FeedbackWidget>
353353
}
354354

355355
static void _hideKeyboard(BuildContext context) {
356-
if (kIsWeb) { // No keyboard to hide in web
357-
return;
356+
final currentFocus = FocusScope.of(context);
357+
358+
if (!currentFocus.hasPrimaryFocus && currentFocus.focusedChild != null) {
359+
currentFocus.unfocus();
358360
}
359-
FocusScope.of(context).requestFocus(FocusNode());
360361
}
361362
}
362363

0 commit comments

Comments
 (0)