Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix jumping when doing long press for selecting text #102270

Merged
merged 2 commits into from
Apr 22, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions packages/flutter/lib/src/widgets/editable_text.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2514,6 +2514,11 @@ class EditableTextState extends State<EditableText> with AutomaticKeepAliveClien
_showCaretOnScreenScheduled = true;
SchedulerBinding.instance.addPostFrameCallback((Duration _) {
_showCaretOnScreenScheduled = false;

// if cursor is inactive, e.g. while selecting text, do not jump away
if (!_cursorActive) {
return;
}
if (_currentCaretRect == null || !_scrollController.hasClients) {
return;
}
Expand Down
47 changes: 47 additions & 0 deletions packages/flutter/test/widgets/editable_text_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -5183,6 +5183,53 @@ void main() {
// toolbar. Until we change that, this test should remain skipped.
}, skip: kIsWeb); // [intended]


testWidgets('text selection handle visibility for long text', (WidgetTester tester) async {
// long text which is scrollable based on given box size
const String testText =
'Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet.';
final TextEditingController controller =
TextEditingController(text: testText);
final ScrollController scrollController = ScrollController();

await tester.pumpWidget(MaterialApp(
home: Align(
alignment: Alignment.topLeft,
child: SizedBox(
width: 100,
height: 100,
child: SingleChildScrollView(
controller: scrollController,
child: EditableText(
controller: controller,
showSelectionHandles: true,
focusNode: FocusNode(),
style: Typography.material2018().black.subtitle1!,
cursorColor: Colors.blue,
backgroundCursorColor: Colors.grey,
selectionControls: materialTextSelectionControls,
keyboardType: TextInputType.multiline,
maxLines: null,
),
),
),
),
));

// scroll to a text that is outside of the inital visible rect
scrollController.jumpTo(151);
await tester.pump();

// long press on a word to trigger a select
await tester.longPressAt(const Offset(20, 15));
// wait for adjustments of scroll area
await tester.pump();
await tester.pumpAndSettle();

// assert not jumped to top
expect(scrollController.offset, equals(151));
});

const String testText = 'Now is the time for\n' // 20
'all good people\n' // 20 + 16 => 36
'to come to the aid\n' // 36 + 19 => 55
Expand Down