From f2814ddfe616e39e56af7778af9be76dc42aebbd Mon Sep 17 00:00:00 2001 From: matasb-google <81666173+matasb-google@users.noreply.github.com> Date: Tue, 1 Jul 2025 23:23:47 +0200 Subject: [PATCH] Fix crash when editableText is not mounted (#171159) Fix issue when text_selection crashes due to editableText not being mounted. No issue filled (internal b/425840577) Likely introduced by https://github.com/flutter/flutter/pull/167881 ## Pre-launch Checklist - [X] I read the [Contributor Guide] and followed the process outlined there for submitting PRs. - [X] I read the [Tree Hygiene] wiki page, which explains my responsibilities. - [X] I read and followed the [Flutter Style Guide], including [Features we expect every widget to implement]. - [X] I signed the [CLA]. - [X] I listed at least one issue that this PR fixes in the description above. - [X] I updated/added relevant documentation (doc comments with `///`). - [X] I added new tests to check the change I am making, or this PR is [test-exempt]. - [X] I followed the [breaking change policy] and added [Data Driven Fixes] where supported. - [X] All existing and new tests are passing. [Contributor Guide]: https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md#overview [Tree Hygiene]: https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md [test-exempt]: https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md#tests [Flutter Style Guide]: https://github.com/flutter/flutter/blob/main/docs/contributing/Style-guide-for-Flutter-repo.md [Features we expect every widget to implement]: https://github.com/flutter/flutter/blob/main/docs/contributing/Style-guide-for-Flutter-repo.md#features-we-expect-every-widget-to-implement [CLA]: https://cla.developers.google.com/ [flutter/tests]: https://github.com/flutter/tests [breaking change policy]: https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md#handling-breaking-changes [Discord]: https://github.com/flutter/flutter/blob/main/docs/contributing/Chat.md [Data Driven Fixes]: https://github.com/flutter/flutter/blob/main/docs/contributing/Data-driven-Fixes.md --- .../lib/src/widgets/text_selection.dart | 3 +- .../test/widgets/text_selection_test.dart | 44 ++++++++++++++++++- 2 files changed, 45 insertions(+), 2 deletions(-) diff --git a/packages/flutter/lib/src/widgets/text_selection.dart b/packages/flutter/lib/src/widgets/text_selection.dart index 79d0e4e2d85..de9c681549b 100644 --- a/packages/flutter/lib/src/widgets/text_selection.dart +++ b/packages/flutter/lib/src/widgets/text_selection.dart @@ -2933,7 +2933,8 @@ class TextSelectionGestureDetectorBuilder { _longPressStartedWithoutFocus = false; _dragStartViewportOffset = 0.0; _dragStartScrollOffset = 0.0; - if (defaultTargetPlatform == TargetPlatform.iOS && + if (_isEditableTextMounted && + defaultTargetPlatform == TargetPlatform.iOS && delegate.selectionEnabled && editableText.textEditingValue.selection.isCollapsed) { // Update the floating cursor. diff --git a/packages/flutter/test/widgets/text_selection_test.dart b/packages/flutter/test/widgets/text_selection_test.dart index c52051803c6..ee995b17207 100644 --- a/packages/flutter/test/widgets/text_selection_test.dart +++ b/packages/flutter/test/widgets/text_selection_test.dart @@ -536,7 +536,6 @@ void main() { }, variant: const TargetPlatformVariant({TargetPlatform.iOS}), ); - testWidgets( 'test TextSelectionGestureDetectorBuilder long press on non-Apple Platforms', (WidgetTester tester) async { @@ -557,6 +556,49 @@ void main() { ), ); + testWidgets( + 'does not crash when long press is cancelled after unmounting', + (WidgetTester tester) async { + // Regression test for b/425840577. + final ScrollController scrollController = ScrollController(); + addTearDown(scrollController.dispose); + + await tester.pumpWidget( + MaterialApp( + home: Material( + child: CustomScrollView( + controller: scrollController, + slivers: [ + SliverList( + delegate: SliverChildBuilderDelegate( + (_, int index) => index == 0 ? const TextField() : const SizedBox(height: 50), + childCount: 200, + addAutomaticKeepAlives: false, + ), + ), + ], + ), + ), + ), + ); + + final EditableTextState state = tester.state(find.byType(EditableText)); + // Start a long press, don't release it, and don't completely reach kLongPressTimeout so the + // gesture is not accepted and is cancelled when the recognizer is disposed. + await tester.startGesture(tester.getCenter(find.byType(TextField))); + await tester.pump(const Duration(milliseconds: 200)); + await tester.pumpAndSettle(); + + // While attempting to long press, scroll the TextField out of view + // to dispose of it and its gesture recognizers. + scrollController.jumpTo(8000.0); + await tester.pump(); + expect(state.mounted, isFalse); + // Should reach the end of the test without any failures. + }, + variant: TargetPlatformVariant.only(TargetPlatform.iOS), + ); + testWidgets( 'TextSelectionGestureDetectorBuilder right click Apple platforms', (WidgetTester tester) async {