Expose selectionHeightStyle and SelectionWidthStyle on SelectableText (#85640)
This commit is contained in:
@@ -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<SelectableText> with AutomaticKeepAlive
|
||||
cursorHeight: widget.cursorHeight,
|
||||
cursorRadius: cursorRadius,
|
||||
cursorColor: cursorColor,
|
||||
selectionHeightStyle: widget.selectionHeightStyle,
|
||||
selectionWidthStyle: widget.selectionWidthStyle,
|
||||
cursorOpacityAnimates: cursorOpacityAnimates,
|
||||
cursorOffset: cursorOffset,
|
||||
paintCursorAboveText: paintCursorAboveText,
|
||||
|
||||
@@ -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>{ TargetPlatform.iOS, TargetPlatform.android }));
|
||||
|
||||
testWidgets('text selection style 1', (WidgetTester tester) async {
|
||||
await tester.pumpWidget(
|
||||
MaterialApp(
|
||||
home: Material(
|
||||
child: Center(
|
||||
child: Column(
|
||||
children: const <Widget>[
|
||||
SelectableText.rich(
|
||||
TextSpan(
|
||||
children: <TextSpan>[
|
||||
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 <Widget>[
|
||||
SelectableText.rich(
|
||||
TextSpan(
|
||||
children: <TextSpan>[
|
||||
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'),
|
||||
);
|
||||
});
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user