Skip to content

Commit 8df91c6

Browse files
author
Renzo Olivares
committed
opt 1
1 parent 8013821 commit 8df91c6

2 files changed

Lines changed: 31 additions & 40 deletions

File tree

packages/flutter/lib/src/widgets/selectable_region.dart

Lines changed: 25 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -2925,12 +2925,7 @@ abstract class MultiSelectableSelectionContainerDelegate extends SelectionContai
29252925
final int skipIndex = currentSelectionStartIndex == -1
29262926
? currentSelectionEndIndex
29272927
: currentSelectionStartIndex;
2928-
selectables
2929-
.where((Selectable target) => target != selectables[skipIndex])
2930-
.forEach(
2931-
(Selectable target) =>
2932-
dispatchSelectionEventToChild(target, const ClearSelectionEvent()),
2933-
);
2928+
_clearSelectables(skipIndex: skipIndex);
29342929
return;
29352930
}
29362931
final int skipStart = min(currentSelectionStartIndex, currentSelectionEndIndex);
@@ -2985,30 +2980,36 @@ abstract class MultiSelectableSelectionContainerDelegate extends SelectionContai
29852980
return closestIndex;
29862981
}
29872982

2983+
// Clears the selection in all [selectables], optionally skipping
2984+
// the [Selectable] at the given index.
2985+
void _clearSelectables({int skipIndex = -1}) {
2986+
for (var i = 0; i < selectables.length; i++) {
2987+
if (i == skipIndex) {
2988+
continue;
2989+
}
2990+
dispatchSelectionEventToChild(selectables[i], const ClearSelectionEvent());
2991+
}
2992+
}
2993+
29882994
SelectionResult _handleSelectBoundary(SelectionEvent event) {
29892995
assert(
29902996
event is SelectWordSelectionEvent || event is SelectParagraphSelectionEvent,
29912997
'This method should only be given selection events that select text boundaries.',
29922998
);
2993-
late final Offset effectiveGlobalPosition;
2994-
if (event.type == SelectionEventType.selectWord) {
2995-
effectiveGlobalPosition = (event as SelectWordSelectionEvent).globalPosition;
2996-
} else if (event.type == SelectionEventType.selectParagraph) {
2997-
effectiveGlobalPosition = (event as SelectParagraphSelectionEvent).globalPosition;
2998-
}
2999+
final Offset effectiveGlobalPosition = switch (event) {
3000+
SelectWordSelectionEvent(:final globalPosition) => globalPosition,
3001+
SelectParagraphSelectionEvent(:final globalPosition) => globalPosition,
3002+
_ => throw ArgumentError('Unsupported selection event: $event'),
3003+
};
29993004
SelectionResult? lastSelectionResult;
30003005
for (var index = 0; index < selectables.length; index += 1) {
30013006
var globalRectsContainPosition = false;
3002-
if (selectables[index].boundingBoxes.isNotEmpty) {
3003-
for (final Rect rect in selectables[index].boundingBoxes) {
3004-
final Rect globalRect = MatrixUtils.transformRect(
3005-
selectables[index].getTransformTo(null),
3006-
rect,
3007-
);
3008-
if (globalRect.contains(effectiveGlobalPosition)) {
3009-
globalRectsContainPosition = true;
3010-
break;
3011-
}
3007+
final Matrix4 transform = selectables[index].getTransformTo(null);
3008+
for (final Rect rect in selectables[index].boundingBoxes) {
3009+
final Rect globalRect = MatrixUtils.transformRect(transform, rect);
3010+
if (globalRect.contains(effectiveGlobalPosition)) {
3011+
globalRectsContainPosition = true;
3012+
break;
30123013
}
30133014
}
30143015
if (globalRectsContainPosition) {
@@ -3026,12 +3027,7 @@ abstract class MultiSelectableSelectionContainerDelegate extends SelectionContai
30263027
if (selectables[index].value != existingGeometry) {
30273028
// Geometry has changed as a result of select word, need to clear the
30283029
// selection of other selectables to keep selection in sync.
3029-
selectables
3030-
.where((Selectable target) => target != selectables[index])
3031-
.forEach(
3032-
(Selectable target) =>
3033-
dispatchSelectionEventToChild(target, const ClearSelectionEvent()),
3034-
);
3030+
_clearSelectables(skipIndex: index);
30353031
currentSelectionStartIndex = currentSelectionEndIndex = index;
30363032
}
30373033
return SelectionResult.end;
@@ -3052,12 +3048,7 @@ abstract class MultiSelectableSelectionContainerDelegate extends SelectionContai
30523048
if (selectables[nearestIndex].value != existingGeometry) {
30533049
// Geometry has changed as a result of select word, need to clear the
30543050
// selection of other selectables to keep selection in sync.
3055-
selectables
3056-
.where((Selectable target) => target != selectables[nearestIndex])
3057-
.forEach(
3058-
(Selectable target) =>
3059-
dispatchSelectionEventToChild(target, const ClearSelectionEvent()),
3060-
);
3051+
_clearSelectables(skipIndex: nearestIndex);
30613052
currentSelectionStartIndex = currentSelectionEndIndex = nearestIndex;
30623053
}
30633054
}

packages/flutter/lib/src/widgets/text.dart

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1456,12 +1456,12 @@ class _SelectableTextContainerDelegate extends StaticSelectionContainerDelegate
14561456
final int skipIndex = currentSelectionStartIndex == -1
14571457
? currentSelectionEndIndex
14581458
: currentSelectionStartIndex;
1459-
selectables
1460-
.where((Selectable target) => target != selectables[skipIndex])
1461-
.forEach(
1462-
(Selectable target) =>
1463-
dispatchSelectionEventToChild(target, const ClearSelectionEvent()),
1464-
);
1459+
for (var i = 0; i < selectables.length; i++) {
1460+
if (i == skipIndex) {
1461+
continue;
1462+
}
1463+
dispatchSelectionEventToChild(selectables[i], const ClearSelectionEvent());
1464+
}
14651465
return;
14661466
}
14671467
final int skipStart = min(currentSelectionStartIndex, currentSelectionEndIndex);

0 commit comments

Comments
 (0)