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
This commit is contained in:
Joe Kawai
2024-08-16 07:00:15 +08:00
committed by GitHub
parent 81ee230f49
commit 5d59f8bb1a
2 changed files with 52 additions and 0 deletions

View File

@@ -99,6 +99,10 @@ class DefaultTextStyle extends InheritedTheme {
/// [DefaultTextStyle] using the [DefaultTextStyle.new] constructor directly.
/// See the source below for an example of how to do this (since that's
/// essentially what this constructor does).
///
/// If a [textHeightBehavior] is provided, the existing configuration will be
/// replaced compeletely. To retain part of the original [textHeightBehavior],
/// manually obtain the ambient [DefaultTextStyle] using [DefaultTextStyle.of].
static Widget merge({
Key? key,
TextStyle? style,
@@ -107,6 +111,7 @@ class DefaultTextStyle extends InheritedTheme {
TextOverflow? overflow,
int? maxLines,
TextWidthBasis? textWidthBasis,
TextHeightBehavior? textHeightBehavior,
required Widget child,
}) {
return Builder(
@@ -120,6 +125,7 @@ class DefaultTextStyle extends InheritedTheme {
overflow: overflow ?? parent.overflow,
maxLines: maxLines ?? parent.maxLines,
textWidthBasis: textWidthBasis ?? parent.textWidthBasis,
textHeightBehavior: textHeightBehavior ?? parent.textHeightBehavior,
child: child,
);
},

View File

@@ -14,6 +14,52 @@ import 'package:leak_tracker_flutter_testing/leak_tracker_flutter_testing.dart';
import 'semantics_tester.dart';
void main() {
testWidgets('DefaultTextStyle.merge correctly merges arguments', (WidgetTester tester) async {
DefaultTextStyle defaultTextStyle = const DefaultTextStyle.fallback();
await tester.pumpWidget(
Directionality(
textDirection: TextDirection.ltr,
child: DefaultTextStyle(
style: const TextStyle(color: Colors.black, fontSize: 20),
textAlign: TextAlign.left,
softWrap: false,
overflow: TextOverflow.ellipsis,
maxLines: 2,
textWidthBasis: TextWidthBasis.longestLine,
textHeightBehavior: const TextHeightBehavior(applyHeightToFirstAscent: false),
child: DefaultTextStyle.merge(
style: const TextStyle(color: Colors.red, fontWeight: FontWeight.bold),
textAlign: TextAlign.center,
softWrap: true,
overflow: TextOverflow.fade,
maxLines: 3,
textWidthBasis: TextWidthBasis.parent,
textHeightBehavior: const TextHeightBehavior(applyHeightToLastDescent: false),
child: Builder(
builder: (BuildContext context) {
defaultTextStyle = DefaultTextStyle.of(context);
return const Text('Text');
},
),
),
),
),
);
expect(defaultTextStyle.style, const TextStyle(
color: Colors.red,
fontSize: 20,
fontWeight: FontWeight.bold,
));
expect(defaultTextStyle.textAlign, TextAlign.center);
expect(defaultTextStyle.softWrap, true);
expect(defaultTextStyle.overflow, TextOverflow.fade);
expect(defaultTextStyle.maxLines, 3);
expect(defaultTextStyle.textWidthBasis, TextWidthBasis.parent);
expect(defaultTextStyle.textHeightBehavior, const TextHeightBehavior(applyHeightToLastDescent: false));
});
testWidgets('Text respects media query', (WidgetTester tester) async {
await tester.pumpWidget(MediaQuery.withClampedTextScaling(
minScaleFactor: 1.3,