From 478d1dae77eb26591c22b8faccc8de96a6ff6001 Mon Sep 17 00:00:00 2001 From: "Lucas.Xu" Date: Mon, 9 Jan 2023 23:03:34 +0700 Subject: [PATCH] remove the unused check in selectable_text (#117716) --- .../lib/src/material/selectable_text.dart | 10 +---- .../test/widgets/selectable_text_test.dart | 39 +++++++++++++++++++ 2 files changed, 41 insertions(+), 8 deletions(-) diff --git a/packages/flutter/lib/src/material/selectable_text.dart b/packages/flutter/lib/src/material/selectable_text.dart index 226623a83092..2af93d678690 100644 --- a/packages/flutter/lib/src/material/selectable_text.dart +++ b/packages/flutter/lib/src/material/selectable_text.dart @@ -558,8 +558,6 @@ class _SelectableTextState extends State implements TextSelectio }); } - TextSelection? _lastSeenTextSelection; - void _handleSelectionChanged(TextSelection selection, SelectionChangedCause? cause) { final bool willShowSelectionHandles = _shouldShowSelectionHandles(cause); if (willShowSelectionHandles != _showSelectionHandles) { @@ -567,12 +565,8 @@ class _SelectableTextState extends State implements TextSelectio _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: diff --git a/packages/flutter/test/widgets/selectable_text_test.dart b/packages/flutter/test/widgets/selectable_text_test.dart index ce3bfde3c87e..a3cc2986eac7 100644 --- a/packages/flutter/test/widgets/selectable_text_test.dart +++ b/packages/flutter/test/widgets/selectable_text_test.dart @@ -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. + }); }