Thanks to visit codestin.com
Credit goes to github.com

Skip to content

TextField magnifier stuck on long press cancel #167881

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

Open
wants to merge 4 commits into
base: master
Choose a base branch
from

Conversation

O-Hannonen
Copy link

Fixes #167879

This PR fixes bug where magnifier and floating cursor get stuck on screen when long press is cancelled (eg. by closing the app).

Pre-launch Checklist

If you need help, consider asking for advice on the #hackers-new channel on Discord.

@github-actions github-actions bot added a: text input Entering text in a text field or keyboard related problems framework flutter/packages/flutter repository. See also f: labels. f: material design flutter/packages/flutter/material repository. f: cupertino flutter/packages/flutter/cupertino repository labels Apr 26, 2025
Copy link
Contributor

@VB10 VB10 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lgtm 🎉


/// Handler for [TextSelectionGestureDetector.onSingleLongTapCancel].
///
/// By default, it hides magnifier and floating cursor if necesasary.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
/// By default, it hides magnifier and floating cursor if necesasary.
/// By default, it hides the magnifier and the floating cursor if necessary.

@@ -3304,6 +3323,9 @@ class TextSelectionGestureDetector extends StatefulWidget {
/// Called after [onSingleLongTapStart] when the pointer is lifted.
final GestureLongPressEndCallback? onSingleLongTapEnd;

/// Called after [onSingleLongTapStart] when the pointer is cancelled.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
/// Called after [onSingleLongTapStart] when the pointer is cancelled.
/// Called after [onSingleLongTapStart] when the pointer is canceled.

@@ -2748,6 +2749,22 @@ class TextSelectionGestureDetectorBuilder {
}
}

void _longTapCleanup(){
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What do you think of this name?

Suggested change
void _longTapCleanup(){
void _onSingleLongTapEndOrCancel() {

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Makes sense!

@loic-sharma
Copy link
Member

FYI there's a test failure: https://logs.chromium.org/logs/flutter/buildbucket/cr-buildbucket/8716520139161227697/+/u/run_test.dart_for_framework_tests_shard_and_subshard_widgets/stdout

It looks like if the text field is unmounted after a long press down, onLongPressCancel is called. I suspect we'll want to update _hideMagnifierIfSupportedByPlatform to no-op if the text field is unmounted.

Test failure logs...
06:03 +5819 ~113: /b/s/w/ir/x/w/flutter/packages/flutter/test/widgets/text_selection_test.dart: test TextSelectionGestureDetectorBuilder forcePress disabled
══╡ EXCEPTION CAUGHT BY GESTURE ╞═══════════════════════════════════════════════════════════════════
The following _TypeError was thrown while handling a gesture:
Null check operator used on a null value

When the exception was thrown, this was the stack:
#0      TextSelectionGestureDetectorBuilder.editableText (package:flutter/src/widgets/text_selection.dart:2176:78)
#1      TextSelectionGestureDetectorBuilder._hideMagnifierIfSupportedByPlatform (package:flutter/src/widgets/text_selection.dart:2068:9)
#2      TextSelectionGestureDetectorBuilder._longTapCleanup (package:flutter/src/widgets/text_selection.dart:2753:5)
#3      TextSelectionGestureDetectorBuilder.onSingleLongTapCancel (package:flutter/src/widgets/text_selection.dart:2682:5)
#4      _TextSelectionGestureDetectorState._handleLongPressCancel (package:flutter/src/widgets/text_selection.dart:3471:35)
#5      GestureRecognizer.invokeCallback (package:flutter/src/gestures/recognizer.dart:345:24)
#6      LongPressGestureRecognizer._checkLongPressCancel (package:flutter/src/gestures/long_press.dart:721:13)
#7      LongPressGestureRecognizer.resolve (package:flutter/src/gestures/long_press.dart:867:9)
#8      OneSequenceGestureRecognizer.dispose (package:flutter/src/gestures/recognizer.dart:470:5)
#9      PrimaryPointerGestureRecognizer.dispose (package:flutter/src/gestures/recognizer.dart:765:11)
#10     RawGestureDetectorState.dispose (package:flutter/src/widgets/gesture_detector.dart:1529:18)
#11     StatefulElement.unmount (package:flutter/src/widgets/framework.dart:5922:11)
...
#349    ListIterable.forEach (dart:_internal/iterable.dart:49:13)
#350    _InactiveElements._unmountAll (package:flutter/src/widgets/framework.dart:2084:25)
#351    BuildOwner.lockState (package:flutter/src/widgets/framework.dart:2965:15)
#352    BuildOwner.finalizeTree (package:flutter/src/widgets/framework.dart:3288:7)
#353    AutomatedTestWidgetsFlutterBinding.drawFrame (package:flutter_test/src/binding.dart:1537:19)
#354    RendererBinding._handlePersistentFrameCallback (package:flutter/src/rendering/binding.dart:495:5)
#355    SchedulerBinding._invokeFrameCallback (package:flutter/src/scheduler/binding.dart:1438:15)
#356    SchedulerBinding.handleDrawFrame (package:flutter/src/scheduler/binding.dart:1351:9)
#357    AutomatedTestWidgetsFlutterBinding.scheduleWarmUpFrame (package:flutter_test/src/binding.dart:1441:5)
#358    _runWidget (package:flutter/src/widgets/binding.dart:1547:7)
#359    runApp (package:flutter/src/widgets/binding.dart:1480:3)
#360    TestWidgetsFlutterBinding._runTestBody (package:flutter_test/src/binding.dart:1071:7)
<asynchronous suspension>
<asynchronous suspension>
(elided one frame from package:stack_trace)

Handler: "onLongPressCancel"
Recognizer:
  LongPressGestureRecognizer#02ba0
════════════════════════════════════════════════════════════════════════════════════════════════════
...
06:03 +5819 ~113 -1: /b/s/w/ir/x/w/flutter/packages/flutter/test/widgets/text_selection_test.dart: test TextSelectionGestureDetectorBuilder forcePress disabled [E]
  Test failed. See exception logs above.
  The test description was: test TextSelectionGestureDetectorBuilder forcePress disabled

@O-Hannonen O-Hannonen requested a review from loic-sharma May 3, 2025 08:53
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
a: text input Entering text in a text field or keyboard related problems f: cupertino flutter/packages/flutter/cupertino repository f: material design flutter/packages/flutter/material repository. framework flutter/packages/flutter repository. See also f: labels.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

TextField magnifier gets stuck on screen when long press is cancelled
3 participants