From 505845c5ac5329ee8f8d6d2812dfc25d5e088220 Mon Sep 17 00:00:00 2001 From: LongCatIsLooong <31859944+LongCatIsLooong@users.noreply.github.com> Date: Fri, 26 Jan 2024 11:12:24 -0800 Subject: [PATCH] Remove textScaleFactor references from flutter/flutter (#142271) These should the the last remaining `MediaQueryData.textScaleFactor` and `TextScaler.textScaleFactor` references. --- .../demo/shrine/supplemental/product_card.dart | 7 +++++-- .../flutter_gallery/lib/gallery/home.dart | 7 ++++--- .../material/time_picker/show_time_picker.0.dart | 5 ++--- .../flutter/lib/src/material/range_slider.dart | 6 +++++- packages/flutter/lib/src/material/slider.dart | 11 +++++++---- .../flutter/lib/src/material/slider_theme.dart | 2 +- .../flutter/lib/src/widgets/editable_text.dart | 6 +----- packages/flutter/test/cupertino/nav_bar_test.dart | 10 ++++++---- .../flutter/test/cupertino/tab_scaffold_test.dart | 5 +++-- packages/flutter/test/material/dialog_test.dart | 5 +++-- .../test/material/dropdown_form_field_test.dart | 5 +++-- .../test/material/elevated_button_test.dart | 14 ++++++-------- .../flutter/test/material/filled_button_test.dart | 14 ++++++-------- .../test/material/navigation_rail_test.dart | 15 +++++++++------ .../test/material/outlined_button_test.dart | 14 ++++++-------- .../flutter/test/material/text_button_test.dart | 14 ++++++-------- 16 files changed, 73 insertions(+), 67 deletions(-) diff --git a/dev/integration_tests/flutter_gallery/lib/demo/shrine/supplemental/product_card.dart b/dev/integration_tests/flutter_gallery/lib/demo/shrine/supplemental/product_card.dart index 5a32001fed..6b2f611c4c 100644 --- a/dev/integration_tests/flutter_gallery/lib/demo/shrine/supplemental/product_card.dart +++ b/dev/integration_tests/flutter_gallery/lib/demo/shrine/supplemental/product_card.dart @@ -33,6 +33,10 @@ class ProductCard extends StatelessWidget { fit: BoxFit.cover, ); + // The fontSize to use for computing the heuristic UI scaling factor. + const double defaultFontSize = 14.0; + final double containerScaingFactor = MediaQuery.textScalerOf(context).scale(defaultFontSize) / defaultFontSize; + return ScopedModelDescendant( builder: (BuildContext context, Widget? child, AppStateModel model) { return GestureDetector( @@ -52,8 +56,7 @@ class ProductCard extends StatelessWidget { child: imageWidget, ), SizedBox( - // ignore: deprecated_member_use, https://github.com/flutter/flutter/issues/128825 - height: kTextBoxHeight * MediaQuery.textScalerOf(context).textScaleFactor, + height: kTextBoxHeight * containerScaingFactor, width: 121.0, child: Column( mainAxisAlignment: MainAxisAlignment.end, diff --git a/dev/integration_tests/flutter_gallery/lib/gallery/home.dart b/dev/integration_tests/flutter_gallery/lib/gallery/home.dart index 4a5f01fede..756f2c7321 100644 --- a/dev/integration_tests/flutter_gallery/lib/gallery/home.dart +++ b/dev/integration_tests/flutter_gallery/lib/gallery/home.dart @@ -178,8 +178,9 @@ class _DemoItem extends StatelessWidget { Widget build(BuildContext context) { final ThemeData theme = Theme.of(context); final bool isDark = theme.brightness == Brightness.dark; - // ignore: deprecated_member_use, https://github.com/flutter/flutter/issues/128825 - final double textScaleFactor = MediaQuery.textScalerOf(context).textScaleFactor; + // The fontSize to use for computing the heuristic UI scaling factor. + const double defaultFontSize = 14.0; + final double containerScaingFactor = MediaQuery.textScalerOf(context).scale(defaultFontSize) / defaultFontSize; return RawMaterialButton( splashColor: theme.primaryColor.withOpacity(0.12), highlightColor: Colors.transparent, @@ -187,7 +188,7 @@ class _DemoItem extends StatelessWidget { _launchDemo(context); }, child: Container( - constraints: BoxConstraints(minHeight: _kDemoItemHeight * textScaleFactor), + constraints: BoxConstraints(minHeight: _kDemoItemHeight * containerScaingFactor), child: Row( children: [ Container( diff --git a/examples/api/lib/material/time_picker/show_time_picker.0.dart b/examples/api/lib/material/time_picker/show_time_picker.0.dart index d7d05702d6..e9727d99ad 100644 --- a/examples/api/lib/material/time_picker/show_time_picker.0.dart +++ b/examples/api/lib/material/time_picker/show_time_picker.0.dart @@ -126,11 +126,10 @@ class _TimePickerOptionsState extends State { children: [ Expanded( child: GridView( - gridDelegate: SliverGridDelegateWithMaxCrossAxisExtent( + gridDelegate: const SliverGridDelegateWithMaxCrossAxisExtent( maxCrossAxisExtent: 350, mainAxisSpacing: 4, - // ignore: deprecated_member_use, https://github.com/flutter/flutter/issues/128825 - mainAxisExtent: 200 * MediaQuery.textScalerOf(context).textScaleFactor, + mainAxisExtent: 200, crossAxisSpacing: 4, ), children: [ diff --git a/packages/flutter/lib/src/material/range_slider.dart b/packages/flutter/lib/src/material/range_slider.dart index 3aafe10723..2f8c742549 100644 --- a/packages/flutter/lib/src/material/range_slider.dart +++ b/packages/flutter/lib/src/material/range_slider.dart @@ -671,6 +671,10 @@ class _RangeSliderState extends State with TickerProviderStateMixin // in slider.dart. Size screenSize() => MediaQuery.sizeOf(context); + final double fontSize = sliderTheme.valueIndicatorTextStyle?.fontSize ?? kDefaultFontSize; + final double fontSizeToScale = fontSize == 0.0 ? kDefaultFontSize : fontSize; + final double effectiveTextScale = MediaQuery.textScalerOf(context).scale(fontSizeToScale) / fontSizeToScale; + return FocusableActionDetector( enabled: _enabled, onShowHoverHighlight: _handleHoverChanged, @@ -683,7 +687,7 @@ class _RangeSliderState extends State with TickerProviderStateMixin divisions: widget.divisions, labels: widget.labels, sliderTheme: sliderTheme, - textScaleFactor: MediaQuery.of(context).textScaleFactor, + textScaleFactor: effectiveTextScale, screenSize: screenSize(), onChanged: _enabled && (widget.max > widget.min) ? _handleChanged : null, onChangeStart: widget.onChangeStart != null ? _handleDragStart : null, diff --git a/packages/flutter/lib/src/material/slider.dart b/packages/flutter/lib/src/material/slider.dart index f2075f7c9c..6204d61c2e 100644 --- a/packages/flutter/lib/src/material/slider.dart +++ b/packages/flutter/lib/src/material/slider.dart @@ -882,13 +882,16 @@ class _SliderState extends State with TickerProviderStateMixin { shortcutMap = _traditionalNavShortcutMap; } - final double textScaleFactor = theme.useMaterial3 + final double fontSize = sliderTheme.valueIndicatorTextStyle?.fontSize ?? kDefaultFontSize; + final double fontSizeToScale = fontSize == 0.0 ? kDefaultFontSize : fontSize; + final TextScaler textScaler = theme.useMaterial3 // TODO(tahatesser): This is an eye-balled value. // This needs to be updated when accessibility // guidelines are available on the material specs page // https://m3.material.io/components/sliders/accessibility. - ? MediaQuery.textScalerOf(context).clamp(maxScaleFactor: 1.3).textScaleFactor - : MediaQuery.textScalerOf(context).textScaleFactor; + ? MediaQuery.textScalerOf(context).clamp(maxScaleFactor: 1.3) + : MediaQuery.textScalerOf(context); + final double effectiveTextScale = textScaler.scale(fontSizeToScale) / fontSizeToScale; return Semantics( container: true, @@ -912,7 +915,7 @@ class _SliderState extends State with TickerProviderStateMixin { divisions: widget.divisions, label: widget.label, sliderTheme: sliderTheme, - textScaleFactor: textScaleFactor, + textScaleFactor: effectiveTextScale, screenSize: screenSize(), onChanged: (widget.onChanged != null) && (widget.max > widget.min) ? _handleChanged : null, onChangeStart: _handleDragStart, diff --git a/packages/flutter/lib/src/material/slider_theme.dart b/packages/flutter/lib/src/material/slider_theme.dart index 4b8a623d5d..7d20948975 100644 --- a/packages/flutter/lib/src/material/slider_theme.dart +++ b/packages/flutter/lib/src/material/slider_theme.dart @@ -902,7 +902,7 @@ abstract class SliderComponentShape { /// The `textScaleFactor` argument can be used to determine whether the /// component should paint larger or smaller, depending on whether /// [textScaleFactor] is greater than 1 for larger, and between 0 and 1 for - /// smaller. It usually comes from [MediaQueryData.textScaleFactor]. + /// smaller. It's usually computed from [MediaQueryData.textScaler]. /// {@endtemplate} /// /// {@template flutter.material.SliderComponentShape.paint.sizeWithOverflow} diff --git a/packages/flutter/lib/src/widgets/editable_text.dart b/packages/flutter/lib/src/widgets/editable_text.dart index 6832e39c9b..9fbb650481 100644 --- a/packages/flutter/lib/src/widgets/editable_text.dart +++ b/packages/flutter/lib/src/widgets/editable_text.dart @@ -5405,11 +5405,7 @@ class _ScribblePlaceholder extends WidgetSpan { if (hasStyle) { builder.pushStyle(style!.getTextStyle(textScaler: textScaler)); } - builder.addPlaceholder( - size.width * textScaler.textScaleFactor, - size.height * textScaler.textScaleFactor, - alignment, - ); + builder.addPlaceholder(size.width, size.height, alignment); if (hasStyle) { builder.pop(); } diff --git a/packages/flutter/test/cupertino/nav_bar_test.dart b/packages/flutter/test/cupertino/nav_bar_test.dart index 241a854b22..d757445c55 100644 --- a/packages/flutter/test/cupertino/nav_bar_test.dart +++ b/packages/flutter/test/cupertino/nav_bar_test.dart @@ -1197,8 +1197,9 @@ void main() { await tester.pumpWidget( CupertinoApp( home: Builder(builder: (BuildContext context) { - return MediaQuery( - data: MediaQuery.of(context).copyWith(textScaleFactor: 99), + return MediaQuery.withClampedTextScaling( + minScaleFactor: 99, + maxScaleFactor: 99, child: const CupertinoPageScaffold( child: CustomScrollView( slivers: [ @@ -1243,8 +1244,9 @@ void main() { tester.state(find.byType(Navigator)).push(CupertinoPageRoute( title: 'title', builder: (BuildContext context) { - return MediaQuery( - data: MediaQuery.of(context).copyWith(textScaleFactor: 99), + return MediaQuery.withClampedTextScaling( + minScaleFactor: 99, + maxScaleFactor: 99, child: const CupertinoPageScaffold( child: CustomScrollView( slivers: [ diff --git a/packages/flutter/test/cupertino/tab_scaffold_test.dart b/packages/flutter/test/cupertino/tab_scaffold_test.dart index 938fb83c2e..ff0ef53f56 100644 --- a/packages/flutter/test/cupertino/tab_scaffold_test.dart +++ b/packages/flutter/test/cupertino/tab_scaffold_test.dart @@ -1098,8 +1098,9 @@ void main() { await tester.pumpWidget( CupertinoApp( home: Builder(builder: (BuildContext context) { - return MediaQuery( - data: MediaQuery.of(context).copyWith(textScaleFactor: 99), + return MediaQuery.withClampedTextScaling( + minScaleFactor: 99, + maxScaleFactor: 99, child: CupertinoTabScaffold( tabBar: CupertinoTabBar( items: List.generate( diff --git a/packages/flutter/test/material/dialog_test.dart b/packages/flutter/test/material/dialog_test.dart index d3b47c8704..ac530f8524 100644 --- a/packages/flutter/test/material/dialog_test.dart +++ b/packages/flutter/test/material/dialog_test.dart @@ -30,8 +30,9 @@ MaterialApp _buildAppWithDialog( context: context, traversalEdgeBehavior: traversalEdgeBehavior, builder: (BuildContext context) { - return MediaQuery( - data: MediaQuery.of(context).copyWith(textScaleFactor: textScaleFactor), + return MediaQuery.withClampedTextScaling( + minScaleFactor: textScaleFactor, + maxScaleFactor: textScaleFactor, child: dialog, ); }, diff --git a/packages/flutter/test/material/dropdown_form_field_test.dart b/packages/flutter/test/material/dropdown_form_field_test.dart index 7d56471733..d2d0b301e1 100644 --- a/packages/flutter/test/material/dropdown_form_field_test.dart +++ b/packages/flutter/test/material/dropdown_form_field_test.dart @@ -575,8 +575,9 @@ void main() { TestApp( textDirection: TextDirection.ltr, child: Builder( - builder: (BuildContext context) => MediaQuery( - data: MediaQuery.of(context).copyWith(textScaleFactor: 3.0), + builder: (BuildContext context) => MediaQuery.withClampedTextScaling( + minScaleFactor: 3.0, + maxScaleFactor: 3.0, child: Material( child: Center( child: DropdownButtonFormField( diff --git a/packages/flutter/test/material/elevated_button_test.dart b/packages/flutter/test/material/elevated_button_test.dart index 7f3c393214..86ea05da5d 100644 --- a/packages/flutter/test/material/elevated_button_test.dart +++ b/packages/flutter/test/material/elevated_button_test.dart @@ -1058,10 +1058,9 @@ void main() { ), home: Builder( builder: (BuildContext context) { - return MediaQuery( - data: MediaQuery.of(context).copyWith( - textScaleFactor: textScaleFactor, - ), + return MediaQuery.withClampedTextScaling( + minScaleFactor: textScaleFactor, + maxScaleFactor: textScaleFactor, child: Directionality( textDirection: textDirection, child: Scaffold( @@ -1225,10 +1224,9 @@ void main() { theme: ThemeData.from(colorScheme: const ColorScheme.light()), home: Builder( builder: (BuildContext context) { - return MediaQuery( - data: MediaQuery.of(context).copyWith( - textScaleFactor: 2, - ), + return MediaQuery.withClampedTextScaling( + minScaleFactor: 2, + maxScaleFactor: 2, child: Scaffold( body: Center( child: ElevatedButton( diff --git a/packages/flutter/test/material/filled_button_test.dart b/packages/flutter/test/material/filled_button_test.dart index ce707f42e2..0a136b4bde 100644 --- a/packages/flutter/test/material/filled_button_test.dart +++ b/packages/flutter/test/material/filled_button_test.dart @@ -1203,10 +1203,9 @@ void main() { ), home: Builder( builder: (BuildContext context) { - return MediaQuery( - data: MediaQuery.of(context).copyWith( - textScaleFactor: textScaleFactor, - ), + return MediaQuery.withClampedTextScaling( + minScaleFactor: textScaleFactor, + maxScaleFactor: textScaleFactor, child: Directionality( textDirection: textDirection, child: Scaffold( @@ -1339,10 +1338,9 @@ void main() { theme: ThemeData.from(colorScheme: const ColorScheme.light()), home: Builder( builder: (BuildContext context) { - return MediaQuery( - data: MediaQuery.of(context).copyWith( - textScaleFactor: 2, - ), + return MediaQuery.withClampedTextScaling( + minScaleFactor: 2, + maxScaleFactor: 2, child: Scaffold( body: Center( child: FilledButton( diff --git a/packages/flutter/test/material/navigation_rail_test.dart b/packages/flutter/test/material/navigation_rail_test.dart index 916b659429..7085dec5de 100644 --- a/packages/flutter/test/material/navigation_rail_test.dart +++ b/packages/flutter/test/material/navigation_rail_test.dart @@ -1856,8 +1856,9 @@ void main() { return Scaffold( body: Row( children: [ - MediaQuery( - data: MediaQuery.of(context).copyWith(textScaleFactor: 3.0), + MediaQuery.withClampedTextScaling( + minScaleFactor: 3.0, + maxScaleFactor: 3.0, child: NavigationRail( selectedIndex: 0, destinations: const [ @@ -5135,8 +5136,9 @@ void main() { return Scaffold( body: Row( children: [ - MediaQuery( - data: MediaQuery.of(context).copyWith(textScaleFactor: 3.0), + MediaQuery.withClampedTextScaling( + minScaleFactor: 3.0, + maxScaleFactor: 3.0, child: NavigationRail( selectedIndex: 0, destinations: const [ @@ -5511,8 +5513,9 @@ Future _pumpNavigationRail( ), home: Builder( builder: (BuildContext context) { - return MediaQuery( - data: MediaQuery.of(context).copyWith(textScaleFactor: textScaleFactor), + return MediaQuery.withClampedTextScaling( + minScaleFactor: textScaleFactor, + maxScaleFactor: textScaleFactor, child: Scaffold( body: Row( children: [ diff --git a/packages/flutter/test/material/outlined_button_test.dart b/packages/flutter/test/material/outlined_button_test.dart index 69832d3bfd..588b09bd4b 100644 --- a/packages/flutter/test/material/outlined_button_test.dart +++ b/packages/flutter/test/material/outlined_button_test.dart @@ -1310,10 +1310,9 @@ void main() { ), home: Builder( builder: (BuildContext context) { - return MediaQuery( - data: MediaQuery.of(context).copyWith( - textScaleFactor: textScaleFactor, - ), + return MediaQuery.withClampedTextScaling( + minScaleFactor: textScaleFactor, + maxScaleFactor: textScaleFactor, child: Directionality( textDirection: textDirection, child: Scaffold( @@ -1449,10 +1448,9 @@ void main() { theme: ThemeData(useMaterial3: false), home: Builder( builder: (BuildContext context) { - return MediaQuery( - data: MediaQuery.of(context).copyWith( - textScaleFactor: 2, - ), + return MediaQuery.withClampedTextScaling( + minScaleFactor: 2, + maxScaleFactor: 2, child: Scaffold( body: Center( child: OutlinedButton( diff --git a/packages/flutter/test/material/text_button_test.dart b/packages/flutter/test/material/text_button_test.dart index 809c1c52cb..d62a820920 100644 --- a/packages/flutter/test/material/text_button_test.dart +++ b/packages/flutter/test/material/text_button_test.dart @@ -1084,10 +1084,9 @@ void main() { ), home: Builder( builder: (BuildContext context) { - return MediaQuery( - data: MediaQuery.of(context).copyWith( - textScaleFactor: textScaleFactor, - ), + return MediaQuery.withClampedTextScaling( + minScaleFactor: textScaleFactor, + maxScaleFactor: textScaleFactor, child: Directionality( textDirection: textDirection, child: Scaffold( @@ -1226,10 +1225,9 @@ void main() { theme: ThemeData.from(colorScheme: const ColorScheme.light()), home: Builder( builder: (BuildContext context) { - return MediaQuery( - data: MediaQuery.of(context).copyWith( - textScaleFactor: 2, - ), + return MediaQuery.withClampedTextScaling( + minScaleFactor: 2, + maxScaleFactor: 2, child: Scaffold( body: Center( child: TextButton(