Skip to content

Commit 328d3a5

Browse files
committed
Take back the reported connection when the platform restores focus
When iOS restores first responder to a text input view whose connection it had reported closed, flutter#191781 now tells the framework, which re-requests focus from TextInputClient.onFocusReceived. The client then attaches a new connection, and that connection carries a new client id. iOS delivers the text picked from an AutoFill sheet such as "Hide My Email" immediately after restoring first responder, using the client id it already holds. That delivery arrives before a newly attached connection exists, so the client id check in TextInput._handleTextInputInvocation discards it and the field stays empty. Take the reported connection back instead of letting a new one attach, so the id the platform is using stays valid and the text is applied. Fixes flutter#157495
1 parent 416d09a commit 328d3a5

3 files changed

Lines changed: 158 additions & 2 deletions

File tree

‎packages/flutter/lib/src/services/text_input.dart‎

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2223,8 +2223,24 @@ class TextInput {
22232223
case 'TextInputClient.onFocusReceived':
22242224
final args = methodCall.arguments as List<dynamic>;
22252225
final clientId = args[0] as int;
2226-
if (_lastConnection != null && _lastConnection!._id == clientId) {
2227-
return _lastConnection!._client.onFocusReceived();
2226+
final TextInputConnection? last = _lastConnection;
2227+
if (last != null && last._id == clientId) {
2228+
// Take the reported connection back rather than leaving the client to
2229+
// attach a new one, so that it keeps the client id the platform is
2230+
// already using. iOS delivers the text picked from an AutoFill sheet
2231+
// such as "Hide My Email" immediately after restoring focus, using
2232+
// that id, and a connection attached later carries a different one.
2233+
// See https://github.com/flutter/flutter/issues/157495.
2234+
final wasDetached = _currentConnection == null;
2235+
if (wasDetached) {
2236+
_currentConnection = last;
2237+
}
2238+
final bool accepted = last._client.onFocusReceived();
2239+
if (!accepted && wasDetached) {
2240+
// The client is gone or refused focus, so leave it detached.
2241+
_currentConnection = null;
2242+
}
2243+
return accepted;
22282244
}
22292245
return false;
22302246
}

‎packages/flutter/lib/src/widgets/editable_text.dart‎

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2584,6 +2584,11 @@ class EditableTextState extends State<EditableText>
25842584
TextInputConnection? _textInputConnection;
25852585
bool get _hasInputConnection => _textInputConnection?.attached ?? false;
25862586

2587+
// The connection the platform last reported closed, kept only so that
2588+
// [onFocusReceived] can take it back if the platform reattaches it. See
2589+
// [connectionClosed].
2590+
TextInputConnection? _closedConnection;
2591+
25872592
TextSelectionOverlay? _selectionOverlay;
25882593
ScrollNotificationObserverState? _scrollNotificationObserver;
25892594
({TextEditingValue value, Rect selectionBounds})? _dataWhenToolbarShowScheduled;
@@ -4168,6 +4173,7 @@ class EditableTextState extends State<EditableText>
41684173
// _needsAutofill changes to false from true, the platform needs to be
41694174
// notified to exclude this field from the autofill context. So we need to
41704175
// provide the autofillId.
4176+
_closedConnection = null;
41714177
_textInputConnection = _needsAutofill && currentAutofillScope != null
41724178
? currentAutofillScope!.attach(this, _effectiveAutofillClient.textInputConfiguration)
41734179
: TextInput.attach(this, _effectiveAutofillClient.textInputConfiguration);
@@ -4254,6 +4260,14 @@ class EditableTextState extends State<EditableText>
42544260
@override
42554261
bool onFocusReceived() {
42564262
if (mounted && !_hasFocus && widget.focusNode.canRequestFocus) {
4263+
// If the platform reattached the connection it had reported as closed,
4264+
// take it back rather than letting _openInputConnection attach a new one,
4265+
// so the client id the platform is using keeps matching this end.
4266+
final TextInputConnection? closed = _closedConnection;
4267+
if (_textInputConnection == null && (closed?.attached ?? false)) {
4268+
_textInputConnection = closed;
4269+
}
4270+
_closedConnection = null;
42574271
widget.focusNode.requestFocus();
42584272
return true;
42594273
}
@@ -4264,6 +4278,7 @@ class EditableTextState extends State<EditableText>
42644278
void connectionClosed() {
42654279
if (_hasInputConnection) {
42664280
_textInputConnection!.connectionClosedReceived();
4281+
_closedConnection = _textInputConnection;
42674282
_textInputConnection = null;
42684283
_lastKnownRemoteTextEditingValue = null;
42694284
widget.focusNode.unfocus();

‎packages/flutter/test/widgets/editable_text_test.dart‎

Lines changed: 125 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2076,6 +2076,131 @@ void main() {
20762076
expect(focusNode.hasFocus, isTrue);
20772077
});
20782078

2079+
// Regression test for https://github.com/flutter/flutter/issues/157495.
2080+
//
2081+
// When iOS restores first responder after an AutoFill sheet such as "Hide My
2082+
// Email" is dismissed, it delivers the text the user picked immediately
2083+
// afterwards, using the client id it already had. Attaching a fresh
2084+
// connection would change that id and the text would be discarded, so the
2085+
// reported connection is taken back instead.
2086+
testWidgets('takes back the reported connection so a late edit still lands', (
2087+
WidgetTester tester,
2088+
) async {
2089+
Future<void> send(String method, List<dynamic> args) async {
2090+
await TestDefaultBinaryMessengerBinding.instance.defaultBinaryMessenger.handlePlatformMessage(
2091+
SystemChannels.textInput.name,
2092+
SystemChannels.textInput.codec.encodeMethodCall(MethodCall(method, args)),
2093+
(_) {},
2094+
);
2095+
}
2096+
2097+
await tester.pumpWidget(
2098+
MediaQuery(
2099+
data: const MediaQueryData(),
2100+
child: Directionality(
2101+
textDirection: TextDirection.ltr,
2102+
child: FocusScope(
2103+
node: focusScopeNode,
2104+
autofocus: true,
2105+
child: EditableText(
2106+
backgroundCursorColor: Colors.grey,
2107+
controller: controller,
2108+
focusNode: focusNode,
2109+
style: textStyle,
2110+
autofocus: true,
2111+
cursorColor: cursorColor,
2112+
autofillHints: const <String>[AutofillHints.email],
2113+
),
2114+
),
2115+
),
2116+
),
2117+
);
2118+
expect(focusNode.hasFocus, isTrue);
2119+
2120+
final clientId =
2121+
(tester.testTextInput.log
2122+
.lastWhere((MethodCall call) => call.method == 'TextInput.setClient')
2123+
.arguments
2124+
as List<dynamic>)[0]
2125+
as int;
2126+
2127+
// The sheet takes first responder; the engine reports the connection closed.
2128+
await send('TextInputClient.onConnectionClosed', <dynamic>[clientId]);
2129+
await tester.pump();
2130+
expect(focusNode.hasFocus, isFalse);
2131+
2132+
// The sheet is dismissed and the platform restores focus to the same client.
2133+
await send('TextInputClient.onFocusReceived', <dynamic>[clientId]);
2134+
await tester.pump();
2135+
expect(focusNode.hasFocus, isTrue);
2136+
2137+
// The picked text arrives under the client id the platform still holds.
2138+
await send('TextInputClient.updateEditingState', <dynamic>[
2139+
clientId,
2140+
const TextEditingValue(
2141+
2142+
selection: TextSelection.collapsed(offset: 17),
2143+
).toJSON(),
2144+
]);
2145+
await tester.pump();
2146+
expect(controller.text, '[email protected]');
2147+
});
2148+
2149+
testWidgets('does not take back a connection for a different client', (
2150+
WidgetTester tester,
2151+
) async {
2152+
Future<void> send(String method, List<dynamic> args) async {
2153+
await TestDefaultBinaryMessengerBinding.instance.defaultBinaryMessenger.handlePlatformMessage(
2154+
SystemChannels.textInput.name,
2155+
SystemChannels.textInput.codec.encodeMethodCall(MethodCall(method, args)),
2156+
(_) {},
2157+
);
2158+
}
2159+
2160+
await tester.pumpWidget(
2161+
MediaQuery(
2162+
data: const MediaQueryData(),
2163+
child: Directionality(
2164+
textDirection: TextDirection.ltr,
2165+
child: FocusScope(
2166+
node: focusScopeNode,
2167+
autofocus: true,
2168+
child: EditableText(
2169+
backgroundCursorColor: Colors.grey,
2170+
controller: controller,
2171+
focusNode: focusNode,
2172+
style: textStyle,
2173+
autofocus: true,
2174+
cursorColor: cursorColor,
2175+
),
2176+
),
2177+
),
2178+
),
2179+
);
2180+
2181+
final clientId =
2182+
(tester.testTextInput.log
2183+
.lastWhere((MethodCall call) => call.method == 'TextInput.setClient')
2184+
.arguments
2185+
as List<dynamic>)[0]
2186+
as int;
2187+
2188+
await send('TextInputClient.onConnectionClosed', <dynamic>[clientId]);
2189+
await tester.pump();
2190+
expect(focusNode.hasFocus, isFalse);
2191+
2192+
await send('TextInputClient.onFocusReceived', <dynamic>[clientId + 99]);
2193+
await tester.pump();
2194+
expect(focusNode.hasFocus, isFalse);
2195+
2196+
await send('TextInputClient.updateEditingState', <dynamic>[
2197+
clientId,
2198+
const TextEditingValue(text: 'discarded').toJSON(),
2199+
]);
2200+
await tester.pump();
2201+
expect(controller.text, isEmpty);
2202+
});
2203+
20792204
testWidgets('can re-acquire focus in Offstage', (WidgetTester tester) async {
20802205
final editableTextKey = GlobalKey<EditableTextState>();
20812206
await tester.pumpWidget(

0 commit comments

Comments
 (0)