Fix line breaks being lost when copying after selection gesture in SelectableRegion (#184421)
Original discussion: https://github.com/flutter/flutter/pull/184043
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
- [x] I read the [Contributor Guide] and followed the process outlined
there for submitting PRs.
- [x] I read the [AI contribution guidelines] and understand my
responsibilities, or I am not using AI tools.
- [x] I read the [Tree Hygiene] wiki page, which explains my
responsibilities.
- [x] I read and followed the [Flutter Style Guide], including [Features
we expect every widget to implement].
- [x] I signed the [CLA].
- [x] I listed at least one issue that this PR fixes in the description
above.
- [x] I updated/added relevant documentation (doc comments with `///`).
- [x] I added new tests to check the change I am making, or this PR is
[test-exempt].
- [x] I followed the [breaking change policy] and added [Data Driven
Fixes] where supported.
- [x] All existing and new tests are passing.
---------
Co-authored-by: Renzo Olivares <roliv@google.com>
1 file changed