Skip to content

Fix line breaks being lost when copying after selection gesture in SelectableRegion#184043

Closed
Renzo-Olivares wants to merge 10000 commits into
flutter:masterfrom
Renzo-Olivares:selectableregion-newlines-copy-fix
Closed

Fix line breaks being lost when copying after selection gesture in SelectableRegion#184043
Renzo-Olivares wants to merge 10000 commits into
flutter:masterfrom
Renzo-Olivares:selectableregion-newlines-copy-fix

Conversation

@Renzo-Olivares

@Renzo-Olivares Renzo-Olivares commented Mar 23, 2026

Copy link
Copy Markdown
Contributor

Fixes #154253

Before this change the _rect used by _SelectableFragment was calculated using getBoxesForSelection which defaults to a BoxHeightStyle of BoxHeightStyle.tight. This style ensures the boxes returned from getBoxesForSelection tightly wrap each character. This was causing an issue when text ended or was split on new lines because the SelectableFragment._rect did not include the region that actually had the new lines. So when _SelectableFragment internally used SelectionUtils.adjustDragOffset to clamp the position to the selectable rect, the selectable rect is inaccurate and always results in a position that is above the new line and not on it.

After this change the _rect used by _SelectableFragment is calculated using getBoxesForSelection with a BoxHeightStyle of BoxHeightStyle.max. This style ensures the boxes returned from getBoxesForSelection cover the entire height of the line, and as a result including the new line character region.

Other notes:

TextPainter.getBoxesForSelection explicitly notes that leading or trailing new line characters will be represented by zero-width boxes.

Pre-launch Checklist

  • I read the [Contributor Guide] and followed the process outlined there for submitting PRs.
  • I read the [AI contribution guidelines] and understand my responsibilities, or I am not using AI tools.
  • I read the [Tree Hygiene] wiki page, which explains my responsibilities.
  • I read and followed the [Flutter Style Guide], including [Features we expect every widget to implement].
  • I signed the [CLA].
  • I listed at least one issue that this PR fixes in the description above.
  • I updated/added relevant documentation (doc comments with ///).
  • I added new tests to check the change I am making, or this PR is [test-exempt].
  • I followed the [breaking change policy] and added [Data Driven Fixes] where supported.
  • All existing and new tests are passing.

Loading
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

a: accessibility Accessibility, e.g. VoiceOver or TalkBack. (aka a11y) a: animation Animation APIs a: desktop Running on desktop a: text input Entering text in a text field or keyboard related problems CICD Run CI/CD engine flutter/engine related. See also e: labels. f: cupertino flutter/packages/flutter/cupertino repository f: focus Focus traversal, gaining or losing focus f: gestures flutter/packages/flutter/gestures repository. f: material design flutter/packages/flutter/material repository. f: routes Navigator, Router, and related APIs. f: scrolling Viewports, list views, slivers, etc.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Line breaks are lost when selecting text in SelectionArea/SelectableRegion