Skip to content

Commit 5d59f8b

Browse files
authored
Add TextHeightBehavior argument for DefaultTextStyle.merge (#153178)
Addresses an issue where `DefaultTextStyle.merge` is missing the `textHeightBehavior` argument. The argument is present in the `DefaultTextStyle` constructor. Resolves #120176
1 parent 81ee230 commit 5d59f8b

File tree

2 files changed

+52
-0
lines changed

2 files changed

+52
-0
lines changed

packages/flutter/lib/src/widgets/text.dart

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,10 @@ class DefaultTextStyle extends InheritedTheme {
9999
/// [DefaultTextStyle] using the [DefaultTextStyle.new] constructor directly.
100100
/// See the source below for an example of how to do this (since that's
101101
/// essentially what this constructor does).
102+
///
103+
/// If a [textHeightBehavior] is provided, the existing configuration will be
104+
/// replaced compeletely. To retain part of the original [textHeightBehavior],
105+
/// manually obtain the ambient [DefaultTextStyle] using [DefaultTextStyle.of].
102106
static Widget merge({
103107
Key? key,
104108
TextStyle? style,
@@ -107,6 +111,7 @@ class DefaultTextStyle extends InheritedTheme {
107111
TextOverflow? overflow,
108112
int? maxLines,
109113
TextWidthBasis? textWidthBasis,
114+
TextHeightBehavior? textHeightBehavior,
110115
required Widget child,
111116
}) {
112117
return Builder(
@@ -120,6 +125,7 @@ class DefaultTextStyle extends InheritedTheme {
120125
overflow: overflow ?? parent.overflow,
121126
maxLines: maxLines ?? parent.maxLines,
122127
textWidthBasis: textWidthBasis ?? parent.textWidthBasis,
128+
textHeightBehavior: textHeightBehavior ?? parent.textHeightBehavior,
123129
child: child,
124130
);
125131
},

packages/flutter/test/widgets/text_test.dart

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,52 @@ import 'package:leak_tracker_flutter_testing/leak_tracker_flutter_testing.dart';
1414
import 'semantics_tester.dart';
1515

1616
void main() {
17+
testWidgets('DefaultTextStyle.merge correctly merges arguments', (WidgetTester tester) async {
18+
DefaultTextStyle defaultTextStyle = const DefaultTextStyle.fallback();
19+
20+
await tester.pumpWidget(
21+
Directionality(
22+
textDirection: TextDirection.ltr,
23+
child: DefaultTextStyle(
24+
style: const TextStyle(color: Colors.black, fontSize: 20),
25+
textAlign: TextAlign.left,
26+
softWrap: false,
27+
overflow: TextOverflow.ellipsis,
28+
maxLines: 2,
29+
textWidthBasis: TextWidthBasis.longestLine,
30+
textHeightBehavior: const TextHeightBehavior(applyHeightToFirstAscent: false),
31+
child: DefaultTextStyle.merge(
32+
style: const TextStyle(color: Colors.red, fontWeight: FontWeight.bold),
33+
textAlign: TextAlign.center,
34+
softWrap: true,
35+
overflow: TextOverflow.fade,
36+
maxLines: 3,
37+
textWidthBasis: TextWidthBasis.parent,
38+
textHeightBehavior: const TextHeightBehavior(applyHeightToLastDescent: false),
39+
child: Builder(
40+
builder: (BuildContext context) {
41+
defaultTextStyle = DefaultTextStyle.of(context);
42+
return const Text('Text');
43+
},
44+
),
45+
),
46+
),
47+
),
48+
);
49+
50+
expect(defaultTextStyle.style, const TextStyle(
51+
color: Colors.red,
52+
fontSize: 20,
53+
fontWeight: FontWeight.bold,
54+
));
55+
expect(defaultTextStyle.textAlign, TextAlign.center);
56+
expect(defaultTextStyle.softWrap, true);
57+
expect(defaultTextStyle.overflow, TextOverflow.fade);
58+
expect(defaultTextStyle.maxLines, 3);
59+
expect(defaultTextStyle.textWidthBasis, TextWidthBasis.parent);
60+
expect(defaultTextStyle.textHeightBehavior, const TextHeightBehavior(applyHeightToLastDescent: false));
61+
});
62+
1763
testWidgets('Text respects media query', (WidgetTester tester) async {
1864
await tester.pumpWidget(MediaQuery.withClampedTextScaling(
1965
minScaleFactor: 1.3,

0 commit comments

Comments
 (0)