[Line Heights] Add textHeightBehavior to SelectableText. (#58530)

This commit is contained in:
MH Johnson 2020-06-03 14:53:02 -04:00 committed by GitHub
parent 79e2409419
commit 4bae771509
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 52 additions and 0 deletions

View File

@ -221,6 +221,7 @@ class SelectableText extends StatefulWidget {
this.enableInteractiveSelection = true,
this.onTap,
this.scrollPhysics,
this.textHeightBehavior,
this.textWidthBasis,
}) : assert(showCursor != null),
assert(autofocus != null),
@ -270,6 +271,7 @@ class SelectableText extends StatefulWidget {
this.enableInteractiveSelection = true,
this.onTap,
this.scrollPhysics,
this.textHeightBehavior,
this.textWidthBasis,
}) : assert(showCursor != null),
assert(autofocus != null),
@ -406,6 +408,9 @@ class SelectableText extends StatefulWidget {
/// {@macro flutter.widgets.editableText.scrollPhysics}
final ScrollPhysics scrollPhysics;
/// {@macro flutter.dart:ui.textHeightBehavior}
final TextHeightBehavior textHeightBehavior;
/// {@macro flutter.painting.textPainter.textWidthBasis}
final TextWidthBasis textWidthBasis;
@ -430,6 +435,7 @@ class SelectableText extends StatefulWidget {
properties.add(DiagnosticsProperty<Color>('cursorColor', cursorColor, defaultValue: null));
properties.add(FlagProperty('selectionEnabled', value: selectionEnabled, defaultValue: true, ifFalse: 'selection disabled'));
properties.add(DiagnosticsProperty<ScrollPhysics>('scrollPhysics', scrollPhysics, defaultValue: null));
properties.add(DiagnosticsProperty<TextHeightBehavior>('textHeightBehavior', textHeightBehavior, defaultValue: null));
}
}
@ -598,6 +604,7 @@ class _SelectableTextState extends State<SelectableText> with AutomaticKeepAlive
style: effectiveTextStyle,
readOnly: true,
textWidthBasis: widget.textWidthBasis ?? defaultTextStyle.textWidthBasis,
textHeightBehavior: widget.textHeightBehavior ?? defaultTextStyle.textHeightBehavior,
showSelectionHandles: _showSelectionHandles,
showCursor: widget.showCursor,
controller: _controller,

View File

@ -212,6 +212,7 @@ class RenderEditable extends RenderBox with RelayoutWhenSystemFontsChangeMixin {
this.ignorePointer = false,
bool readOnly = false,
bool forceLine = true,
TextHeightBehavior textHeightBehavior,
TextWidthBasis textWidthBasis = TextWidthBasis.parent,
String obscuringCharacter = '',
bool obscureText = false,
@ -264,6 +265,7 @@ class RenderEditable extends RenderBox with RelayoutWhenSystemFontsChangeMixin {
textScaleFactor: textScaleFactor,
locale: locale,
strutStyle: strutStyle,
textHeightBehavior: textHeightBehavior,
textWidthBasis: textWidthBasis,
),
_cursorColor = cursorColor,
@ -322,6 +324,15 @@ class RenderEditable extends RenderBox with RelayoutWhenSystemFontsChangeMixin {
/// The default value of this property is false.
bool ignorePointer;
/// {@macro flutter.dart:ui.textHeightBehavior}
TextHeightBehavior get textHeightBehavior => _textPainter.textHeightBehavior;
set textHeightBehavior(TextHeightBehavior value) {
if (_textPainter.textHeightBehavior == value)
return;
_textPainter.textHeightBehavior = value;
markNeedsTextLayout();
}
/// {@macro flutter.widgets.text.DefaultTextStyle.textWidthBasis}
TextWidthBasis get textWidthBasis => _textPainter.textWidthBasis;
set textWidthBasis(TextWidthBasis value) {

View File

@ -375,6 +375,7 @@ class EditableText extends StatefulWidget {
this.minLines,
this.expands = false,
this.forceLine = true,
this.textHeightBehavior,
this.textWidthBasis = TextWidthBasis.parent,
this.autofocus = false,
bool showCursor,
@ -486,6 +487,9 @@ class EditableText extends StatefulWidget {
/// {@endtemplate}
final bool obscureText;
/// {@macro flutter.dart:ui.textHeightBehavior},
final TextHeightBehavior textHeightBehavior;
/// {@macro flutter.widgets.text.DefaultTextStyle.textWidthBasis}
final TextWidthBasis textWidthBasis;
@ -1146,6 +1150,7 @@ class EditableText extends StatefulWidget {
properties.add(DiagnosticsProperty<ScrollController>('scrollController', scrollController, defaultValue: null));
properties.add(DiagnosticsProperty<ScrollPhysics>('scrollPhysics', scrollPhysics, defaultValue: null));
properties.add(DiagnosticsProperty<Iterable<String>>('autofillHints', autofillHints, defaultValue: null));
properties.add(DiagnosticsProperty<TextHeightBehavior>('textHeightBehavior', textHeightBehavior, defaultValue: null));
}
}
@ -2085,6 +2090,7 @@ class EditableTextState extends State<EditableText> with AutomaticKeepAliveClien
textAlign: widget.textAlign,
textDirection: _textDirection,
locale: widget.locale,
textHeightBehavior: widget.textHeightBehavior,
textWidthBasis: widget.textWidthBasis,
obscuringCharacter: widget.obscuringCharacter,
obscureText: widget.obscureText,
@ -2149,6 +2155,7 @@ class _Editable extends LeafRenderObjectWidget {
this.showCursor,
this.forceLine,
this.readOnly,
this.textHeightBehavior,
this.textWidthBasis,
this.hasFocus,
this.maxLines,
@ -2206,6 +2213,7 @@ class _Editable extends LeafRenderObjectWidget {
final Locale locale;
final String obscuringCharacter;
final bool obscureText;
final TextHeightBehavior textHeightBehavior;
final TextWidthBasis textWidthBasis;
final bool autocorrect;
final SmartDashesType smartDashesType;
@ -2255,6 +2263,7 @@ class _Editable extends LeafRenderObjectWidget {
ignorePointer: rendererIgnoresPointer,
obscuringCharacter: obscuringCharacter,
obscureText: obscureText,
textHeightBehavior: textHeightBehavior,
textWidthBasis: textWidthBasis,
cursorWidth: cursorWidth,
cursorRadius: cursorRadius,
@ -2295,6 +2304,7 @@ class _Editable extends LeafRenderObjectWidget {
..onSelectionChanged = onSelectionChanged
..onCaretChanged = onCaretChanged
..ignorePointer = rendererIgnoresPointer
..textHeightBehavior = textHeightBehavior
..textWidthBasis = textWidthBasis
..obscuringCharacter = obscuringCharacter
..obscureText = obscureText

View File

@ -374,6 +374,30 @@ void main() {
expect(textBox.size, const Size(633.0, 28.0));
});
testWidgets('can switch between textHeightBehavior', (WidgetTester tester) async {
const String text = 'selectable text';
const TextHeightBehavior textHeightBehavior = TextHeightBehavior(
applyHeightToFirstAscent: false,
applyHeightToLastDescent: false,
);
await tester.pumpWidget(
boilerplate(
child: const SelectableText(text),
),
);
expect(findRenderEditable(tester).textHeightBehavior, isNull);
await tester.pumpWidget(
boilerplate(
child: const SelectableText(
text,
textHeightBehavior: textHeightBehavior,
),
),
);
expect(findRenderEditable(tester).textHeightBehavior, textHeightBehavior);
});
testWidgets('Cursor blinks when showCursor is true', (WidgetTester tester) async {
await tester.pumpWidget(
overlay(