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.

<!-- Links -->
[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
This commit is contained in:
matasb-google 2025-07-01 23:23:47 +02:00 committed by GitHub
parent a0711824a5
commit f2814ddfe6
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 45 additions and 2 deletions

View File

@ -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.

View File

@ -536,7 +536,6 @@ void main() {
},
variant: const TargetPlatformVariant(<TargetPlatform>{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: <Widget>[
SliverList(
delegate: SliverChildBuilderDelegate(
(_, int index) => index == 0 ? const TextField() : const SizedBox(height: 50),
childCount: 200,
addAutomaticKeepAlives: false,
),
),
],
),
),
),
);
final EditableTextState state = tester.state<EditableTextState>(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 {