From 7a739151155546e9272a8102ea6febfc1919211a Mon Sep 17 00:00:00 2001 From: Renzo Olivares Date: Thu, 1 Jul 2021 09:01:05 -0700 Subject: [PATCH] Expose selectionHeightStyle and SelectionWidthStyle on SelectableText (#85640) --- .../lib/src/material/selectable_text.dart | 26 ++++- .../test/widgets/selectable_text_test.dart | 106 ++++++++++++++++++ 2 files changed, 129 insertions(+), 3 deletions(-) diff --git a/packages/flutter/lib/src/material/selectable_text.dart b/packages/flutter/lib/src/material/selectable_text.dart index b3069f7bc8..17f6a6b9e8 100644 --- a/packages/flutter/lib/src/material/selectable_text.dart +++ b/packages/flutter/lib/src/material/selectable_text.dart @@ -2,6 +2,8 @@ // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. +import 'dart:ui' as ui show BoxHeightStyle, BoxWidthStyle; + import 'package:flutter/cupertino.dart'; import 'package:flutter/foundation.dart'; import 'package:flutter/gestures.dart'; @@ -168,9 +170,9 @@ class SelectableText extends StatefulWidget { /// closest enclosing [DefaultTextStyle]. /// - /// The [showCursor], [autofocus], [dragStartBehavior], and [data] parameters - /// must not be null. If specified, the [maxLines] argument must be greater - /// than zero. + /// The [showCursor], [autofocus], [dragStartBehavior], [selectionHeightStyle], + /// [selectionWidthStyle] and [data] parameters must not be null. If specified, + /// the [maxLines] argument must be greater than zero. const SelectableText( String this.data, { Key? key, @@ -189,6 +191,8 @@ class SelectableText extends StatefulWidget { this.cursorHeight, this.cursorRadius, this.cursorColor, + this.selectionHeightStyle = ui.BoxHeightStyle.tight, + this.selectionWidthStyle = ui.BoxWidthStyle.tight, this.dragStartBehavior = DragStartBehavior.start, this.enableInteractiveSelection = true, this.selectionControls, @@ -200,6 +204,8 @@ class SelectableText extends StatefulWidget { }) : assert(showCursor != null), assert(autofocus != null), assert(dragStartBehavior != null), + assert(selectionHeightStyle != null), + assert(selectionWidthStyle != null), assert(maxLines == null || maxLines > 0), assert(minLines == null || minLines > 0), assert( @@ -242,6 +248,8 @@ class SelectableText extends StatefulWidget { this.cursorHeight, this.cursorRadius, this.cursorColor, + this.selectionHeightStyle = ui.BoxHeightStyle.tight, + this.selectionWidthStyle = ui.BoxWidthStyle.tight, this.dragStartBehavior = DragStartBehavior.start, this.enableInteractiveSelection = true, this.selectionControls, @@ -352,6 +360,16 @@ class SelectableText extends StatefulWidget { /// Defaults to the theme's `cursorColor` when null. final Color? cursorColor; + /// Controls how tall the selection highlight boxes are computed to be. + /// + /// See [ui.BoxHeightStyle] for details on available styles. + final ui.BoxHeightStyle selectionHeightStyle; + + /// Controls how wide the selection highlight boxes are computed to be. + /// + /// See [ui.BoxWidthStyle] for details on available styles. + final ui.BoxWidthStyle selectionWidthStyle; + /// {@macro flutter.widgets.editableText.enableInteractiveSelection} final bool enableInteractiveSelection; @@ -665,6 +683,8 @@ class _SelectableTextState extends State with AutomaticKeepAlive cursorHeight: widget.cursorHeight, cursorRadius: cursorRadius, cursorColor: cursorColor, + selectionHeightStyle: widget.selectionHeightStyle, + selectionWidthStyle: widget.selectionWidthStyle, cursorOpacityAnimates: cursorOpacityAnimates, cursorOffset: cursorOffset, paintCursorAboveText: paintCursorAboveText, diff --git a/packages/flutter/test/widgets/selectable_text_test.dart b/packages/flutter/test/widgets/selectable_text_test.dart index 9047d5fe05..68a3351734 100644 --- a/packages/flutter/test/widgets/selectable_text_test.dart +++ b/packages/flutter/test/widgets/selectable_text_test.dart @@ -3,6 +3,8 @@ // found in the LICENSE file. @TestOn('!chrome') +import 'dart:ui' as ui show BoxHeightStyle, BoxWidthStyle; + import 'package:flutter/cupertino.dart'; import 'package:flutter/foundation.dart'; import 'package:flutter/gestures.dart'; @@ -4799,4 +4801,108 @@ void main() { expect(selection!.baseOffset, 6); expect(selection!.extentOffset, 14); }, variant: const TargetPlatformVariant({ TargetPlatform.iOS, TargetPlatform.android })); + + testWidgets('text selection style 1', (WidgetTester tester) async { + await tester.pumpWidget( + MaterialApp( + home: Material( + child: Center( + child: Column( + children: const [ + SelectableText.rich( + TextSpan( + children: [ + TextSpan( + text: 'Atwater Peel ', + style: TextStyle( + fontSize: 30.0, + ), + ), + TextSpan( + text: 'Sherbrooke Bonaventure ', + style: TextStyle( + fontSize: 15.0, + ), + ), + TextSpan( + text: 'hi wassup!', + style: TextStyle( + fontSize: 10.0, + ), + ), + ], + ), + key: Key('field0'), + selectionHeightStyle: ui.BoxHeightStyle.includeLineSpacingTop, + selectionWidthStyle: ui.BoxWidthStyle.max, + ), + ], + ), + ), + ), + ), + ); + + final EditableText editableTextWidget = tester.widget(find.byType(EditableText).first); + final TextEditingController controller = editableTextWidget.controller; + controller.selection = const TextSelection(baseOffset: 0, extentOffset: 46); + await tester.pump(); + + await expectLater( + find.byType(MaterialApp), + matchesGoldenFile('selectable_text_golden.TextSelectionStyle.1.png'), + ); + }); + + testWidgets('text selection style 2', (WidgetTester tester) async { + await tester.pumpWidget( + MaterialApp( + home: Material( + child: Center( + child: Column( + children: const [ + SelectableText.rich( + TextSpan( + children: [ + TextSpan( + text: 'Atwater Peel ', + style: TextStyle( + fontSize: 30.0, + ), + ), + TextSpan( + text: 'Sherbrooke Bonaventure ', + style: TextStyle( + fontSize: 15.0, + ), + ), + TextSpan( + text: 'hi wassup!', + style: TextStyle( + fontSize: 10.0, + ), + ), + ], + ), + key: Key('field0'), + selectionHeightStyle: ui.BoxHeightStyle.includeLineSpacingBottom, + selectionWidthStyle: ui.BoxWidthStyle.tight, + ), + ], + ), + ), + ), + ), + ); + + final EditableText editableTextWidget = tester.widget(find.byType(EditableText).first); + final TextEditingController controller = editableTextWidget.controller; + controller.selection = const TextSelection(baseOffset: 0, extentOffset: 46); + await tester.pump(); + + await expectLater( + find.byType(MaterialApp), + matchesGoldenFile('selectable_text_golden.TextSelectionStyle.2.png'), + ); + }); }