Skip to content

Commit 103d780

Browse files
authored
Fix multiline EditableText selection highlighting width (flutter#185239)
## Description This PR fixes multiline text selection highlighting that can incorrectly include trailing whitespace width from another line. <img width="500" height="1100" alt="Screenshot_1776501043" src="https://github.com/user-attachments/assets/9cff38f7-86fc-447b-8026-c419c5de36af" /> In `EditableText`, the default `defaultSelectionWidthStyle` now returns `BoxWidthStyle.tight` always This prevents multiline selections from being visually extended to match other line widths (the behavior reported in the issue), while preserving current single-line behavior and honoring explicit `selectionWidthStyle` overrides. ## Fixed issue Fixes flutter#174689 Fixes flutter#180311 ## Tests Added widget tests in `packages/flutter/test/widgets/editable_text_test.dart`: - `EditableText uses tight selection width style by default for multiline` - `Explicit selectionWidthStyle is honored for multiline EditableText` Locally verified: - `flutter test packages/flutter/test/widgets/editable_text_test.dart --plain-name "selection width style"` - `flutter test packages/flutter/test/material/text_field_test.dart --plain-name "text selection style"` - `flutter analyze packages/flutter/lib/src/widgets/editable_text.dart packages/flutter/test/widgets/editable_text_test.dart`
1 parent 0c29588 commit 103d780

5 files changed

Lines changed: 85 additions & 41 deletions

File tree

‎packages/flutter/lib/src/rendering/editable.dart‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -321,7 +321,7 @@ class RenderEditable extends RenderBox
321321
Offset cursorOffset = Offset.zero,
322322
this._devicePixelRatio = 1.0,
323323
ui.BoxHeightStyle selectionHeightStyle = ui.BoxHeightStyle.max,
324-
ui.BoxWidthStyle selectionWidthStyle = ui.BoxWidthStyle.max,
324+
ui.BoxWidthStyle selectionWidthStyle = ui.BoxWidthStyle.tight,
325325
this._enableInteractiveSelection,
326326
this.floatingCursorAddedMargin = const EdgeInsets.fromLTRB(4, 4, 4, 5),
327327
TextRange? promptRectRange,
@@ -596,6 +596,8 @@ class RenderEditable extends RenderBox
596596

597597
/// Controls how wide the selection highlight boxes are computed to be.
598598
///
599+
/// Defaults to [ui.BoxWidthStyle.tight].
600+
///
599601
/// See [ui.BoxWidthStyle] for details on available styles.
600602
ui.BoxWidthStyle get selectionWidthStyle => _selectionPainter.selectionWidthStyle;
601603
set selectionWidthStyle(ui.BoxWidthStyle value) {

‎packages/flutter/lib/src/widgets/_web_browser_detection_io.dart‎

Lines changed: 0 additions & 12 deletions
This file was deleted.

‎packages/flutter/lib/src/widgets/_web_browser_detection_web.dart‎

Lines changed: 0 additions & 11 deletions
This file was deleted.

‎packages/flutter/lib/src/widgets/editable_text.dart‎

Lines changed: 2 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,6 @@ import 'package:flutter/rendering.dart';
2525
import 'package:flutter/scheduler.dart';
2626
import 'package:flutter/services.dart';
2727

28-
import '_web_browser_detection_io.dart'
29-
if (dart.library.js_interop) '_web_browser_detection_web.dart';
3028
import 'actions.dart';
3129
import 'app_lifecycle_listener.dart';
3230
import 'autofill.dart';
@@ -2155,21 +2153,8 @@ class EditableText extends StatefulWidget {
21552153

21562154
/// The default value for [selectionWidthStyle].
21572155
///
2158-
/// On web platforms, this defaults to [ui.BoxWidthStyle.max] for Apple platforms running
2159-
/// Safari (webkit) based browsers and [ui.BoxWidthStyle.tight] for all others.
2160-
///
2161-
/// On non-web platforms, this defaults to [ui.BoxWidthStyle.max].
2162-
static ui.BoxWidthStyle get defaultSelectionWidthStyle {
2163-
if (kIsWeb) {
2164-
if (defaultTargetPlatform == TargetPlatform.iOS || WebBrowserDetection.isSafari) {
2165-
// On macOS web, the selection width behavior differs when running on
2166-
// Chrom(e|ium) (blink) or Safari (webkit).
2167-
return ui.BoxWidthStyle.max;
2168-
}
2169-
return ui.BoxWidthStyle.tight;
2170-
}
2171-
return ui.BoxWidthStyle.max;
2172-
}
2156+
/// Returns [ui.BoxWidthStyle.tight] on all platforms.
2157+
static ui.BoxWidthStyle get defaultSelectionWidthStyle => ui.BoxWidthStyle.tight;
21732158

21742159
/// The default value for [stylusHandwritingEnabled].
21752160
static const bool defaultStylusHandwritingEnabled = true;

‎packages/flutter/test/widgets/editable_text_test.dart‎

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -294,6 +294,86 @@ void main() {
294294
skip: kIsWeb, // [intended]
295295
);
296296

297+
testWidgets('default text selection width style', (WidgetTester tester) async {
298+
const blue = Color(0xFF2196F3);
299+
const grey = Color(0xFF9E9E9E);
300+
const black = Color(0xFF000000);
301+
const selectionColor = Color(0xFFFFFF00);
302+
controller.text = 'a b c\na b c d e f g';
303+
304+
await tester.pumpWidget(
305+
TestWidgetsApp(
306+
home: Center(
307+
child: EditableText(
308+
controller: controller,
309+
focusNode: focusNode,
310+
maxLines: null,
311+
style: const TextStyle(
312+
fontFamily: 'FlutterTest',
313+
fontSize: 14.0,
314+
color: black,
315+
height: 2.0,
316+
),
317+
cursorColor: blue,
318+
backgroundCursorColor: grey,
319+
selectionColor: selectionColor,
320+
keyboardType: TextInputType.text,
321+
),
322+
),
323+
),
324+
);
325+
326+
controller.selection = TextSelection(baseOffset: 0, extentOffset: controller.text.length);
327+
await tester.pumpAndSettle();
328+
329+
await expectLater(
330+
find.byType(TestWidgetsApp),
331+
matchesGoldenFile('editable_text_golden.TextSelectionWidthStyle.1.png'),
332+
);
333+
}, variant: TargetPlatformVariant.all());
334+
335+
// Regression test for https://github.com/flutter/flutter/issues/174689.
336+
testWidgets('Tight selectionWidthStyle does not expand to a longer next line', (
337+
WidgetTester tester,
338+
) async {
339+
const backgroundCursorColor = Color(0xFF9E9E9E);
340+
const selectionColor = Color(0xFF000000);
341+
const fontSize = 10.0;
342+
const characterWidth = fontSize; // FlutterTest is a square monospace font.
343+
controller.text = 'abc\nabcdefghij';
344+
345+
await tester.pumpWidget(
346+
TestWidgetsApp(
347+
home: Align(
348+
alignment: Alignment.topLeft,
349+
child: EditableText(
350+
controller: controller,
351+
focusNode: focusNode,
352+
maxLines: null,
353+
selectionWidthStyle: BoxWidthStyle.tight,
354+
style: const TextStyle(fontFamily: 'FlutterTest', fontSize: fontSize, height: 1.0),
355+
cursorColor: cursorColor,
356+
backgroundCursorColor: backgroundCursorColor,
357+
selectionColor: selectionColor,
358+
),
359+
),
360+
),
361+
);
362+
363+
final RenderEditable renderEditable = findRenderEditable(tester);
364+
expect(renderEditable.selectionWidthStyle, BoxWidthStyle.tight);
365+
366+
// Select the first line, including the line feed character. The second line
367+
// is longer, but BoxWidthStyle.tight should not expand the highlight to the
368+
// second line's width.
369+
controller.selection = const TextSelection(baseOffset: 0, extentOffset: 4);
370+
await tester.pump();
371+
372+
const expectedSelectionRect = Rect.fromLTRB(0.0, 0.0, 3 * characterWidth, fontSize);
373+
374+
expect(renderEditable, paints..rect(color: selectionColor, rect: expectedSelectionRect));
375+
});
376+
297377
group('Check the passed groupId value', () {
298378
testWidgets('The value of the passed-in groupId should match the groupId of the EditableText', (
299379
WidgetTester tester,

0 commit comments

Comments
 (0)