From b9c889d7d046574fd04ca9435ef888d0d02963e7 Mon Sep 17 00:00:00 2001 From: Bruno Leroux Date: Wed, 23 Oct 2024 17:13:35 +0200 Subject: [PATCH] Reland "Fix TextField selects all content after the application is resumed" (#157399) ## Description Relands https://github.com/flutter/flutter/pull/156968 wich was reverted in https://github.com/flutter/flutter/pull/157378 This PR makes `EditableText` aware of the lifecycle 'resumed' state to let the current selection unchanged when the application is resumed (on web and desktop, 'resumed' means the Flutter app window regained focus). Before this PR, on web and desktop, the whole content of a `TextField` was selected whenever a `TextField` gained focus. This is the correct behavior when tabbing between fields but it is not when a field regains focus after the application is resumed ## Related Issue Fixes [When switching to another browser tab or window and then going back, all text on TextField is selected automatically](https://github.com/flutter/flutter/issues/156078). ## Tests Adds 1 test. --- .../lib/src/widgets/editable_text.dart | 12 +++++- .../test/widgets/editable_text_test.dart | 39 +++++++++++++++++++ 2 files changed, 50 insertions(+), 1 deletion(-) diff --git a/packages/flutter/lib/src/widgets/editable_text.dart b/packages/flutter/lib/src/widgets/editable_text.dart index 957a398c5e..8b03bfedde 100644 --- a/packages/flutter/lib/src/widgets/editable_text.dart +++ b/packages/flutter/lib/src/widgets/editable_text.dart @@ -26,6 +26,7 @@ import 'package:flutter/scheduler.dart'; import 'package:flutter/services.dart'; import 'actions.dart'; +import 'app_lifecycle_listener.dart'; import 'autofill.dart'; import 'automatic_keep_alive.dart'; import 'basic.dart'; @@ -2362,6 +2363,9 @@ class EditableTextState extends State with AutomaticKeepAliveClien Orientation? _lastOrientation; + late final AppLifecycleListener _appLifecycleListener; + bool _justResumed = false; + @override bool get wantKeepAlive => widget.focusNode.hasFocus; @@ -2984,6 +2988,9 @@ class EditableTextState extends State with AutomaticKeepAliveClien widget.focusNode.addListener(_handleFocusChanged); _cursorVisibilityNotifier.value = widget.showCursor; _spellCheckConfiguration = _inferSpellCheckConfiguration(widget.spellCheckConfiguration); + _appLifecycleListener = AppLifecycleListener( + onResume: () => _justResumed = true, + ); _initProcessTextActions(); } @@ -3198,6 +3205,7 @@ class EditableTextState extends State with AutomaticKeepAliveClien clipboardStatus.removeListener(_onChangedClipboardStatus); clipboardStatus.dispose(); _cursorVisibilityNotifier.dispose(); + _appLifecycleListener.dispose(); FocusManager.instance.removeListener(_unflagInternalFocus); _disposeScrollNotificationObserver(); super.dispose(); @@ -4400,7 +4408,9 @@ class EditableTextState extends State with AutomaticKeepAliveClien final bool shouldSelectAll = widget.selectionEnabled && (kIsWeb || isDesktop) && !_isMultiline - && !_nextFocusChangeIsInternal; + && !_nextFocusChangeIsInternal + && !_justResumed; + _justResumed = false; if (shouldSelectAll) { // On native web and desktop platforms, single line tags // select all when receiving focus. diff --git a/packages/flutter/test/widgets/editable_text_test.dart b/packages/flutter/test/widgets/editable_text_test.dart index 90cf086815..7abbadb37f 100644 --- a/packages/flutter/test/widgets/editable_text_test.dart +++ b/packages/flutter/test/widgets/editable_text_test.dart @@ -17017,6 +17017,45 @@ void main() { }, skip: !kIsWeb, // [intended] ); + + // Regression test for https://github.com/flutter/flutter/issues/156078. + testWidgets('when having focus regained after the app resumed', (WidgetTester tester) async { + Future setAppLifeCycleState(AppLifecycleState state) async { + final ByteData? message = const StringCodec().encodeMessage(state.toString()); + await TestDefaultBinaryMessengerBinding.instance.defaultBinaryMessenger + .handlePlatformMessage('flutter/lifecycle', message, (_) {}); + } + + final TextEditingController controller = TextEditingController(text: 'Flutter!'); + addTearDown(controller.dispose); + final FocusNode focusNode = FocusNode(); + addTearDown(focusNode.dispose); + + await tester.pumpWidget( + MaterialApp( + home: Center( + child: EditableText( + key: ValueKey(controller.text), + controller: controller, + focusNode: focusNode, + autofocus: true, + style: Typography.material2018().black.titleMedium!, + cursorColor: Colors.blue, + backgroundCursorColor: Colors.grey, + ), + ), + ), + ); + + expect(focusNode.hasFocus, true); + expect(controller.selection, collapsedAtEnd('Flutter!').selection); + + await setAppLifeCycleState(AppLifecycleState.inactive); + await setAppLifeCycleState(AppLifecycleState.resumed); + + expect(focusNode.hasFocus, true); + expect(controller.selection, collapsedAtEnd('Flutter!').selection); + }, variant: TargetPlatformVariant.all()); }); testWidgets('EditableText respects MediaQuery.boldText', (WidgetTester tester) async {