Skip to content

Commit b2e22d3

Browse files
Replaces textScaleFactor with TextScaler (#128522)
Deprecate `textScaleFactor` in favor of `textScaler`, in preparation for Android 14 [Non-linear font scaling to 200%](https://developer.android.com/about/versions/14/features#non-linear-font-scaling). The `TextScaler` class can be moved to `dart:ui` in the future, if we decide to use the Android platform API or AndroidX to get the scaling curve instead of hard coding the curve in the framework. I haven't put the Flutter version in the deprecation message so the analyzer checks are failing. Will do so after I finish the migration guide. **Why `TextScaler.textScaleFactor`** The author of a `TextScaler` subclass should provide a fallback `textScaleFactor`. By making `TextScaler` also contain the `textScaleFactor` information it also makes it easier to migrate: if a widget overrides `MediaQueryData.textScaler` in the tree, for unmigrated widgets in the subtree it would also have to override `MediaQueryData.textScaleFactor`, and that makes it difficult to remove `MediaQueryData.textScaleFactor` in the future. ## A full list of affected APIs in this PR Deprecated: The method/getter/setter/argument is annotated with a `@Deprecated()` annotation in this PR, and the caller should replace it with `textScaler` instead. Unless otherwise specified there will be a Flutter fix available to help with migration but it's still recommended to migrate case-by-case. **Replaced**: The method this `textScaleFactor` argument belongs to is rarely called directly by user code and is not overridden by any of the registered custom tests, so the argument is directly replaced by `TextScaler`. **To Be Deprecated**: The method/getter/setter/argument can't be deprecated in this PR because a registered customer test depends on it and a Flutter fix isn't available (or the test was run without applying flutter fixes first). This method/getter/setter/argument will be deprecated in a followup PR once the registered test is migrated. ### `Painting` Library | Affected API | State of `textScaleFactor` | Comment | | --- | --- | --- | | `InlineSpan.build({ double textScaleFactor = 1.0 })` argument | **Replaced** | | | `TextStyle.getParagraphStyle({ double TextScaleFactor = 1.0 })` argument | **Replaced** | | | `TextStyle.getTextStyle({ double TextScaleFactor = 1.0 })` argument| Deprecated | Can't replace: https://github.com/superlistapp/super_editor/blob/c47fd38dca4b7f43611690913b551a1773c563d7/super_editor/lib/src/infrastructure/super_textfield/desktop/desktop_textfield.dart#L1903-L1905| | `TextPainter({ double TextScaleFactor = 1.0 })` constructor argument | Deprecated | | | `TextPainter.textScaleFactor` getter and setter | Deprecated | No Flutter Fix, not expressible yet | | `TextPainter.computeWidth({ double TextScaleFactor = 1.0 })` argument | Deprecated | | | `TextPainter.computeMaxIntrinsicWidth({ double TextScaleFactor = 1.0 })` argument | Deprecated | | ### `Rendering` Library | Affected API | State of `textScaleFactor` | Comment | | --- | --- | --- | | `RenderEditable({ double TextScaleFactor = 1.0 })` constructor argument | Deprecated | | | `RenderEditable.textScaleFactor` getter and setter | Deprecated | No Flutter Fix, not expressible yet | | `RenderParagraph({ double TextScaleFactor = 1.0 })` constructor argument | Deprecated | | | `RenderParagraph.textScaleFactor` getter and setter | Deprecated | No Flutter Fix, not expressible yet | ### `Widgets` Library | Affected API | State of `textScaleFactor` | Comment | | --- | --- | --- | | `MediaQueryData({ double TextScaleFactor = 1.0 })` constructor argument | **To Be Deprecated** | https://github.com/flutter/packages/blob/cd7b93532e5cb605a42735e20f1de70fc00adae7/packages/flutter_markdown/test/text_scale_factor_test.dart#LL39C21-L39C35 | | `MediaQueryData.textScaleFactor` getter | Deprecated | | | `MediaQueryData.copyWith({ double? TextScaleFactor })` argument | Deprecated | | | `MediaQuery.maybeTextScaleFactorOf(BuildContext context)` static method | Deprecated | No Flutter Fix, not expressible yet | | `MediaQuery.textScaleFactorOf(BuildContext context)` static method | **To Be Deprecated** | https://github.com/flutter/packages/blob/cd7b93532e5cb605a42735e20f1de70fc00adae7/packages/flutter_markdown/lib/src/_functions_io.dart#L68-L70, No Flutter Fix, not expressible yet | | `RichText({ double TextScaleFactor = 1.0 })` constructor argument | **To Be Deprecated** | https://github.com/flutter/packages/blob/cd7b93532e5cb605a42735e20f1de70fc00adae7/packages/flutter_markdown/lib/src/builder.dart#L829-L843 | | `RichText.textScaleFactor` getter | **To Be Deprecated** | A constructor argument can't be deprecated right away| | `Text({ double? TextScaleFactor = 1.0 })` constructor argument | **To Be Deprecated** | https://github.com/flutter/packages/blob/914d120da12fba458c020210727831c31bd71041/packages/rfw/lib/src/flutter/core_widgets.dart#L647 , No Flutter Fix because of dart-lang/sdk#52664 | | `Text.rich({ double? TextScaleFactor = 1.0 })` constructor argument | **To Be Deprecated** | The default constructor has an argument that can't be deprecated right away. No Flutter Fix because of dart-lang/sdk#52664 | | `Text.textScaleFactor` getter | **To Be Deprecated** | A constructor argument can't be deprecated right away | | `EditableText({ double? TextScaleFactor = 1.0 })` constructor argument | Deprecated | No Flutter Fix because of dart-lang/sdk#52664 | | `EditableText.textScaleFactor` getter | Deprecated | | ### `Material` Library | Affected API | State of `textScaleFactor` | Comment | | --- | --- | --- | | `SelectableText({ double? TextScaleFactor = 1.0 })` constructor argument | **To Be Deprecated** | https://github.com/flutter/packages/blob/cd7b93532e5cb605a42735e20f1de70fc00adae7/packages/flutter_markdown/lib/src/builder.dart#L829-L843, No Flutter Fix because of dart-lang/sdk#52664 | | `SelectableText.rich({ double? TextScaleFactor = 1.0 })` constructor argument | **To Be Deprecated** | The default constructor has an argument that can't be deprecated right away. No Flutter Fix because of dart-lang/sdk#52664 | | `SelectableText.textScaleFactor` getter | **To Be Deprecated** | A constructor argument can't be deprecated right away | A lot of material widgets (`Slider`, `RangeSlider`, `TimePicker`, and different types of buttons) also change their layout based on `textScaleFactor`. These need to be handled in a case-by-case fashion and will be migrated in follow-up PRs.
1 parent 0d1cc33 commit b2e22d3

File tree

85 files changed

+1696
-457
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

85 files changed

+1696
-457
lines changed

dev/benchmarks/macrobenchmarks/lib/src/web/bench_pageview_scroll_linethrough.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ class _CustomPainter extends CustomPainter {
9595
yPosition = viewPadding;
9696
_textPainter.textDirection = TextDirection.ltr;
9797
_textPainter.textWidthBasis = TextWidthBasis.longestLine;
98-
_textPainter.textScaleFactor = 1;
98+
_textPainter.textScaler = TextScaler.noScaling;
9999
const TextStyle textStyle =
100100
TextStyle(color: Colors.black87, fontSize: 13, fontFamily: 'Roboto');
101101

dev/integration_tests/flutter_gallery/lib/demo/shrine/supplemental/product_card.dart

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,8 @@ class ProductCard extends StatelessWidget {
5252
child: imageWidget,
5353
),
5454
SizedBox(
55-
height: kTextBoxHeight * MediaQuery.of(context).textScaleFactor,
55+
// ignore: deprecated_member_use, https://github.com/flutter/flutter/issues/128825
56+
height: kTextBoxHeight * MediaQuery.textScalerOf(context).textScaleFactor,
5657
width: 121.0,
5758
child: Column(
5859
mainAxisAlignment: MainAxisAlignment.end,

dev/integration_tests/flutter_gallery/lib/gallery/app.dart

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -104,10 +104,10 @@ class _GalleryAppState extends State<GalleryApp> {
104104
Widget _applyTextScaleFactor(Widget child) {
105105
return Builder(
106106
builder: (BuildContext context) {
107-
return MediaQuery(
108-
data: MediaQuery.of(context).copyWith(
109-
textScaleFactor: _options!.textScaleFactor!.scale,
110-
),
107+
final double? textScaleFactor = _options!.textScaleFactor!.scale;
108+
return MediaQuery.withClampedTextScaling(
109+
minScaleFactor: textScaleFactor ?? 0.0,
110+
maxScaleFactor: textScaleFactor ?? double.infinity,
111111
child: child,
112112
);
113113
},

dev/integration_tests/flutter_gallery/lib/gallery/home.dart

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -178,7 +178,8 @@ class _DemoItem extends StatelessWidget {
178178
Widget build(BuildContext context) {
179179
final ThemeData theme = Theme.of(context);
180180
final bool isDark = theme.brightness == Brightness.dark;
181-
final double textScaleFactor = MediaQuery.textScaleFactorOf(context);
181+
// ignore: deprecated_member_use, https://github.com/flutter/flutter/issues/128825
182+
final double textScaleFactor = MediaQuery.textScalerOf(context).textScaleFactor;
182183
return RawMaterialButton(
183184
splashColor: theme.primaryColor.withOpacity(0.12),
184185
highlightColor: Colors.transparent,

dev/integration_tests/flutter_gallery/lib/gallery/options.dart

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,8 @@ class _OptionsItem extends StatelessWidget {
100100

101101
@override
102102
Widget build(BuildContext context) {
103-
final double textScaleFactor = MediaQuery.textScaleFactorOf(context);
103+
// ignore: deprecated_member_use, https://github.com/flutter/flutter/issues/128825
104+
final double textScaleFactor = MediaQuery.textScalerOf(context).textScaleFactor;
104105

105106
return MergeSemantics(
106107
child: Container(

dev/manual_tests/lib/density.dart

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -621,14 +621,17 @@ class _MyHomePageState extends State<MyHomePage> {
621621
data: Theme.of(context).copyWith(visualDensity: _model.density),
622622
child: Directionality(
623623
textDirection: _model.rtl ? TextDirection.rtl : TextDirection.ltr,
624-
child: MediaQuery(
625-
data: MediaQuery.of(context).copyWith(textScaleFactor: _model.size),
626-
child: SizedBox.expand(
627-
child: ListView(
628-
children: tiles,
624+
child: Builder(builder: (BuildContext context) {
625+
final MediaQueryData mediaQueryData = MediaQuery.of(context);
626+
return MediaQuery(
627+
data: mediaQueryData.copyWith(textScaler: TextScaler.linear(_model.size)),
628+
child: SizedBox.expand(
629+
child: ListView(
630+
children: tiles,
631+
),
629632
),
630-
),
631-
),
633+
);
634+
}),
632635
),
633636
),
634637
),

dev/tools/gen_defaults/lib/action_chip_template.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ class _${blockName}DefaultsM3 extends ChipThemeData {
9191
EdgeInsetsGeometry? get labelPadding => EdgeInsets.lerp(
9292
const EdgeInsets.symmetric(horizontal: 8.0),
9393
const EdgeInsets.symmetric(horizontal: 4.0),
94-
clampDouble(MediaQuery.textScaleFactorOf(context) - 1.0, 0.0, 1.0),
94+
clampDouble(MediaQuery.textScalerOf(context).textScaleFactor - 1.0, 0.0, 1.0),
9595
)!;
9696
}
9797
''';

dev/tools/gen_defaults/lib/chip_template.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ class _${blockName}DefaultsM3 extends ChipThemeData {
7070
EdgeInsetsGeometry? get labelPadding => EdgeInsets.lerp(
7171
const EdgeInsets.symmetric(horizontal: 8.0),
7272
const EdgeInsets.symmetric(horizontal: 4.0),
73-
clampDouble(MediaQuery.textScaleFactorOf(context) - 1.0, 0.0, 1.0),
73+
clampDouble(MediaQuery.textScalerOf(context).textScaleFactor - 1.0, 0.0, 1.0),
7474
)!;
7575
}
7676
''';

dev/tools/gen_defaults/lib/filter_chip_template.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ class _${blockName}DefaultsM3 extends ChipThemeData {
108108
EdgeInsetsGeometry? get labelPadding => EdgeInsets.lerp(
109109
const EdgeInsets.symmetric(horizontal: 8.0),
110110
const EdgeInsets.symmetric(horizontal: 4.0),
111-
clampDouble(MediaQuery.textScaleFactorOf(context) - 1.0, 0.0, 1.0),
111+
clampDouble(MediaQuery.textScalerOf(context).textScaleFactor - 1.0, 0.0, 1.0),
112112
)!;
113113
}
114114
''';

dev/tools/gen_defaults/lib/input_chip_template.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ class _${blockName}DefaultsM3 extends ChipThemeData {
8585
EdgeInsetsGeometry? get labelPadding => EdgeInsets.lerp(
8686
const EdgeInsets.symmetric(horizontal: 8.0),
8787
const EdgeInsets.symmetric(horizontal: 4.0),
88-
clampDouble(MediaQuery.textScaleFactorOf(context) - 1.0, 0.0, 1.0),
88+
clampDouble(MediaQuery.textScalerOf(context).textScaleFactor - 1.0, 0.0, 1.0),
8989
)!;
9090
}
9191
''';

0 commit comments

Comments
 (0)