Skip to content

Commit

Permalink
Update FocusManager platform check to include iOS (flutter#148612)
Browse files Browse the repository at this point in the history
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#148475
  • Loading branch information
nate-thegrate authored May 23, 2024
1 parent 02d5286 commit 8d955cd
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 7 deletions.
13 changes: 11 additions & 2 deletions packages/flutter/lib/src/widgets/focus_manager.dart
Original file line number Diff line number Diff line change
Expand Up @@ -1532,12 +1532,21 @@ class FocusManager with DiagnosticableTreeMixin, ChangeNotifier {
if (kFlutterMemoryAllocationsEnabled) {
ChangeNotifier.maybeDispatchObjectCreation(this);
}
if (kIsWeb || defaultTargetPlatform != TargetPlatform.android) {
final bool shouldListenToAppLifecycle = switch (defaultTargetPlatform) {
TargetPlatform.android || TargetPlatform.iOS => false,
TargetPlatform.fuchsia || TargetPlatform.linux => true,
TargetPlatform.windows || TargetPlatform.macOS => true,
};
if (shouldListenToAppLifecycle) {
// It appears that some Android keyboard implementations can cause
// app lifecycle state changes: adding this listener would cause the
// text field to unfocus as the user is trying to type.
//
// Until this is resolved, we won't be adding the listener to Android apps.
// Additionally, on iOS, input fields aren't automatically populated
// with relevant data when using autofill.
//
// Until these are resolved, we won't be adding the listener to mobile platforms.
// https://github.com/flutter/flutter/issues/148475#issuecomment-2118407411
// https://github.com/flutter/flutter/pull/142930#issuecomment-1981750069
_appLifecycleListener = _AppLifecycleListener(_appLifecycleChange);
WidgetsBinding.instance.addObserver(_appLifecycleListener!);
Expand Down
18 changes: 13 additions & 5 deletions packages/flutter/test/widgets/focus_manager_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -354,9 +354,13 @@ void main() {
logs.clear();
}, variant: KeySimulatorTransitModeVariant.all());

testWidgets('FocusManager ignores app lifecycle changes on Android.', (WidgetTester tester) async {
final bool shouldRespond = kIsWeb || defaultTargetPlatform != TargetPlatform.android;
if (shouldRespond) {
testWidgets('FocusManager ignores app lifecycle changes on Android and iOS.', (WidgetTester tester) async {
final bool shouldListenToAppLifecycle = switch (defaultTargetPlatform) {
TargetPlatform.android || TargetPlatform.iOS => false,
TargetPlatform.fuchsia || TargetPlatform.linux => true,
TargetPlatform.windows || TargetPlatform.macOS => true,
};
if (shouldListenToAppLifecycle) {
return;
}

Expand Down Expand Up @@ -387,8 +391,12 @@ void main() {
});

testWidgets('FocusManager responds to app lifecycle changes.', (WidgetTester tester) async {
final bool shouldRespond = kIsWeb || defaultTargetPlatform != TargetPlatform.android;
if (!shouldRespond) {
final bool shouldListenToAppLifecycle = switch (defaultTargetPlatform) {
TargetPlatform.android || TargetPlatform.iOS => false,
TargetPlatform.fuchsia || TargetPlatform.linux => true,
TargetPlatform.windows || TargetPlatform.macOS => true,
};
if (!shouldListenToAppLifecycle) {
return;
}

Expand Down

0 comments on commit 8d955cd

Please sign in to comment.