Skip to content

Commit 557fca4

Browse files
Add a sentinel value for TextStyle.height (#149049)
Fixes: flutter/flutter#58765 The rationale for the choice of the sentinel value: flutter/engine#52940 The exact value of `kTextHeightNone` should be kept as an implementation detail. It's unfortunate that the current value `0` is dangerously close to `TextStyle.height`'s valid domain. If we ever allow `TextStyle.height == 0` (which totally makes sense) then it shouldn't be difficult to change the const.
1 parent 90937b0 commit 557fca4

File tree

4 files changed

+27
-10
lines changed

4 files changed

+27
-10
lines changed

packages/flutter/lib/painting.dart

+1-1
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
/// painting boxes.
1818
library painting;
1919

20-
export 'dart:ui' show PlaceholderAlignment, Shadow, TextHeightBehavior, TextLeadingDistribution;
20+
export 'dart:ui' show PlaceholderAlignment, Shadow, TextHeightBehavior, TextLeadingDistribution, kTextHeightNone;
2121

2222
export 'src/painting/alignment.dart';
2323
export 'src/painting/basic_types.dart';

packages/flutter/lib/src/painting/text_style.dart

+10-9
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import 'dart:ui' as ui show
88
Shadow,
99
StrutStyle,
1010
TextStyle,
11+
kTextHeightNone,
1112
lerpDouble;
1213

1314
import 'package:flutter/foundation.dart';
@@ -640,14 +641,13 @@ class TextStyle with Diagnosticable {
640641

641642
/// The height of this text span, as a multiple of the font size.
642643
///
643-
/// When [height] is null or omitted, the line height will be determined
644-
/// by the font's metrics directly, which may differ from the fontSize.
645-
/// When [height] is non-null, the line height of the span of text will be a
646-
/// multiple of [fontSize] and be exactly `fontSize * height` logical pixels
647-
/// tall.
644+
/// When [height] is [kTextHeightNone], the line height will be determined by
645+
/// the font's metrics directly, which may differ from the fontSize. Otherwise
646+
/// the line height of the span of text will be a multiple of [fontSize],
647+
/// and be exactly `fontSize * height` logical pixels tall.
648648
///
649-
/// For most fonts, setting [height] to 1.0 is not the same as omitting or
650-
/// setting height to null because the [fontSize] sets the height of the EM-square,
649+
/// For most fonts, setting [height] to 1.0 is not the same as setting height
650+
/// to [kTextHeightNone] because the [fontSize] sets the height of the EM-square,
651651
/// which is different than the font provided metrics for line height. The
652652
/// following diagram illustrates the difference between the font-metrics
653653
/// defined line height and the line height produced with `height: 1.0`
@@ -954,7 +954,8 @@ class TextStyle with Diagnosticable {
954954
/// [TextStyle] with a [FontWeight.w300].
955955
///
956956
/// If the underlying values are null, then the corresponding factors and/or
957-
/// deltas must not be specified.
957+
/// deltas must not be specified. Additionally, if [height] is [kTextHeightNone]
958+
/// it will not be modified by this method.
958959
///
959960
/// If [foreground] is specified on this object, then applying [color] here
960961
/// will have no effect and if [background] is specified on this object, then
@@ -1014,7 +1015,7 @@ class TextStyle with Diagnosticable {
10141015
letterSpacing: letterSpacing == null ? null : letterSpacing! * letterSpacingFactor + letterSpacingDelta,
10151016
wordSpacing: wordSpacing == null ? null : wordSpacing! * wordSpacingFactor + wordSpacingDelta,
10161017
textBaseline: textBaseline ?? this.textBaseline,
1017-
height: height == null ? null : height! * heightFactor + heightDelta,
1018+
height: (height == null || height == ui.kTextHeightNone) ? height : height! * heightFactor + heightDelta,
10181019
leadingDistribution: leadingDistribution ?? this.leadingDistribution,
10191020
locale: locale ?? this.locale,
10201021
foreground: foreground,

packages/flutter/test/painting/text_painter_test.dart

+11
Original file line numberDiff line numberDiff line change
@@ -1696,6 +1696,17 @@ void main() {
16961696
);
16971697
});
16981698

1699+
test('kTextHeightNone unsets the text height multiplier', () {
1700+
final TextPainter painter = TextPainter(
1701+
textDirection: TextDirection.ltr,
1702+
text: const TextSpan(
1703+
style: TextStyle(fontSize: 10, height: 1000),
1704+
children: <TextSpan>[TextSpan(text: 'A', style: TextStyle(height: kTextHeightNone))],
1705+
),
1706+
)..layout();
1707+
expect(painter.height, 10);
1708+
});
1709+
16991710
test('TextPainter dispatches memory events', () async {
17001711
await expectLater(
17011712
await memoryEvents(() => TextPainter().dispose(), TextPainter),

packages/flutter/test/painting/text_style_test.dart

+5
Original file line numberDiff line numberDiff line change
@@ -570,6 +570,11 @@ void main() {
570570
style.apply(leadingDistribution: TextLeadingDistribution.proportional).leadingDistribution,
571571
TextLeadingDistribution.proportional,
572572
);
573+
574+
expect(
575+
const TextStyle(height: kTextHeightNone).apply(heightFactor: 1000, heightDelta: 1000).height,
576+
kTextHeightNone,
577+
);
573578
});
574579

575580
test('TextStyle fontFamily and package', () {

0 commit comments

Comments
 (0)