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

Commit e429dae

Browse files
committed
[web] Fix done button click not blur in iOS keyboard (#96357)
1 parent 820e05e commit e429dae

File tree

2 files changed

+24
-23
lines changed

2 files changed

+24
-23
lines changed

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

Lines changed: 6 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1453,24 +1453,13 @@ 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.
1467-
subscriptions.add(activeDomElement.onBlur.listen((_) {
1468-
if (windowHasFocus) {
1469-
activeDomElement.focus();
1470-
} else {
1456+
Future<void>.delayed(Duration.zero, () {
1457+
// On iOS, blur is trigerred if the virtual keyboard is closed or the
1458+
// browser is sent to background or the browser tab is changed.
1459+
subscriptions.add(activeDomElement.onBlur.listen((_) {
14711460
owner.sendTextConnectionClosedToFrameworkIfAny();
1472-
}
1473-
}));
1461+
}));
1462+
});
14741463
}
14751464

14761465
@override

lib/web_ui/test/text_editing_test.dart

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -665,12 +665,24 @@ 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+
expect(spy.messages, hasLength(1));
672+
expect(spy.messages[0].channel, 'flutter/textinput');
673+
expect(
674+
spy.messages[0].methodName, 'TextInputClient.onConnectionClosed');
675+
await Future<void>.delayed(Duration.zero);
676+
// DOM element loses the focus.
677+
expect(defaultTextEditingRoot.activeElement, null);
678+
} else {
679+
// No connection close message sent.
680+
expect(spy.messages, hasLength(0));
681+
await Future<void>.delayed(Duration.zero);
682+
// DOM element still keeps the focus.
683+
expect(defaultTextEditingRoot.activeElement,
684+
textEditing!.strategy.domElement);
685+
}
674686
},
675687
// TODO(mdebbar): https://github.com/flutter/flutter/issues/50769
676688
skip: browserEngine == BrowserEngine.edge);

0 commit comments

Comments
 (0)