diff --git a/packages/flutter/lib/src/widgets/text.dart b/packages/flutter/lib/src/widgets/text.dart index df5c8deff0..fbba170d4a 100644 --- a/packages/flutter/lib/src/widgets/text.dart +++ b/packages/flutter/lib/src/widgets/text.dart @@ -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, ); }, diff --git a/packages/flutter/test/widgets/text_test.dart b/packages/flutter/test/widgets/text_test.dart index cde4879eea..e38fa88c04 100644 --- a/packages/flutter/test/widgets/text_test.dart +++ b/packages/flutter/test/widgets/text_test.dart @@ -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,