Skip to content

Commit 8d955cd

Browse files
Update FocusManager platform check to include iOS (#148612)
Both iOS and Android run into issues when the FocusManager starts responding to app lifecycle changes. Fortunately, this feature is primarily meant for desktop platforms, so the problem can be resolved with a platform check. fixes flutter/flutter#148475
1 parent 02d5286 commit 8d955cd

File tree

2 files changed

+24
-7
lines changed

2 files changed

+24
-7
lines changed

packages/flutter/lib/src/widgets/focus_manager.dart

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1532,12 +1532,21 @@ class FocusManager with DiagnosticableTreeMixin, ChangeNotifier {
15321532
if (kFlutterMemoryAllocationsEnabled) {
15331533
ChangeNotifier.maybeDispatchObjectCreation(this);
15341534
}
1535-
if (kIsWeb || defaultTargetPlatform != TargetPlatform.android) {
1535+
final bool shouldListenToAppLifecycle = switch (defaultTargetPlatform) {
1536+
TargetPlatform.android || TargetPlatform.iOS => false,
1537+
TargetPlatform.fuchsia || TargetPlatform.linux => true,
1538+
TargetPlatform.windows || TargetPlatform.macOS => true,
1539+
};
1540+
if (shouldListenToAppLifecycle) {
15361541
// It appears that some Android keyboard implementations can cause
15371542
// app lifecycle state changes: adding this listener would cause the
15381543
// text field to unfocus as the user is trying to type.
15391544
//
1540-
// Until this is resolved, we won't be adding the listener to Android apps.
1545+
// Additionally, on iOS, input fields aren't automatically populated
1546+
// with relevant data when using autofill.
1547+
//
1548+
// Until these are resolved, we won't be adding the listener to mobile platforms.
1549+
// https://github.com/flutter/flutter/issues/148475#issuecomment-2118407411
15411550
// https://github.com/flutter/flutter/pull/142930#issuecomment-1981750069
15421551
_appLifecycleListener = _AppLifecycleListener(_appLifecycleChange);
15431552
WidgetsBinding.instance.addObserver(_appLifecycleListener!);

packages/flutter/test/widgets/focus_manager_test.dart

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -354,9 +354,13 @@ void main() {
354354
logs.clear();
355355
}, variant: KeySimulatorTransitModeVariant.all());
356356

357-
testWidgets('FocusManager ignores app lifecycle changes on Android.', (WidgetTester tester) async {
358-
final bool shouldRespond = kIsWeb || defaultTargetPlatform != TargetPlatform.android;
359-
if (shouldRespond) {
357+
testWidgets('FocusManager ignores app lifecycle changes on Android and iOS.', (WidgetTester tester) async {
358+
final bool shouldListenToAppLifecycle = switch (defaultTargetPlatform) {
359+
TargetPlatform.android || TargetPlatform.iOS => false,
360+
TargetPlatform.fuchsia || TargetPlatform.linux => true,
361+
TargetPlatform.windows || TargetPlatform.macOS => true,
362+
};
363+
if (shouldListenToAppLifecycle) {
360364
return;
361365
}
362366

@@ -387,8 +391,12 @@ void main() {
387391
});
388392

389393
testWidgets('FocusManager responds to app lifecycle changes.', (WidgetTester tester) async {
390-
final bool shouldRespond = kIsWeb || defaultTargetPlatform != TargetPlatform.android;
391-
if (!shouldRespond) {
394+
final bool shouldListenToAppLifecycle = switch (defaultTargetPlatform) {
395+
TargetPlatform.android || TargetPlatform.iOS => false,
396+
TargetPlatform.fuchsia || TargetPlatform.linux => true,
397+
TargetPlatform.windows || TargetPlatform.macOS => true,
398+
};
399+
if (!shouldListenToAppLifecycle) {
392400
return;
393401
}
394402

0 commit comments

Comments
 (0)