diff --git a/packages/flutter/lib/src/rendering/paragraph.dart b/packages/flutter/lib/src/rendering/paragraph.dart index f36c6fee6f..b125836b63 100644 --- a/packages/flutter/lib/src/rendering/paragraph.dart +++ b/packages/flutter/lib/src/rendering/paragraph.dart @@ -1476,6 +1476,11 @@ class _SelectableFragment with Selectable, ChangeNotifier implements TextLayoutM } final TextRange word = paragraph.getWordBoundary(position); assert(word.isNormalized); + if (word.start < range.start && word.end < range.start) { + return SelectionResult.previous; + } else if (word.start > range.end && word.end > range.end) { + return SelectionResult.next; + } // Fragments are separated by placeholder span, the word boundary shouldn't // expand across fragments. assert(word.start >= range.start && word.end <= range.end); diff --git a/packages/flutter/lib/src/widgets/selectable_region.dart b/packages/flutter/lib/src/widgets/selectable_region.dart index 5aea500ec0..290f5261d7 100644 --- a/packages/flutter/lib/src/widgets/selectable_region.dart +++ b/packages/flutter/lib/src/widgets/selectable_region.dart @@ -1669,12 +1669,21 @@ abstract class MultiSelectableSelectionContainerDelegate extends SelectionContai /// /// Returns positive if a is lower, negative if a is higher. static int _compareHorizontally(Rect a, Rect b) { + // a encloses b. if (a.left - b.left < precisionErrorTolerance && a.right - b.right > - precisionErrorTolerance) { - // a encloses b. + // b ends before a. + if (a.right - b.right > precisionErrorTolerance) { + return 1; + } return -1; } + + // b encloses a. if (b.left - a.left < precisionErrorTolerance && b.right - a.right > - precisionErrorTolerance) { - // b encloses a. + // a ends before b. + if (b.right - a.right > precisionErrorTolerance) { + return -1; + } return 1; } if ((a.left - b.left).abs() > precisionErrorTolerance) { @@ -1888,13 +1897,23 @@ abstract class MultiSelectableSelectionContainerDelegate extends SelectionContai /// [SelectWordSelectionEvent.globalPosition]. @protected SelectionResult handleSelectWord(SelectWordSelectionEvent event) { + SelectionResult? lastSelectionResult; for (int index = 0; index < selectables.length; index += 1) { final Rect localRect = Rect.fromLTWH(0, 0, selectables[index].size.width, selectables[index].size.height); final Matrix4 transform = selectables[index].getTransformTo(null); final Rect globalRect = MatrixUtils.transformRect(transform, localRect); if (globalRect.contains(event.globalPosition)) { final SelectionGeometry existingGeometry = selectables[index].value; - dispatchSelectionEventToChild(selectables[index], event); + lastSelectionResult = dispatchSelectionEventToChild(selectables[index], event); + if (lastSelectionResult == SelectionResult.next) { + continue; + } + if (index == 0 && lastSelectionResult == SelectionResult.previous) { + return SelectionResult.previous; + } + if (index == selectables.length - 1 && lastSelectionResult == SelectionResult.next) { + return SelectionResult.next; + } if (selectables[index].value != existingGeometry) { // Geometry has changed as a result of select word, need to clear the // selection of other selectables to keep selection in sync. @@ -1904,9 +1923,15 @@ abstract class MultiSelectableSelectionContainerDelegate extends SelectionContai currentSelectionStartIndex = currentSelectionEndIndex = index; } return SelectionResult.end; + } else { + if (lastSelectionResult == SelectionResult.next) { + currentSelectionStartIndex = currentSelectionEndIndex = index - 1; + return SelectionResult.end; + } } } - return SelectionResult.none; + assert(lastSelectionResult == null); + return SelectionResult.end; } /// Removes the selection of all selectables this delegate manages. diff --git a/packages/flutter/test/widgets/selectable_region_test.dart b/packages/flutter/test/widgets/selectable_region_test.dart index 21bedecaaf..e9297b5d50 100644 --- a/packages/flutter/test/widgets/selectable_region_test.dart +++ b/packages/flutter/test/widgets/selectable_region_test.dart @@ -769,6 +769,48 @@ void main() { skip: isBrowser, // https://github.com/flutter/flutter/issues/61020 ); + testWidgets( + 'can select word when a selectables rect is completely inside of another selectables rect', (WidgetTester tester) async { + // Regression test for https://github.com/flutter/flutter/issues/127076. + final UniqueKey outerText = UniqueKey(); + await tester.pumpWidget( + MaterialApp( + home: SelectableRegion( + focusNode: FocusNode(), + selectionControls: materialTextSelectionControls, + child: Scaffold( + body: Center( + child: Text.rich( + const TextSpan( + children: [ + TextSpan( + text: + 'Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.', + ), + WidgetSpan(child: Text('Some text in a WidgetSpan. ')), + TextSpan(text: 'Hello, world.'), + ], + ), + key: outerText, + ), + ), + ), + ), + ), + ); + final RenderParagraph paragraph = tester.renderObject(find.descendant(of: find.byKey(outerText), matching: find.byType(RichText)).first); + // Right click to select word at position. + final TestGesture gesture = await tester.startGesture(textOffsetToPosition(paragraph, 125), kind: PointerDeviceKind.mouse, buttons: kSecondaryMouseButton); + addTearDown(gesture.removePointer); + await tester.pump(); + await gesture.up(); + await tester.pump(); + // Should select "Hello". + expect(paragraph.selections[0], const TextSelection(baseOffset: 124, extentOffset: 129)); + }, + skip: isBrowser, // https://github.com/flutter/flutter/issues/61020 + ); + testWidgets( 'widget span is ignored if it does not contain text - non Apple', (WidgetTester tester) async {