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 {
Widget build(BuildContext context) {
final ThemeData theme = Theme.of(context);
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>[
new Text(
......
......@@ -93,7 +93,7 @@ class _OptionsItem extends StatelessWidget {
@override
Widget build(BuildContext context) {
final double textScaleFactor = MediaQuery.of(context)?.textScaleFactor ?? 1.0;
final double textScaleFactor = MediaQuery.textScaleFactorOf(context);
return new MergeSemantics(
child: new Container(
......
......@@ -270,7 +270,7 @@ class CupertinoDialogAction extends StatelessWidget {
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(
onTap: onPressed,
behavior: HitTestBehavior.opaque,
......
......@@ -684,7 +684,7 @@ class EditableTextState extends State<EditableText> with AutomaticKeepAliveClien
hasFocus: _hasFocus,
maxLines: widget.maxLines,
selectionColor: widget.selectionColor,
textScaleFactor: widget.textScaleFactor ?? MediaQuery.of(context, nullOk: true)?.textScaleFactor ?? 1.0,
textScaleFactor: widget.textScaleFactor ?? MediaQuery.textScaleFactorOf(context),
textAlign: widget.textAlign,
textDirection: _textDirection,
obscureText: widget.obscureText,
......
......@@ -76,6 +76,11 @@ class MediaQueryData {
///
/// For example, if the text scale factor is 1.5, text will be 50% larger than
/// the specified font size.
///
/// See also:
///
/// * [MediaQuery.textScaleFactorOf], a convenience method which returns the
/// textScaleFactor defined for a [BuildContext].
final double textScaleFactor;
/// The number of physical pixels on each side of the display rectangle into
......@@ -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
bool updateShouldNotify(MediaQuery oldWidget) => data != oldWidget.data;
......
......@@ -305,7 +305,7 @@ class Text extends StatelessWidget {
textDirection: textDirection, // RichText uses Directionality.of to obtain a default if this is null.
softWrap: softWrap ?? defaultTextStyle.softWrap,
overflow: overflow ?? defaultTextStyle.overflow,
textScaleFactor: textScaleFactor ?? MediaQuery.of(context, nullOk: true)?.textScaleFactor ?? 1.0,
textScaleFactor: textScaleFactor ?? MediaQuery.textScaleFactorOf(context),
maxLines: maxLines ?? defaultTextStyle.maxLines,
text: new TextSpan(
style: effectiveTextStyle,
......
......@@ -74,50 +74,50 @@ void main() {
expect(copied.alwaysUse24HourFormat, true);
});
testWidgets('MediaQuery.removePadding removes specified padding', (WidgetTester tester) async {
const Size size = const Size(2.0, 4.0);
const double devicePixelRatio = 2.0;
const double textScaleFactor = 1.2;
const EdgeInsets padding = const EdgeInsets.only(top: 1.0, right: 2.0, left: 3.0, bottom: 4.0);
const EdgeInsets viewInsets = const EdgeInsets.only(top: 5.0, right: 6.0, left: 7.0, bottom: 8.0);
testWidgets('MediaQuery.removePadding removes specified padding', (WidgetTester tester) async {
const Size size = const Size(2.0, 4.0);
const double devicePixelRatio = 2.0;
const double textScaleFactor = 1.2;
const EdgeInsets padding = const EdgeInsets.only(top: 1.0, right: 2.0, left: 3.0, bottom: 4.0);
const EdgeInsets viewInsets = const EdgeInsets.only(top: 5.0, right: 6.0, left: 7.0, bottom: 8.0);
MediaQueryData unpadded;
await tester.pumpWidget(
new MediaQuery(
data: const MediaQueryData(
size: size,
devicePixelRatio: devicePixelRatio,
textScaleFactor: textScaleFactor,
padding: padding,
viewInsets: viewInsets,
alwaysUse24HourFormat: true,
),
child: new Builder(
builder: (BuildContext context) {
return new MediaQuery.removePadding(
context: context,
removeLeft: true,
removeTop: true,
removeRight: true,
removeBottom: true,
child: new Builder(
builder: (BuildContext context) {
unpadded = MediaQuery.of(context);
return new Container();
}
),
);
},
),
)
);
MediaQueryData unpadded;
await tester.pumpWidget(
new MediaQuery(
data: const MediaQueryData(
size: size,
devicePixelRatio: devicePixelRatio,
textScaleFactor: textScaleFactor,
padding: padding,
viewInsets: viewInsets,
alwaysUse24HourFormat: true,
),
child: new Builder(
builder: (BuildContext context) {
return new MediaQuery.removePadding(
context: context,
removeLeft: true,
removeTop: true,
removeRight: true,
removeBottom: true,
child: new Builder(
builder: (BuildContext context) {
unpadded = MediaQuery.of(context);
return new Container();
}
),
);
},
),
)
);
expect(unpadded.size, size);
expect(unpadded.devicePixelRatio, devicePixelRatio);
expect(unpadded.textScaleFactor, textScaleFactor);
expect(unpadded.padding, EdgeInsets.zero);
expect(unpadded.viewInsets, viewInsets);
expect(unpadded.alwaysUse24HourFormat, true);
expect(unpadded.size, size);
expect(unpadded.devicePixelRatio, devicePixelRatio);
expect(unpadded.textScaleFactor, textScaleFactor);
expect(unpadded.padding, EdgeInsets.zero);
expect(unpadded.viewInsets, viewInsets);
expect(unpadded.alwaysUse24HourFormat, true);
});
testWidgets('MediaQuery.removeViewInsets removes specified viewInsets', (WidgetTester tester) async {
......@@ -165,4 +165,31 @@ void main() {
expect(unpadded.viewInsets, EdgeInsets.zero);
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