Skip to content
This repository was archived by the owner on Feb 25, 2025. It is now read-only.

Commit b772662

Browse files
JunbinDengjunbindeng
authored andcommitted
[web] Fix done button click not blur in iOS keyboard (#96357)
1 parent f7f0159 commit b772662

File tree

2 files changed

+23
-21
lines changed

2 files changed

+23
-21
lines changed

lib/web_ui/lib/src/engine/text_editing/text_editing.dart

Lines changed: 4 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1453,23 +1453,12 @@ class IOSTextEditingStrategy extends GloballyPositionedTextEditingStrategy {
14531453

14541454
_addTapListener();
14551455

1456-
// On iOS, blur is trigerred in the following cases:
1457-
//
1458-
// 1. The browser app is sent to the background (or the tab is changed). In
1459-
// this case, the window loses focus (see [windowHasFocus]),
1460-
// so we close the input connection with the framework.
1461-
// 2. The user taps on another focusable element. In this case, we refocus
1462-
// the input field and wait for the framework to manage the focus change.
1463-
// 3. The virtual keyboard is closed by tapping "done". We can't detect this
1464-
// programmatically, so we end up refocusing the input field. This is
1465-
// okay because the virtual keyboard will hide, and as soon as the user
1466-
// taps the text field again, the virtual keyboard will come up.
1456+
// On iOS, blur is trigerred if the virtual keyboard is closed or the
1457+
// browser is sent to background or the browser tab is changed.
14671458
subscriptions.add(activeDomElement.onBlur.listen((_) {
1468-
if (windowHasFocus) {
1469-
activeDomElement.focus();
1470-
} else {
1459+
Future<void>.delayed(Duration.zero, () {
14711460
owner.sendTextConnectionClosedToFrameworkIfAny();
1472-
}
1461+
});
14731462
}));
14741463
}
14751464

lib/web_ui/test/text_editing_test.dart

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -665,12 +665,25 @@ void testMain() {
665665
// DOM element is blurred.
666666
textEditing!.strategy.domElement!.blur();
667667

668-
// No connection close message sent.
669-
expect(spy.messages, hasLength(0));
670-
await Future<void>.delayed(Duration.zero);
671-
// DOM element still keeps the focus.
672-
expect(defaultTextEditingRoot.activeElement,
673-
textEditing!.strategy.domElement);
668+
// For ios-safari the connection is closed.
669+
if (browserEngine == BrowserEngine.webkit &&
670+
operatingSystem == OperatingSystem.iOs) {
671+
await Future<void>.delayed(Duration.zero);
672+
expect(spy.messages, hasLength(1));
673+
expect(spy.messages[0].channel, 'flutter/textinput');
674+
expect(
675+
spy.messages[0].methodName, 'TextInputClient.onConnectionClosed');
676+
await Future<void>.delayed(Duration.zero);
677+
// DOM element loses the focus.
678+
expect(defaultTextEditingRoot.activeElement, null);
679+
} else {
680+
// No connection close message sent.
681+
expect(spy.messages, hasLength(0));
682+
await Future<void>.delayed(Duration.zero);
683+
// DOM element still keeps the focus.
684+
expect(defaultTextEditingRoot.activeElement,
685+
textEditing!.strategy.domElement);
686+
}
674687
},
675688
// TODO(mdebbar): https://github.com/flutter/flutter/issues/50769
676689
skip: browserEngine == BrowserEngine.edge);

0 commit comments

Comments
 (0)