Skip to content

Commit

Permalink
Squashed MediaQuery InheritedModel (#114459)
Browse files Browse the repository at this point in the history
  • Loading branch information
moffatman authored Dec 9, 2022
1 parent e57b7f4 commit 73cb7c2
Show file tree
Hide file tree
Showing 88 changed files with 687 additions and 239 deletions.
11 changes: 11 additions & 0 deletions packages/flutter/lib/fix_data/fix_cupertino.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,17 @@
# * Fixes in this file are from the Cupertino library. *
version: 1
transforms:
# Changes made in https://github.com/flutter/flutter/pull/114459
- title: "Migrate to 'boldTextOf'"
date: 2022-10-28
element:
uris: ['widgets.dart', 'material.dart', 'cupertino.dart']
method: 'boldTextOverride'
inClass: 'MediaQuery'
changes:
- kind: 'rename'
newName: 'boldTextOf'

# Change made in https://github.com/flutter/flutter/pull/20649
# TODO(Piinks): Add tests when `bulkApply:false` testing is supported, https://github.com/dart-lang/sdk/issues/44639
- title: "Replace with 'CupertinoPopupSurface'"
Expand Down
11 changes: 11 additions & 0 deletions packages/flutter/lib/fix_data/fix_material/fix_material.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,17 @@
# * ThemeData: fix_theme_data.yaml
version: 1
transforms:
# Changes made in https://github.com/flutter/flutter/pull/114459
- title: "Migrate to 'boldTextOf'"
date: 2022-10-28
element:
uris: ['widgets.dart', 'material.dart', 'cupertino.dart']
method: 'boldTextOverride'
inClass: 'MediaQuery'
changes:
- kind: 'rename'
newName: 'boldTextOf'

# Changes made in https://github.com/flutter/flutter/pull/15303
- title: "Replace 'child' with 'builder'"
date: 2020-12-17
Expand Down
11 changes: 11 additions & 0 deletions packages/flutter/lib/fix_data/fix_widgets/fix_widgets.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,17 @@
# * ListWheelScrollView: fix_list_wheel_scroll_view.yaml
version: 1
transforms:
# Changes made in https://github.com/flutter/flutter/pull/114459
- title: "Migrate to 'boldTextOf'"
date: 2022-10-28
element:
uris: ['widgets.dart', 'material.dart', 'cupertino.dart']
method: 'boldTextOverride'
inClass: 'MediaQuery'
changes:
- kind: 'rename'
newName: 'boldTextOf'

# Changes made in https://github.com/flutter/flutter/pull/87839
- title: "Migrate to 'disallowIndicator'"
date: 2021-08-06
Expand Down
2 changes: 1 addition & 1 deletion packages/flutter/lib/src/cupertino/bottom_tab_bar.dart
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,7 @@ class CupertinoTabBar extends StatelessWidget implements PreferredSizeWidget {
@override
Widget build(BuildContext context) {
assert(debugCheckHasMediaQuery(context));
final double bottomPadding = MediaQuery.of(context).viewPadding.bottom;
final double bottomPadding = MediaQuery.viewPaddingOf(context).bottom;

final Color backgroundColor = CupertinoDynamicColor.resolve(
this.backgroundColor ?? CupertinoTheme.of(context).barBackgroundColor,
Expand Down
2 changes: 1 addition & 1 deletion packages/flutter/lib/src/cupertino/colors.dart
Original file line number Diff line number Diff line change
Expand Up @@ -994,7 +994,7 @@ class CupertinoDynamicColor extends Color with Diagnosticable {
}
bool isHighContrastEnabled = false;
if (_isHighContrastDependent) {
isHighContrastEnabled = MediaQuery.maybeOf(context)?.highContrast ?? false;
isHighContrastEnabled = MediaQuery.maybeHighContrastOf(context) ?? false;
}

final CupertinoUserInterfaceLevelData level = _isInterfaceElevationDependent
Expand Down
4 changes: 2 additions & 2 deletions packages/flutter/lib/src/cupertino/context_menu.dart
Original file line number Diff line number Diff line change
Expand Up @@ -493,7 +493,7 @@ class _CupertinoContextMenuState extends State<CupertinoContextMenu> with Ticker
// it.
_ContextMenuLocation get _contextMenuLocation {
final Rect childRect = _getRect(_childGlobalKey);
final double screenWidth = MediaQuery.of(context).size.width;
final double screenWidth = MediaQuery.sizeOf(context).width;

final double center = screenWidth / 2;
final bool centerDividesChild = childRect.left < center
Expand Down Expand Up @@ -1311,7 +1311,7 @@ class _ContextMenuRouteStaticState extends State<_ContextMenuRouteStatic> with T
Widget _buildChildAnimation(BuildContext context, Widget? child) {
_lastScale = _getScale(
widget.orientation,
MediaQuery.of(context).size.height,
MediaQuery.sizeOf(context).height,
_moveAnimation.value.dy,
);
return Transform.scale(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -158,20 +158,20 @@ class _CupertinoDesktopTextSelectionControlsToolbarState extends State<_Cupertin
}

assert(debugCheckHasMediaQuery(context));
final MediaQueryData mediaQuery = MediaQuery.of(context);
final EdgeInsets mediaQueryPadding = MediaQuery.paddingOf(context);

final Offset midpointAnchor = Offset(
clampDouble(widget.selectionMidpoint.dx - widget.globalEditableRegion.left,
mediaQuery.padding.left,
mediaQuery.size.width - mediaQuery.padding.right,
mediaQueryPadding.left,
MediaQuery.sizeOf(context).width - mediaQueryPadding.right,
),
widget.selectionMidpoint.dy - widget.globalEditableRegion.top,
);

final List<Widget> items = <Widget>[];
final CupertinoLocalizations localizations = CupertinoLocalizations.of(context);
final Widget onePhysicalPixelVerticalDivider =
SizedBox(width: 1.0 / MediaQuery.of(context).devicePixelRatio);
SizedBox(width: 1.0 / MediaQuery.devicePixelRatioOf(context));

void addToolbarButton(
String text,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,9 +85,8 @@ class CupertinoDesktopTextSelectionToolbar extends StatelessWidget {
@override
Widget build(BuildContext context) {
assert(debugCheckHasMediaQuery(context));
final MediaQueryData mediaQuery = MediaQuery.of(context);

final double paddingAbove = mediaQuery.padding.top + _kToolbarScreenPadding;
final double paddingAbove = MediaQuery.paddingOf(context).top + _kToolbarScreenPadding;
final Offset localAdjustment = Offset(_kToolbarScreenPadding, paddingAbove);

return Padding(
Expand Down
20 changes: 10 additions & 10 deletions packages/flutter/lib/src/cupertino/dialog.dart
Original file line number Diff line number Diff line change
Expand Up @@ -149,8 +149,8 @@ const double _kMaxRegularTextScaleFactor = 1.4;
// Accessibility mode on iOS is determined by the text scale factor that the
// user has selected.
bool _isInAccessibilityMode(BuildContext context) {
final MediaQueryData? data = MediaQuery.maybeOf(context);
return data != null && data.textScaleFactor > _kMaxRegularTextScaleFactor;
final double? factor = MediaQuery.maybeTextScaleFactorOf(context);
return factor != null && factor > _kMaxRegularTextScaleFactor;
}

/// An iOS-style alert dialog.
Expand Down Expand Up @@ -257,7 +257,7 @@ class CupertinoAlertDialog extends StatelessWidget {
final Curve insetAnimationCurve;

Widget _buildContent(BuildContext context) {
final double textScaleFactor = MediaQuery.of(context).textScaleFactor;
final double textScaleFactor = MediaQuery.textScaleFactorOf(context);

final List<Widget> children = <Widget>[
if (title != null || content != null)
Expand Down Expand Up @@ -317,7 +317,7 @@ class CupertinoAlertDialog extends StatelessWidget {
Widget build(BuildContext context) {
final CupertinoLocalizations localizations = CupertinoLocalizations.of(context);
final bool isInAccessibilityMode = _isInAccessibilityMode(context);
final double textScaleFactor = MediaQuery.of(context).textScaleFactor;
final double textScaleFactor = MediaQuery.textScaleFactorOf(context);
return CupertinoUserInterfaceLevel(
data: CupertinoUserInterfaceLevelData.elevated,
child: MediaQuery(
Expand All @@ -331,7 +331,7 @@ class CupertinoAlertDialog extends StatelessWidget {
child: LayoutBuilder(
builder: (BuildContext context, BoxConstraints constraints) {
return AnimatedPadding(
padding: MediaQuery.of(context).viewInsets +
padding: MediaQuery.viewInsetsOf(context) +
const EdgeInsets.symmetric(horizontal: 40.0, vertical: 24.0),
duration: insetAnimationDuration,
curve: insetAnimationCurve,
Expand Down Expand Up @@ -611,12 +611,12 @@ class CupertinoActionSheet extends StatelessWidget {
if (cancelButton != null) _buildCancelButton(),
];

final Orientation orientation = MediaQuery.of(context).orientation;
final Orientation orientation = MediaQuery.orientationOf(context);
final double actionSheetWidth;
if (orientation == Orientation.portrait) {
actionSheetWidth = MediaQuery.of(context).size.width - (_kActionSheetEdgeHorizontalPadding * 2);
actionSheetWidth = MediaQuery.sizeOf(context).width - (_kActionSheetEdgeHorizontalPadding * 2);
} else {
actionSheetWidth = MediaQuery.of(context).size.height - (_kActionSheetEdgeHorizontalPadding * 2);
actionSheetWidth = MediaQuery.sizeOf(context).height - (_kActionSheetEdgeHorizontalPadding * 2);
}

return SafeArea(
Expand Down Expand Up @@ -797,7 +797,7 @@ class _CupertinoDialogRenderWidget extends RenderObjectWidget {
@override
RenderObject createRenderObject(BuildContext context) {
return _RenderCupertinoDialog(
dividerThickness: _kDividerThickness / MediaQuery.of(context).devicePixelRatio,
dividerThickness: _kDividerThickness / MediaQuery.devicePixelRatioOf(context),
isInAccessibilityMode: _isInAccessibilityMode(context) && !isActionSheet,
dividerColor: CupertinoDynamicColor.resolve(dividerColor, context),
isActionSheet: isActionSheet,
Expand Down Expand Up @@ -1464,7 +1464,7 @@ class _CupertinoAlertActionSection extends StatelessWidget {

@override
Widget build(BuildContext context) {
final double devicePixelRatio = MediaQuery.of(context).devicePixelRatio;
final double devicePixelRatio = MediaQuery.devicePixelRatioOf(context);

final List<Widget> interactiveButtons = <Widget>[];
for (int i = 0; i < children.length; i += 1) {
Expand Down
2 changes: 1 addition & 1 deletion packages/flutter/lib/src/cupertino/list_section.dart
Original file line number Diff line number Diff line change
Expand Up @@ -347,7 +347,7 @@ class CupertinoListSection extends StatelessWidget {
@override
Widget build(BuildContext context) {
final Color dividerColor = CupertinoColors.separator.resolveFrom(context);
final double dividerHeight = 1.0 / MediaQuery.of(context).devicePixelRatio;
final double dividerHeight = 1.0 / MediaQuery.devicePixelRatioOf(context);

// Long divider is used for wrapping the top and bottom of rows.
// Only used in CupertinoListSectionType.base mode.
Expand Down
2 changes: 1 addition & 1 deletion packages/flutter/lib/src/cupertino/magnifier.dart
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,7 @@ class _CupertinoTextMagnifierState extends State<CupertinoTextMagnifier>
CupertinoMagnifier.kMagnifierAboveFocalPoint),
);

final Rect screenRect = Offset.zero & MediaQuery.of(context).size;
final Rect screenRect = Offset.zero & MediaQuery.sizeOf(context);

// Adjust the magnifier position so that it never exists outside the horizontal
// padding.
Expand Down
6 changes: 3 additions & 3 deletions packages/flutter/lib/src/cupertino/nav_bar.dart
Original file line number Diff line number Diff line change
Expand Up @@ -756,7 +756,7 @@ class _CupertinoSliverNavigationBarState extends State<CupertinoSliverNavigation
actionsForegroundColor: CupertinoTheme.of(context).primaryColor,
transitionBetweenRoutes: widget.transitionBetweenRoutes,
heroTag: widget.heroTag,
persistentHeight: _kNavBarPersistentHeight + MediaQuery.of(context).padding.top,
persistentHeight: _kNavBarPersistentHeight + MediaQuery.paddingOf(context).top,
alwaysShowMiddle: widget.alwaysShowMiddle && widget.middle != null,
stretchConfiguration: widget.stretch ? OverScrollHeaderStretchConfiguration() : null,
),
Expand Down Expand Up @@ -1104,7 +1104,7 @@ class _PersistentNavigationBar extends StatelessWidget {
}

return SizedBox(
height: _kNavBarPersistentHeight + MediaQuery.of(context).padding.top,
height: _kNavBarPersistentHeight + MediaQuery.paddingOf(context).top,
child: SafeArea(
bottom: false,
child: paddedToolbar,
Expand Down Expand Up @@ -1772,7 +1772,7 @@ class _NavigationBarTransition extends StatelessWidget {
// can actually be outside the linearly lerp'ed Rect in the middle of
// the animation, such as the topLargeTitle.
return SizedBox(
height: math.max(heightTween.begin!, heightTween.end!) + MediaQuery.of(context).padding.top,
height: math.max(heightTween.begin!, heightTween.end!) + MediaQuery.paddingOf(context).top,
width: double.infinity,
child: Stack(
children: children,
Expand Down
4 changes: 2 additions & 2 deletions packages/flutter/lib/src/cupertino/route.dart
Original file line number Diff line number Diff line change
Expand Up @@ -690,8 +690,8 @@ class _CupertinoBackGestureDetectorState<T> extends State<_CupertinoBackGestureD
// For devices with notches, the drag area needs to be larger on the side
// that has the notch.
double dragAreaWidth = Directionality.of(context) == TextDirection.ltr ?
MediaQuery.of(context).padding.left :
MediaQuery.of(context).padding.right;
MediaQuery.paddingOf(context).left :
MediaQuery.paddingOf(context).right;
dragAreaWidth = max(dragAreaWidth, _kBackGestureWidth);
return Stack(
fit: StackFit.passthrough,
Expand Down
2 changes: 1 addition & 1 deletion packages/flutter/lib/src/cupertino/scrollbar.dart
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,7 @@ class _CupertinoScrollbarState extends RawScrollbarState<CupertinoScrollbar> {
..mainAxisMargin = _kScrollbarMainAxisMargin
..crossAxisMargin = _kScrollbarCrossAxisMargin
..radius = _radius
..padding = MediaQuery.of(context).padding
..padding = MediaQuery.paddingOf(context)
..minLength = _kScrollbarMinLength
..minOverscrollLength = _kScrollbarMinOverscrollLength
..scrollbarOrientation = widget.scrollbarOrientation;
Expand Down
2 changes: 1 addition & 1 deletion packages/flutter/lib/src/cupertino/text_field.dart
Original file line number Diff line number Diff line change
Expand Up @@ -1224,7 +1224,7 @@ class _CupertinoTextFieldState extends State<CupertinoTextField> with Restoratio
}

final bool enabled = widget.enabled ?? true;
final Offset cursorOffset = Offset(_iOSHorizontalCursorOffsetPixels / MediaQuery.of(context).devicePixelRatio, 0);
final Offset cursorOffset = Offset(_iOSHorizontalCursorOffsetPixels / MediaQuery.devicePixelRatioOf(context), 0);
final List<TextInputFormatter> formatters = <TextInputFormatter>[
...?widget.inputFormatters,
if (widget.maxLength != null)
Expand Down
8 changes: 4 additions & 4 deletions packages/flutter/lib/src/cupertino/text_selection.dart
Original file line number Diff line number Diff line change
Expand Up @@ -260,14 +260,14 @@ class _CupertinoTextSelectionControlsToolbarState extends State<_CupertinoTextSe
}

assert(debugCheckHasMediaQuery(context));
final MediaQueryData mediaQuery = MediaQuery.of(context);
final EdgeInsets mediaQueryPadding = MediaQuery.paddingOf(context);

// The toolbar should appear below the TextField when there is not enough
// space above the TextField to show it, assuming there's always enough
// space at the bottom in this case.
final double anchorX = clampDouble(widget.selectionMidpoint.dx + widget.globalEditableRegion.left,
_kArrowScreenPadding + mediaQuery.padding.left,
mediaQuery.size.width - mediaQuery.padding.right - _kArrowScreenPadding,
_kArrowScreenPadding + mediaQueryPadding.left,
MediaQuery.sizeOf(context).width - mediaQueryPadding.right - _kArrowScreenPadding,
);

final double topAmountInEditableRegion = widget.endpoints.first.point.dy - widget.textLineHeight;
Expand All @@ -289,7 +289,7 @@ class _CupertinoTextSelectionControlsToolbarState extends State<_CupertinoTextSe
final List<Widget> items = <Widget>[];
final CupertinoLocalizations localizations = CupertinoLocalizations.of(context);
final Widget onePhysicalPixelVerticalDivider =
SizedBox(width: 1.0 / MediaQuery.of(context).devicePixelRatio);
SizedBox(width: 1.0 / MediaQuery.devicePixelRatioOf(context));

void addToolbarButton(
String text,
Expand Down
12 changes: 6 additions & 6 deletions packages/flutter/lib/src/cupertino/text_selection_toolbar.dart
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ typedef CupertinoToolbarBuilder = Widget Function(
class _CupertinoToolbarButtonDivider extends StatelessWidget {
@override
Widget build(BuildContext context) {
return SizedBox(width: 1.0 / MediaQuery.of(context).devicePixelRatio);
return SizedBox(width: 1.0 / MediaQuery.devicePixelRatioOf(context));
}
}

Expand Down Expand Up @@ -132,18 +132,18 @@ class CupertinoTextSelectionToolbar extends StatelessWidget {
@override
Widget build(BuildContext context) {
assert(debugCheckHasMediaQuery(context));
final MediaQueryData mediaQuery = MediaQuery.of(context);
final EdgeInsets mediaQueryPadding = MediaQuery.paddingOf(context);

final double paddingAbove = mediaQuery.padding.top + _kToolbarScreenPadding;
final double paddingAbove = mediaQueryPadding.top + _kToolbarScreenPadding;
final double toolbarHeightNeeded = paddingAbove
+ _kToolbarContentDistance
+ _kToolbarHeight;
final bool fitsAbove = anchorAbove.dy >= toolbarHeightNeeded;

// The arrow, which points to the anchor, has some margin so it can't get
// too close to the horizontal edges of the screen.
final double leftMargin = _kArrowScreenPadding + mediaQuery.padding.left;
final double rightMargin = mediaQuery.size.width - mediaQuery.padding.right - _kArrowScreenPadding;
final double leftMargin = _kArrowScreenPadding + mediaQueryPadding.left;
final double rightMargin = MediaQuery.sizeOf(context).width - mediaQueryPadding.right - _kArrowScreenPadding;

final Offset anchorAboveAdjusted = Offset(
clampDouble(anchorAbove.dx, leftMargin, rightMargin),
Expand Down Expand Up @@ -480,7 +480,7 @@ class _CupertinoTextSelectionToolbarContentState extends State<_CupertinoTextSel
onPressed: _handlePreviousPage,
text: '◀',
),
dividerWidth: 1.0 / MediaQuery.of(context).devicePixelRatio,
dividerWidth: 1.0 / MediaQuery.devicePixelRatioOf(context),
nextButton: CupertinoTextSelectionToolbarButton.text(
onPressed: _handleNextPage,
text: '▶',
Expand Down
4 changes: 2 additions & 2 deletions packages/flutter/lib/src/cupertino/theme.dart
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ class CupertinoTheme extends StatelessWidget {
/// [MediaQueryData.platformBrightness] for descendant Cupertino widgets.
static Brightness brightnessOf(BuildContext context) {
final _InheritedCupertinoTheme? inheritedTheme = context.dependOnInheritedWidgetOfExactType<_InheritedCupertinoTheme>();
return inheritedTheme?.theme.data.brightness ?? MediaQuery.of(context).platformBrightness;
return inheritedTheme?.theme.data.brightness ?? MediaQuery.platformBrightnessOf(context);
}

/// Retrieves the [Brightness] to use for descendant Cupertino widgets, based
Expand All @@ -107,7 +107,7 @@ class CupertinoTheme extends StatelessWidget {
/// [MediaQuery] exists, instead of returning null.
static Brightness? maybeBrightnessOf(BuildContext context) {
final _InheritedCupertinoTheme? inheritedTheme = context.dependOnInheritedWidgetOfExactType<_InheritedCupertinoTheme>();
return inheritedTheme?.theme.data.brightness ?? MediaQuery.maybeOf(context)?.platformBrightness;
return inheritedTheme?.theme.data.brightness ?? MediaQuery.maybePlatformBrightnessOf(context);
}

/// The widget below this widget in the tree.
Expand Down
4 changes: 2 additions & 2 deletions packages/flutter/lib/src/material/about.dart
Original file line number Diff line number Diff line change
Expand Up @@ -976,7 +976,7 @@ const double _wideGutterSize = 24.0;
const double _narrowGutterSize = 12.0;

double _getGutterSize(BuildContext context) =>
MediaQuery.of(context).size.width >= _materialGutterThreshold ? _wideGutterSize : _narrowGutterSize;
MediaQuery.sizeOf(context).width >= _materialGutterThreshold ? _wideGutterSize : _narrowGutterSize;

/// Signature for the builder callback used by [_MasterDetailFlow].
typedef _MasterViewBuilder = Widget Function(BuildContext context, bool isLateralUI);
Expand Down Expand Up @@ -1463,7 +1463,7 @@ class _DetailView extends StatelessWidget {
if (_arguments == null) {
return const SizedBox.shrink();
}
final double screenHeight = MediaQuery.of(context).size.height;
final double screenHeight = MediaQuery.sizeOf(context).height;
final double minHeight = (screenHeight - kToolbarHeight) / screenHeight;

return DraggableScrollableSheet(
Expand Down
2 changes: 1 addition & 1 deletion packages/flutter/lib/src/material/action_chip.dart
Original file line number Diff line number Diff line change
Expand Up @@ -239,7 +239,7 @@ class _ActionChipDefaultsM3 extends ChipThemeData {
EdgeInsetsGeometry? get labelPadding => EdgeInsets.lerp(
const EdgeInsets.symmetric(horizontal: 8.0),
const EdgeInsets.symmetric(horizontal: 4.0),
clampDouble(MediaQuery.of(context).textScaleFactor - 1.0, 0.0, 1.0),
clampDouble(MediaQuery.textScaleFactorOf(context) - 1.0, 0.0, 1.0),
)!;
}

Expand Down
Loading

0 comments on commit 73cb7c2

Please sign in to comment.