Unverified Commit a02568b3 authored by Hans Muller's avatar Hans Muller Committed by GitHub

Added MediaQuery.textScaleFactorOf() (#17450)

parent 7809651c
...@@ -174,7 +174,7 @@ class _DemoItem extends StatelessWidget { ...@@ -174,7 +174,7 @@ class _DemoItem extends StatelessWidget {
Widget build(BuildContext context) { Widget build(BuildContext context) {
final ThemeData theme = Theme.of(context); final ThemeData theme = Theme.of(context);
final bool isDark = theme.brightness == Brightness.dark; final bool isDark = theme.brightness == Brightness.dark;
final double textScaleFactor = MediaQuery.of(context)?.textScaleFactor ?? 1.0; final double textScaleFactor = MediaQuery.textScaleFactorOf(context);
final List<Widget> titleChildren = <Widget>[ final List<Widget> titleChildren = <Widget>[
new Text( new Text(
......
...@@ -93,7 +93,7 @@ class _OptionsItem extends StatelessWidget { ...@@ -93,7 +93,7 @@ class _OptionsItem extends StatelessWidget {
@override @override
Widget build(BuildContext context) { Widget build(BuildContext context) {
final double textScaleFactor = MediaQuery.of(context)?.textScaleFactor ?? 1.0; final double textScaleFactor = MediaQuery.textScaleFactorOf(context);
return new MergeSemantics( return new MergeSemantics(
child: new Container( child: new Container(
......
...@@ -270,7 +270,7 @@ class CupertinoDialogAction extends StatelessWidget { ...@@ -270,7 +270,7 @@ class CupertinoDialogAction extends StatelessWidget {
style = style.copyWith(color: style.color.withOpacity(0.5)); style = style.copyWith(color: style.color.withOpacity(0.5));
} }
final double textScaleFactor = MediaQuery.of(context, nullOk: true)?.textScaleFactor ?? 1.0; final double textScaleFactor = MediaQuery.textScaleFactorOf(context);
return new GestureDetector( return new GestureDetector(
onTap: onPressed, onTap: onPressed,
behavior: HitTestBehavior.opaque, behavior: HitTestBehavior.opaque,
......
...@@ -684,7 +684,7 @@ class EditableTextState extends State<EditableText> with AutomaticKeepAliveClien ...@@ -684,7 +684,7 @@ class EditableTextState extends State<EditableText> with AutomaticKeepAliveClien
hasFocus: _hasFocus, hasFocus: _hasFocus,
maxLines: widget.maxLines, maxLines: widget.maxLines,
selectionColor: widget.selectionColor, selectionColor: widget.selectionColor,
textScaleFactor: widget.textScaleFactor ?? MediaQuery.of(context, nullOk: true)?.textScaleFactor ?? 1.0, textScaleFactor: widget.textScaleFactor ?? MediaQuery.textScaleFactorOf(context),
textAlign: widget.textAlign, textAlign: widget.textAlign,
textDirection: _textDirection, textDirection: _textDirection,
obscureText: widget.obscureText, obscureText: widget.obscureText,
......
...@@ -76,6 +76,11 @@ class MediaQueryData { ...@@ -76,6 +76,11 @@ class MediaQueryData {
/// ///
/// For example, if the text scale factor is 1.5, text will be 50% larger than /// For example, if the text scale factor is 1.5, text will be 50% larger than
/// the specified font size. /// the specified font size.
///
/// See also:
///
/// * [MediaQuery.textScaleFactorOf], a convenience method which returns the
/// textScaleFactor defined for a [BuildContext].
final double textScaleFactor; final double textScaleFactor;
/// The number of physical pixels on each side of the display rectangle into /// The number of physical pixels on each side of the display rectangle into
...@@ -402,6 +407,12 @@ class MediaQuery extends InheritedWidget { ...@@ -402,6 +407,12 @@ class MediaQuery extends InheritedWidget {
); );
} }
/// Returns textScaleFactor for the nearest MediaQuery ancestor or 1.0, if
/// no such ancestor exists.
static double textScaleFactorOf(BuildContext context) {
return MediaQuery.of(context, nullOk: true)?.textScaleFactor ?? 1.0;
}
@override @override
bool updateShouldNotify(MediaQuery oldWidget) => data != oldWidget.data; bool updateShouldNotify(MediaQuery oldWidget) => data != oldWidget.data;
......
...@@ -305,7 +305,7 @@ class Text extends StatelessWidget { ...@@ -305,7 +305,7 @@ class Text extends StatelessWidget {
textDirection: textDirection, // RichText uses Directionality.of to obtain a default if this is null. textDirection: textDirection, // RichText uses Directionality.of to obtain a default if this is null.
softWrap: softWrap ?? defaultTextStyle.softWrap, softWrap: softWrap ?? defaultTextStyle.softWrap,
overflow: overflow ?? defaultTextStyle.overflow, overflow: overflow ?? defaultTextStyle.overflow,
textScaleFactor: textScaleFactor ?? MediaQuery.of(context, nullOk: true)?.textScaleFactor ?? 1.0, textScaleFactor: textScaleFactor ?? MediaQuery.textScaleFactorOf(context),
maxLines: maxLines ?? defaultTextStyle.maxLines, maxLines: maxLines ?? defaultTextStyle.maxLines,
text: new TextSpan( text: new TextSpan(
style: effectiveTextStyle, style: effectiveTextStyle,
......
...@@ -165,4 +165,31 @@ void main() { ...@@ -165,4 +165,31 @@ void main() {
expect(unpadded.viewInsets, EdgeInsets.zero); expect(unpadded.viewInsets, EdgeInsets.zero);
expect(unpadded.alwaysUse24HourFormat, true); expect(unpadded.alwaysUse24HourFormat, true);
}); });
testWidgets('MediaQuery.textScaleFactorOf', (WidgetTester tester) async {
double outsideTextScaleFactor;
double insideTextScaleFactor;
await tester.pumpWidget(
new Builder(
builder: (BuildContext context) {
outsideTextScaleFactor = MediaQuery.textScaleFactorOf(context);
return new MediaQuery(
data: const MediaQueryData(
textScaleFactor: 4.0,
),
child: new Builder(
builder: (BuildContext context) {
insideTextScaleFactor = MediaQuery.textScaleFactorOf(context);
return new Container();
},
),
);
},
),
);
expect(outsideTextScaleFactor, 1.0);
expect(insideTextScaleFactor, 4.0);
});
} }
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment