Skip to content

Commit

Permalink
remove the unused check in selectable_text (#117716)
Browse files Browse the repository at this point in the history
  • Loading branch information
LucasXu0 authored Jan 9, 2023
1 parent 6c225dd commit 478d1da
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 8 deletions.
10 changes: 2 additions & 8 deletions packages/flutter/lib/src/material/selectable_text.dart
Original file line number Diff line number Diff line change
Expand Up @@ -558,21 +558,15 @@ class _SelectableTextState extends State<SelectableText> implements TextSelectio
});
}

TextSelection? _lastSeenTextSelection;

void _handleSelectionChanged(TextSelection selection, SelectionChangedCause? cause) {
final bool willShowSelectionHandles = _shouldShowSelectionHandles(cause);
if (willShowSelectionHandles != _showSelectionHandles) {
setState(() {
_showSelectionHandles = willShowSelectionHandles;
});
}
// TODO(chunhtai): The selection may be the same. We should remove this
// check once this is fixed https://github.com/flutter/flutter/issues/76349.
if (widget.onSelectionChanged != null && _lastSeenTextSelection != selection) {
widget.onSelectionChanged!(selection, cause);
}
_lastSeenTextSelection = selection;

widget.onSelectionChanged?.call(selection, cause);

switch (Theme.of(context).platform) {
case TargetPlatform.iOS:
Expand Down
39 changes: 39 additions & 0 deletions packages/flutter/test/widgets/selectable_text_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -5367,4 +5367,43 @@ void main() {
final EditableText editableText = tester.widget(find.byType(EditableText));
expect(editableText.style.fontSize, textStyle.fontSize);
});

testWidgets('SelectableText text span style is merged with default text style', (WidgetTester tester) async {
TextSelection? selection;
int count = 0;

await tester.pumpWidget(
MaterialApp(
home: SelectableText(
'I love Flutter!',
onSelectionChanged: (TextSelection s, _) {
selection = s;
count++;
},
),
),
);

expect(selection, null);
expect(count, 0);

// Tap to put the cursor before the "F".
const int index = 7;
await tester.tapAt(textOffsetToPosition(tester, index));
await tester.pump(const Duration(milliseconds: 500));
expect(
selection,
const TextSelection.collapsed(offset: index),
);
expect(count, 1); // The `onSelectionChanged` will be triggered one time.

// Tap on the same location again.
await tester.tapAt(textOffsetToPosition(tester, index));
await tester.pump(const Duration(milliseconds: 50));
expect(
selection,
const TextSelection.collapsed(offset: index),
);
expect(count, 1); // The `onSelectionChanged` will not be triggered.
});
}

0 comments on commit 478d1da

Please sign in to comment.