Skip to content

Commit

Permalink
[web] Fix inability to type in text fields in iOS (flutter#27563)
Browse files Browse the repository at this point in the history
  • Loading branch information
mdebbar authored Jul 23, 2021
1 parent cee6a89 commit 7591bee
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 23 deletions.
20 changes: 15 additions & 5 deletions lib/web_ui/lib/src/engine/text_editing/text_editing.dart
Original file line number Diff line number Diff line change
Expand Up @@ -1133,13 +1133,23 @@ class IOSTextEditingStrategy extends GloballyPositionedTextEditingStrategy {

_addTapListener();

// On iOS, blur is trigerred if the virtual keyboard is closed or the
// browser is sent to background or the browser tab is changed.
// On iOS, blur is trigerred in the following cases:
//
// Since in all these cases, the connection needs to be closed,
// [domRenderer.windowHasFocus] is not checked in [IOSTextEditingStrategy].
// 1. The browser app is sent to the background (or the tab is changed). In
// this case, the window loses focus (see [domRenderer.windowHasFocus]),
// so we close the input connection with the framework.
// 2. The user taps on another focusable element. In this case, we refocus
// the input field and wait for the framework to manage the focus change.
// 3. The virtual keyboard is closed by tapping "done". We can't detect this
// programmatically, so we end up refocusing the input field. This is
// okay because the virtual keyboard will hide, and as soon as the user
// taps the text field again, the virtual keyboard will come up.
subscriptions.add(activeDomElement.onBlur.listen((_) {
owner.sendTextConnectionClosedToFrameworkIfAny();
if (domRenderer.windowHasFocus) {
activeDomElement.focus();
} else {
owner.sendTextConnectionClosedToFrameworkIfAny();
}
}));
}

Expand Down
24 changes: 6 additions & 18 deletions lib/web_ui/test/text_editing_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -584,24 +584,12 @@ void testMain() {
// DOM element is blurred.
textEditing!.strategy.domElement!.blur();

// For ios-safari the connection is closed.
if (browserEngine == BrowserEngine.webkit &&
operatingSystem == OperatingSystem.iOs) {
expect(spy.messages, hasLength(1));
expect(spy.messages[0].channel, 'flutter/textinput');
expect(
spy.messages[0].methodName, 'TextInputClient.onConnectionClosed');
await Future<void>.delayed(Duration.zero);
// DOM element loses the focus.
expect(defaultTextEditingRoot.activeElement, null);
} else {
// No connection close message sent.
expect(spy.messages, hasLength(0));
await Future<void>.delayed(Duration.zero);
// DOM element still keeps the focus.
expect(defaultTextEditingRoot.activeElement,
textEditing!.strategy.domElement);
}
// No connection close message sent.
expect(spy.messages, hasLength(0));
await Future<void>.delayed(Duration.zero);
// DOM element still keeps the focus.
expect(defaultTextEditingRoot.activeElement,
textEditing!.strategy.domElement);
},
// TODO(nurhan): https://github.com/flutter/flutter/issues/50769
skip: browserEngine == BrowserEngine.edge);
Expand Down

0 comments on commit 7591bee

Please sign in to comment.