diff --git a/packages/flutter/lib/fix_data/fix_cupertino.yaml b/packages/flutter/lib/fix_data/fix_cupertino.yaml index b9e40b7f1e74..b516113dfff9 100644 --- a/packages/flutter/lib/fix_data/fix_cupertino.yaml +++ b/packages/flutter/lib/fix_data/fix_cupertino.yaml @@ -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'" diff --git a/packages/flutter/lib/fix_data/fix_material/fix_material.yaml b/packages/flutter/lib/fix_data/fix_material/fix_material.yaml index 0359c8da87f0..fd927f4b4d9f 100644 --- a/packages/flutter/lib/fix_data/fix_material/fix_material.yaml +++ b/packages/flutter/lib/fix_data/fix_material/fix_material.yaml @@ -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 diff --git a/packages/flutter/lib/fix_data/fix_widgets/fix_widgets.yaml b/packages/flutter/lib/fix_data/fix_widgets/fix_widgets.yaml index 47d25d49a729..38e35c53d0e5 100644 --- a/packages/flutter/lib/fix_data/fix_widgets/fix_widgets.yaml +++ b/packages/flutter/lib/fix_data/fix_widgets/fix_widgets.yaml @@ -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 diff --git a/packages/flutter/lib/src/cupertino/bottom_tab_bar.dart b/packages/flutter/lib/src/cupertino/bottom_tab_bar.dart index 4e1f18e29147..5dc70c3665c2 100644 --- a/packages/flutter/lib/src/cupertino/bottom_tab_bar.dart +++ b/packages/flutter/lib/src/cupertino/bottom_tab_bar.dart @@ -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, diff --git a/packages/flutter/lib/src/cupertino/colors.dart b/packages/flutter/lib/src/cupertino/colors.dart index 41fe80ea0fd1..06178b921a82 100644 --- a/packages/flutter/lib/src/cupertino/colors.dart +++ b/packages/flutter/lib/src/cupertino/colors.dart @@ -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 diff --git a/packages/flutter/lib/src/cupertino/context_menu.dart b/packages/flutter/lib/src/cupertino/context_menu.dart index 87dafbcf7c5b..9e673355c25a 100644 --- a/packages/flutter/lib/src/cupertino/context_menu.dart +++ b/packages/flutter/lib/src/cupertino/context_menu.dart @@ -493,7 +493,7 @@ class _CupertinoContextMenuState extends State 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 @@ -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( diff --git a/packages/flutter/lib/src/cupertino/desktop_text_selection.dart b/packages/flutter/lib/src/cupertino/desktop_text_selection.dart index 88b9d9b3f7c6..2f053f13fe44 100644 --- a/packages/flutter/lib/src/cupertino/desktop_text_selection.dart +++ b/packages/flutter/lib/src/cupertino/desktop_text_selection.dart @@ -158,12 +158,12 @@ 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, ); @@ -171,7 +171,7 @@ class _CupertinoDesktopTextSelectionControlsToolbarState extends State<_Cupertin final List items = []; 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, diff --git a/packages/flutter/lib/src/cupertino/desktop_text_selection_toolbar.dart b/packages/flutter/lib/src/cupertino/desktop_text_selection_toolbar.dart index 990076500eef..5a586c80b290 100644 --- a/packages/flutter/lib/src/cupertino/desktop_text_selection_toolbar.dart +++ b/packages/flutter/lib/src/cupertino/desktop_text_selection_toolbar.dart @@ -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( diff --git a/packages/flutter/lib/src/cupertino/dialog.dart b/packages/flutter/lib/src/cupertino/dialog.dart index 0a7286bb70d0..ddd491caef05 100644 --- a/packages/flutter/lib/src/cupertino/dialog.dart +++ b/packages/flutter/lib/src/cupertino/dialog.dart @@ -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. @@ -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 children = [ if (title != null || content != null) @@ -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( @@ -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, @@ -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( @@ -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, @@ -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 interactiveButtons = []; for (int i = 0; i < children.length; i += 1) { diff --git a/packages/flutter/lib/src/cupertino/list_section.dart b/packages/flutter/lib/src/cupertino/list_section.dart index bdcb445c8614..84cac8bb01cf 100644 --- a/packages/flutter/lib/src/cupertino/list_section.dart +++ b/packages/flutter/lib/src/cupertino/list_section.dart @@ -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. diff --git a/packages/flutter/lib/src/cupertino/magnifier.dart b/packages/flutter/lib/src/cupertino/magnifier.dart index c7b4b6d54c85..e5e1075170fa 100644 --- a/packages/flutter/lib/src/cupertino/magnifier.dart +++ b/packages/flutter/lib/src/cupertino/magnifier.dart @@ -174,7 +174,7 @@ class _CupertinoTextMagnifierState extends State 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. diff --git a/packages/flutter/lib/src/cupertino/nav_bar.dart b/packages/flutter/lib/src/cupertino/nav_bar.dart index d7cfcff1c806..976366d8d285 100644 --- a/packages/flutter/lib/src/cupertino/nav_bar.dart +++ b/packages/flutter/lib/src/cupertino/nav_bar.dart @@ -756,7 +756,7 @@ class _CupertinoSliverNavigationBarState extends State 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, diff --git a/packages/flutter/lib/src/cupertino/scrollbar.dart b/packages/flutter/lib/src/cupertino/scrollbar.dart index b0112ef7d243..34109290f12d 100644 --- a/packages/flutter/lib/src/cupertino/scrollbar.dart +++ b/packages/flutter/lib/src/cupertino/scrollbar.dart @@ -171,7 +171,7 @@ class _CupertinoScrollbarState extends RawScrollbarState { ..mainAxisMargin = _kScrollbarMainAxisMargin ..crossAxisMargin = _kScrollbarCrossAxisMargin ..radius = _radius - ..padding = MediaQuery.of(context).padding + ..padding = MediaQuery.paddingOf(context) ..minLength = _kScrollbarMinLength ..minOverscrollLength = _kScrollbarMinOverscrollLength ..scrollbarOrientation = widget.scrollbarOrientation; diff --git a/packages/flutter/lib/src/cupertino/text_field.dart b/packages/flutter/lib/src/cupertino/text_field.dart index 8fad5ea9f3e8..977b9df22ecd 100644 --- a/packages/flutter/lib/src/cupertino/text_field.dart +++ b/packages/flutter/lib/src/cupertino/text_field.dart @@ -1224,7 +1224,7 @@ class _CupertinoTextFieldState extends State 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 formatters = [ ...?widget.inputFormatters, if (widget.maxLength != null) diff --git a/packages/flutter/lib/src/cupertino/text_selection.dart b/packages/flutter/lib/src/cupertino/text_selection.dart index 512ec71e7889..b70848dadfde 100644 --- a/packages/flutter/lib/src/cupertino/text_selection.dart +++ b/packages/flutter/lib/src/cupertino/text_selection.dart @@ -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; @@ -289,7 +289,7 @@ class _CupertinoTextSelectionControlsToolbarState extends State<_CupertinoTextSe final List items = []; 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, diff --git a/packages/flutter/lib/src/cupertino/text_selection_toolbar.dart b/packages/flutter/lib/src/cupertino/text_selection_toolbar.dart index a630b3dddf97..d0fee9557951 100644 --- a/packages/flutter/lib/src/cupertino/text_selection_toolbar.dart +++ b/packages/flutter/lib/src/cupertino/text_selection_toolbar.dart @@ -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)); } } @@ -132,9 +132,9 @@ 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; @@ -142,8 +142,8 @@ class CupertinoTextSelectionToolbar extends StatelessWidget { // 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), @@ -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: '▶', diff --git a/packages/flutter/lib/src/cupertino/theme.dart b/packages/flutter/lib/src/cupertino/theme.dart index 3d8d493cc038..57e080421094 100644 --- a/packages/flutter/lib/src/cupertino/theme.dart +++ b/packages/flutter/lib/src/cupertino/theme.dart @@ -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 @@ -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. diff --git a/packages/flutter/lib/src/material/about.dart b/packages/flutter/lib/src/material/about.dart index 4378f24a546a..1bab4e8a1a05 100644 --- a/packages/flutter/lib/src/material/about.dart +++ b/packages/flutter/lib/src/material/about.dart @@ -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); @@ -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( diff --git a/packages/flutter/lib/src/material/action_chip.dart b/packages/flutter/lib/src/material/action_chip.dart index 3cada585951d..eaa09fd9f8c4 100644 --- a/packages/flutter/lib/src/material/action_chip.dart +++ b/packages/flutter/lib/src/material/action_chip.dart @@ -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), )!; } diff --git a/packages/flutter/lib/src/material/app_bar.dart b/packages/flutter/lib/src/material/app_bar.dart index 79d6257b1930..781da031627c 100644 --- a/packages/flutter/lib/src/material/app_bar.dart +++ b/packages/flutter/lib/src/material/app_bar.dart @@ -2097,7 +2097,7 @@ class _SliverAppBarState extends State with TickerProviderStateMix Widget build(BuildContext context) { assert(!widget.primary || debugCheckHasMediaQuery(context)); final double bottomHeight = widget.bottom?.preferredSize.height ?? 0.0; - final double topPadding = widget.primary ? MediaQuery.of(context).padding.top : 0.0; + final double topPadding = widget.primary ? MediaQuery.paddingOf(context).top : 0.0; final double collapsedHeight = (widget.pinned && widget.floating && widget.bottom != null) ? (widget.collapsedHeight ?? 0.0) + bottomHeight + topPadding : (widget.collapsedHeight ?? widget.toolbarHeight) + bottomHeight + topPadding; @@ -2211,7 +2211,7 @@ class _ScrollUnderFlexibleSpace extends StatelessWidget { Widget build(BuildContext context) { late final ThemeData theme = Theme.of(context); final FlexibleSpaceBarSettings settings = context.dependOnInheritedWidgetOfExactType()!; - final double topPadding = primary ? MediaQuery.of(context).viewPadding.top : 0; + final double topPadding = primary ? MediaQuery.viewPaddingOf(context).top : 0; final double collapsedHeight = settings.minExtent - topPadding; final double scrollUnderHeight = settings.maxExtent - settings.minExtent; final _ScrollUnderFlexibleConfig config; diff --git a/packages/flutter/lib/src/material/banner.dart b/packages/flutter/lib/src/material/banner.dart index a5ca4b96dae2..245ba2ffbd7e 100644 --- a/packages/flutter/lib/src/material/banner.dart +++ b/packages/flutter/lib/src/material/banner.dart @@ -293,7 +293,7 @@ class _MaterialBannerState extends State { @override Widget build(BuildContext context) { assert(debugCheckHasMediaQuery(context)); - final MediaQueryData mediaQueryData = MediaQuery.of(context); + final bool accessibleNavigation = MediaQuery.accessibleNavigationOf(context); assert(widget.actions.isNotEmpty); @@ -399,7 +399,7 @@ class _MaterialBannerState extends State { onDismiss: () { ScaffoldMessenger.of(context).removeCurrentMaterialBanner(reason: MaterialBannerClosedReason.dismiss); }, - child: mediaQueryData.accessibleNavigation + child: accessibleNavigation ? materialBanner : SlideTransition( position: slideOutAnimation, @@ -408,7 +408,7 @@ class _MaterialBannerState extends State { ); final Widget materialBannerTransition; - if (mediaQueryData.accessibleNavigation) { + if (accessibleNavigation) { materialBannerTransition = materialBanner; } else { materialBannerTransition = AnimatedBuilder( diff --git a/packages/flutter/lib/src/material/bottom_navigation_bar.dart b/packages/flutter/lib/src/material/bottom_navigation_bar.dart index 9b027b11d9c8..eb5557bd898d 100644 --- a/packages/flutter/lib/src/material/bottom_navigation_bar.dart +++ b/packages/flutter/lib/src/material/bottom_navigation_bar.dart @@ -607,8 +607,7 @@ class _Tile extends StatelessWidget { @override Widget build(BuildContext context) { - final MediaQueryData data = MediaQuery.of(context); - if (data.orientation == Orientation.landscape && layout == BottomNavigationBarLandscapeLayout.linear) { + if (MediaQuery.orientationOf(context) == Orientation.landscape && layout == BottomNavigationBarLandscapeLayout.linear) { return Align( heightFactor: 1, child: Row( @@ -1114,7 +1113,7 @@ class _BottomNavigationBarState extends State with TickerPr final BottomNavigationBarLandscapeLayout layout = widget.landscapeLayout ?? bottomTheme.landscapeLayout ?? BottomNavigationBarLandscapeLayout.spread; - final double additionalBottomPadding = MediaQuery.of(context).viewPadding.bottom; + final double additionalBottomPadding = MediaQuery.viewPaddingOf(context).bottom; Color? backgroundColor; switch (_effectiveType) { @@ -1180,14 +1179,13 @@ class _Bar extends StatelessWidget { @override Widget build(BuildContext context) { - final MediaQueryData data = MediaQuery.of(context); Widget alignedChild = child; - if (data.orientation == Orientation.landscape && layout == BottomNavigationBarLandscapeLayout.centered) { + if (MediaQuery.orientationOf(context) == Orientation.landscape && layout == BottomNavigationBarLandscapeLayout.centered) { alignedChild = Align( alignment: Alignment.bottomCenter, heightFactor: 1, child: SizedBox( - width: data.size.height, + width: MediaQuery.sizeOf(context).height, child: child, ), ); diff --git a/packages/flutter/lib/src/material/bottom_sheet.dart b/packages/flutter/lib/src/material/bottom_sheet.dart index e5995b7fe9c6..fb32eb4fed3b 100644 --- a/packages/flutter/lib/src/material/bottom_sheet.dart +++ b/packages/flutter/lib/src/material/bottom_sheet.dart @@ -409,7 +409,6 @@ class _ModalBottomSheetState extends State<_ModalBottomSheet> { Widget build(BuildContext context) { assert(debugCheckHasMediaQuery(context)); assert(debugCheckHasMaterialLocalizations(context)); - final MediaQueryData mediaQuery = MediaQuery.of(context); final MaterialLocalizations localizations = MaterialLocalizations.of(context); final String routeLabel = _getRouteLabel(localizations); @@ -436,7 +435,7 @@ class _ModalBottomSheetState extends State<_ModalBottomSheet> { // Disable the initial animation when accessible navigation is on so // that the semantics are added to the tree at the correct time. final double animationValue = animationCurve.transform( - mediaQuery.accessibleNavigation ? 1.0 : widget.route.animation!.value, + MediaQuery.accessibleNavigationOf(context) ? 1.0 : widget.route.animation!.value, ); return Semantics( scopesRoute: true, diff --git a/packages/flutter/lib/src/material/chip.dart b/packages/flutter/lib/src/material/chip.dart index 6179e281bae0..30a2e97b985c 100644 --- a/packages/flutter/lib/src/material/chip.dart +++ b/packages/flutter/lib/src/material/chip.dart @@ -1158,7 +1158,7 @@ class _RawChipState extends State with MaterialStateMixin, TickerProvid final EdgeInsetsGeometry defaultLabelPadding = 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), )!; final ThemeData theme = Theme.of(context); @@ -2257,7 +2257,7 @@ class _ChipDefaultsM3 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), )!; } diff --git a/packages/flutter/lib/src/material/choice_chip.dart b/packages/flutter/lib/src/material/choice_chip.dart index 9111c2c9b268..4514d7f6ed39 100644 --- a/packages/flutter/lib/src/material/choice_chip.dart +++ b/packages/flutter/lib/src/material/choice_chip.dart @@ -259,7 +259,7 @@ class _ChoiceChipDefaultsM3 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), )!; } diff --git a/packages/flutter/lib/src/material/date_picker.dart b/packages/flutter/lib/src/material/date_picker.dart index 724b72b581b2..65ae2fc1ac2d 100644 --- a/packages/flutter/lib/src/material/date_picker.dart +++ b/packages/flutter/lib/src/material/date_picker.dart @@ -425,7 +425,7 @@ class _DatePickerDialogState extends State with RestorationMix } Size _dialogSize(BuildContext context) { - final Orientation orientation = MediaQuery.of(context).orientation; + final Orientation orientation = MediaQuery.orientationOf(context); switch (_entryMode.value) { case DatePickerEntryMode.calendar: case DatePickerEntryMode.calendarOnly: @@ -456,11 +456,11 @@ class _DatePickerDialogState extends State with RestorationMix final ThemeData theme = Theme.of(context); final ColorScheme colorScheme = theme.colorScheme; final MaterialLocalizations localizations = MaterialLocalizations.of(context); - final Orientation orientation = MediaQuery.of(context).orientation; + final Orientation orientation = MediaQuery.orientationOf(context); final TextTheme textTheme = theme.textTheme; // Constrain the textScaleFactor to the largest supported value to prevent // layout issues. - final double textScaleFactor = math.min(MediaQuery.of(context).textScaleFactor, 1.3); + final double textScaleFactor = math.min(MediaQuery.textScaleFactorOf(context), 1.3); final String dateText = localizations.formatMediumDate(_selectedDate.value); final Color onPrimarySurface = colorScheme.brightness == Brightness.light @@ -1316,9 +1316,8 @@ class _DateRangePickerDialogState extends State with Rest @override Widget build(BuildContext context) { - final MediaQueryData mediaQuery = MediaQuery.of(context); - final Orientation orientation = mediaQuery.orientation; - final double textScaleFactor = math.min(mediaQuery.textScaleFactor, 1.3); + final Orientation orientation = MediaQuery.orientationOf(context); + final double textScaleFactor = math.min(MediaQuery.textScaleFactorOf(context), 1.3); final MaterialLocalizations localizations = MaterialLocalizations.of(context); final ColorScheme colors = Theme.of(context).colorScheme; final Color onPrimarySurface = colors.brightness == Brightness.light @@ -1367,7 +1366,7 @@ class _DateRangePickerDialogState extends State with Rest : localizations.dateRangePickerHelpText.toUpperCase() ), ); - size = mediaQuery.size; + size = MediaQuery.sizeOf(context); insetPadding = EdgeInsets.zero; shape = const RoundedRectangleBorder(); elevation = 0; @@ -1499,7 +1498,7 @@ class _CalendarRangePickerDialog extends StatelessWidget { final ThemeData theme = Theme.of(context); final ColorScheme colorScheme = theme.colorScheme; final MaterialLocalizations localizations = MaterialLocalizations.of(context); - final Orientation orientation = MediaQuery.of(context).orientation; + final Orientation orientation = MediaQuery.orientationOf(context); final TextTheme textTheme = theme.textTheme; final Color headerForeground = colorScheme.brightness == Brightness.light ? colorScheme.onPrimary @@ -1539,7 +1538,7 @@ class _CalendarRangePickerDialog extends StatelessWidget { bottom: PreferredSize( preferredSize: const Size(double.infinity, 64), child: Row(children: [ - SizedBox(width: MediaQuery.of(context).size.width < 360 ? 42 : 72), + SizedBox(width: MediaQuery.sizeOf(context).width < 360 ? 42 : 72), Expanded( child: Semantics( label: '$helpText $startDateText to $endDateText', @@ -2017,7 +2016,7 @@ class _DayHeaders extends StatelessWidget { return Container( constraints: BoxConstraints( - maxWidth: MediaQuery.of(context).orientation == Orientation.landscape + maxWidth: MediaQuery.orientationOf(context) == Orientation.landscape ? _maxCalendarWidthLandscape : _maxCalendarWidthPortrait, maxHeight: _monthItemRowHeight, @@ -2469,7 +2468,7 @@ class _MonthItemState extends State<_MonthItem> { paddedDayItems.addAll(weekList); } - final double maxWidth = MediaQuery.of(context).orientation == Orientation.landscape + final double maxWidth = MediaQuery.orientationOf(context) == Orientation.landscape ? _maxCalendarWidthLandscape : _maxCalendarWidthPortrait; return Column( @@ -2623,7 +2622,7 @@ class _InputDateRangePickerDialog extends StatelessWidget { final ThemeData theme = Theme.of(context); final ColorScheme colorScheme = theme.colorScheme; final MaterialLocalizations localizations = MaterialLocalizations.of(context); - final Orientation orientation = MediaQuery.of(context).orientation; + final Orientation orientation = MediaQuery.orientationOf(context); final TextTheme textTheme = theme.textTheme; final Color onPrimarySurfaceColor = colorScheme.brightness == Brightness.light diff --git a/packages/flutter/lib/src/material/desktop_text_selection.dart b/packages/flutter/lib/src/material/desktop_text_selection.dart index b67c371f8f5a..f2b029dd111e 100644 --- a/packages/flutter/lib/src/material/desktop_text_selection.dart +++ b/packages/flutter/lib/src/material/desktop_text_selection.dart @@ -177,12 +177,11 @@ class _DesktopTextSelectionControlsToolbarState extends State<_DesktopTextSelect return const SizedBox.shrink(); } - 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, ); diff --git a/packages/flutter/lib/src/material/desktop_text_selection_toolbar.dart b/packages/flutter/lib/src/material/desktop_text_selection_toolbar.dart index e1dbfe679a16..df69b4556001 100644 --- a/packages/flutter/lib/src/material/desktop_text_selection_toolbar.dart +++ b/packages/flutter/lib/src/material/desktop_text_selection_toolbar.dart @@ -64,9 +64,8 @@ class DesktopTextSelectionToolbar 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( diff --git a/packages/flutter/lib/src/material/dialog.dart b/packages/flutter/lib/src/material/dialog.dart index 4204fac8c896..95ab97582352 100644 --- a/packages/flutter/lib/src/material/dialog.dart +++ b/packages/flutter/lib/src/material/dialog.dart @@ -217,7 +217,7 @@ class Dialog extends StatelessWidget { Widget build(BuildContext context) { final ThemeData theme = Theme.of(context); final DialogTheme dialogTheme = DialogTheme.of(context); - final EdgeInsets effectivePadding = MediaQuery.of(context).viewInsets + (insetPadding ?? EdgeInsets.zero); + final EdgeInsets effectivePadding = MediaQuery.viewInsetsOf(context) + (insetPadding ?? EdgeInsets.zero); final DialogTheme defaults = theme.useMaterial3 ? (_fullscreen ? _DialogFullscreenDefaultsM3(context) : _DialogDefaultsM3(context)) : _DialogDefaultsM2(context); @@ -642,7 +642,7 @@ class AlertDialog extends StatelessWidget { // The paddingScaleFactor is used to adjust the padding of Dialog's // children. - final double paddingScaleFactor = _paddingScaleFactor(MediaQuery.of(context).textScaleFactor); + final double paddingScaleFactor = _paddingScaleFactor(MediaQuery.textScaleFactorOf(context)); final TextDirection? textDirection = Directionality.maybeOf(context); Widget? iconWidget; @@ -1075,7 +1075,7 @@ class SimpleDialog extends StatelessWidget { // The paddingScaleFactor is used to adjust the padding of Dialog // children. - final double paddingScaleFactor = _paddingScaleFactor(MediaQuery.of(context).textScaleFactor); + final double paddingScaleFactor = _paddingScaleFactor(MediaQuery.textScaleFactorOf(context)); final TextDirection? textDirection = Directionality.maybeOf(context); Widget? titleWidget; diff --git a/packages/flutter/lib/src/material/drawer.dart b/packages/flutter/lib/src/material/drawer.dart index 2c571af2b4cd..c1d3ea3b5d5e 100644 --- a/packages/flutter/lib/src/material/drawer.dart +++ b/packages/flutter/lib/src/material/drawer.dart @@ -382,11 +382,11 @@ class DrawerController extends StatefulWidget { /// drawer. /// /// By default, the value used is 20.0 added to the padding edge of - /// `MediaQuery.of(context).padding` that corresponds to [alignment]. + /// `MediaQuery.paddingOf(context)` that corresponds to [alignment]. /// This ensures that the drag area for notched devices is not obscured. For /// example, if [alignment] is set to [DrawerAlignment.start] and /// `TextDirection.of(context)` is set to [TextDirection.ltr], - /// 20.0 will be added to `MediaQuery.of(context).padding.left`. + /// 20.0 will be added to `MediaQuery.paddingOf(context).left`. final double? edgeDragWidth; /// Whether or not the drawer is opened or closed. @@ -680,7 +680,6 @@ class DrawerControllerState extends State with SingleTickerPro Widget _buildDrawer(BuildContext context) { final bool drawerIsStart = widget.alignment == DrawerAlignment.start; - final EdgeInsets padding = MediaQuery.of(context).padding; final TextDirection textDirection = Directionality.of(context); final bool isDesktop; switch (Theme.of(context).platform) { @@ -698,6 +697,7 @@ class DrawerControllerState extends State with SingleTickerPro double? dragAreaWidth = widget.edgeDragWidth; if (widget.edgeDragWidth == null) { + final EdgeInsets padding = MediaQuery.paddingOf(context); switch (textDirection) { case TextDirection.ltr: dragAreaWidth = _kEdgeDragWidth + diff --git a/packages/flutter/lib/src/material/drawer_header.dart b/packages/flutter/lib/src/material/drawer_header.dart index bff2c267d87c..3b579d1f5bcd 100644 --- a/packages/flutter/lib/src/material/drawer_header.dart +++ b/packages/flutter/lib/src/material/drawer_header.dart @@ -76,7 +76,7 @@ class DrawerHeader extends StatelessWidget { assert(debugCheckHasMaterial(context)); assert(debugCheckHasMediaQuery(context)); final ThemeData theme = Theme.of(context); - final double statusBarHeight = MediaQuery.of(context).padding.top; + final double statusBarHeight = MediaQuery.paddingOf(context).top; return Container( height: statusBarHeight + _kDrawerHeaderHeight, margin: margin, diff --git a/packages/flutter/lib/src/material/dropdown.dart b/packages/flutter/lib/src/material/dropdown.dart index 6ff22a186145..9becd8def0b1 100644 --- a/packages/flutter/lib/src/material/dropdown.dart +++ b/packages/flutter/lib/src/material/dropdown.dart @@ -1333,7 +1333,7 @@ class _DropdownButtonState extends State> with WidgetsBindi // Similarly, we don't reduce the height of the button so much that its icon // would be clipped. double get _denseButtonHeight { - final double textScaleFactor = MediaQuery.of(context).textScaleFactor; + final double textScaleFactor = MediaQuery.textScaleFactorOf(context); final double fontSize = _textStyle!.fontSize ?? Theme.of(context).textTheme.titleMedium!.fontSize!; final double scaledFontSize = textScaleFactor * fontSize; return math.max(scaledFontSize, math.max(widget.iconSize, _kDenseButtonHeight)); @@ -1369,7 +1369,7 @@ class _DropdownButtonState extends State> with WidgetsBindi bool get _enabled => widget.items != null && widget.items!.isNotEmpty && widget.onChanged != null; Orientation _getOrientation(BuildContext context) { - Orientation? result = MediaQuery.maybeOf(context)?.orientation; + Orientation? result = MediaQuery.maybeOrientationOf(context); if (result == null) { // If there's no MediaQuery, then use the window aspect to determine // orientation. diff --git a/packages/flutter/lib/src/material/elevated_button.dart b/packages/flutter/lib/src/material/elevated_button.dart index 60bb0d35d818..cb98168e2521 100644 --- a/packages/flutter/lib/src/material/elevated_button.dart +++ b/packages/flutter/lib/src/material/elevated_button.dart @@ -252,7 +252,7 @@ class ElevatedButton extends ButtonStyleButton { /// each state, and "others" means all other states. /// /// The `textScaleFactor` is the value of - /// `MediaQuery.of(context).textScaleFactor` and the names of the + /// `MediaQuery.textScaleFactorOf(context)` and the names of the /// EdgeInsets constructors and `EdgeInsetsGeometry.lerp` have been /// abbreviated for readability. /// @@ -394,7 +394,7 @@ EdgeInsetsGeometry _scaledPadding(BuildContext context) { const EdgeInsets.symmetric(horizontal: 16), const EdgeInsets.symmetric(horizontal: 8), const EdgeInsets.symmetric(horizontal: 4), - MediaQuery.maybeOf(context)?.textScaleFactor ?? 1, + MediaQuery.textScaleFactorOf(context), ); } @@ -500,7 +500,7 @@ class _ElevatedButtonWithIcon extends ElevatedButton { const EdgeInsetsDirectional.fromSTEB(12, 0, 16, 0), const EdgeInsets.symmetric(horizontal: 8), const EdgeInsetsDirectional.fromSTEB(8, 0, 4, 0), - MediaQuery.maybeOf(context)?.textScaleFactor ?? 1, + MediaQuery.textScaleFactorOf(context), ); return super.defaultStyleOf(context).copyWith( padding: MaterialStatePropertyAll(scaledPadding), @@ -516,7 +516,7 @@ class _ElevatedButtonWithIconChild extends StatelessWidget { @override Widget build(BuildContext context) { - final double scale = MediaQuery.maybeOf(context)?.textScaleFactor ?? 1; + final double scale = MediaQuery.textScaleFactorOf(context); final double gap = scale <= 1 ? 8 : lerpDouble(8, 4, math.min(scale - 1, 1))!; return Row( mainAxisSize: MainAxisSize.min, diff --git a/packages/flutter/lib/src/material/filled_button.dart b/packages/flutter/lib/src/material/filled_button.dart index 5b7662247f90..fa9793a6de29 100644 --- a/packages/flutter/lib/src/material/filled_button.dart +++ b/packages/flutter/lib/src/material/filled_button.dart @@ -288,7 +288,7 @@ class FilledButton extends ButtonStyleButton { /// each state, and "others" means all other states. /// /// The `textScaleFactor` is the value of - /// `MediaQuery.of(context).textScaleFactor` and the names of the + /// `MediaQuery.textScaleFactorOf(context)` and the names of the /// EdgeInsets constructors and `EdgeInsetsGeometry.lerp` have been /// abbreviated for readability. /// @@ -368,7 +368,7 @@ EdgeInsetsGeometry _scaledPadding(BuildContext context) { const EdgeInsets.symmetric(horizontal: 16), const EdgeInsets.symmetric(horizontal: 8), const EdgeInsets.symmetric(horizontal: 4), - MediaQuery.maybeOf(context)?.textScaleFactor ?? 1, + MediaQuery.textScaleFactorOf(context), ); } @@ -471,7 +471,7 @@ class _FilledButtonWithIcon extends FilledButton { const EdgeInsetsDirectional.fromSTEB(12, 0, 16, 0), const EdgeInsets.symmetric(horizontal: 8), const EdgeInsetsDirectional.fromSTEB(8, 0, 4, 0), - MediaQuery.maybeOf(context)?.textScaleFactor ?? 1, + MediaQuery.textScaleFactorOf(context), ); return super.defaultStyleOf(context).copyWith( padding: MaterialStatePropertyAll(scaledPadding), @@ -487,7 +487,7 @@ class _FilledButtonWithIconChild extends StatelessWidget { @override Widget build(BuildContext context) { - final double scale = MediaQuery.maybeOf(context)?.textScaleFactor ?? 1; + final double scale = MediaQuery.textScaleFactorOf(context); // Adjust the gap based on the text scale factor. Start at 8, and lerp // to 4 based on how large the text is. final double gap = scale <= 1 ? 8 : lerpDouble(8, 4, math.min(scale - 1, 1))!; diff --git a/packages/flutter/lib/src/material/filter_chip.dart b/packages/flutter/lib/src/material/filter_chip.dart index 5119f8fc6350..43440efdcc3e 100644 --- a/packages/flutter/lib/src/material/filter_chip.dart +++ b/packages/flutter/lib/src/material/filter_chip.dart @@ -267,7 +267,7 @@ class _FilterChipDefaultsM3 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), )!; } diff --git a/packages/flutter/lib/src/material/ink_well.dart b/packages/flutter/lib/src/material/ink_well.dart index a5e46985d4d5..bdba4f7cf859 100644 --- a/packages/flutter/lib/src/material/ink_well.dart +++ b/packages/flutter/lib/src/material/ink_well.dart @@ -1018,7 +1018,7 @@ class _InkResponseState extends State<_InkResponseStateWidget> } bool get _shouldShowFocus { - final NavigationMode mode = MediaQuery.maybeOf(context)?.navigationMode ?? NavigationMode.traditional; + final NavigationMode mode = MediaQuery.maybeNavigationModeOf(context) ?? NavigationMode.traditional; switch (mode) { case NavigationMode.traditional: return enabled && _hasFocus; @@ -1166,7 +1166,7 @@ class _InkResponseState extends State<_InkResponseStateWidget> } bool get _canRequestFocus { - final NavigationMode mode = MediaQuery.maybeOf(context)?.navigationMode ?? NavigationMode.traditional; + final NavigationMode mode = MediaQuery.maybeNavigationModeOf(context) ?? NavigationMode.traditional; switch (mode) { case NavigationMode.traditional: return enabled && widget.canRequestFocus; diff --git a/packages/flutter/lib/src/material/input_chip.dart b/packages/flutter/lib/src/material/input_chip.dart index 8efd27faa9c9..fd3b6e0d146b 100644 --- a/packages/flutter/lib/src/material/input_chip.dart +++ b/packages/flutter/lib/src/material/input_chip.dart @@ -310,7 +310,7 @@ class _InputChipDefaultsM3 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), )!; } diff --git a/packages/flutter/lib/src/material/magnifier.dart b/packages/flutter/lib/src/material/magnifier.dart index 64ab312eccfc..ce6b960ba02b 100644 --- a/packages/flutter/lib/src/material/magnifier.dart +++ b/packages/flutter/lib/src/material/magnifier.dart @@ -134,7 +134,7 @@ class _TextMagnifierState extends State { void _determineMagnifierPositionAndFocalPoint() { final MagnifierInfo selectionInfo = widget.magnifierInfo.value; - final Rect screenRect = Offset.zero & MediaQuery.of(context).size; + final Rect screenRect = Offset.zero & MediaQuery.sizeOf(context); // Since by default we draw at the top left corner, this offset // shifts the magnifier so we draw at the center, and then also includes diff --git a/packages/flutter/lib/src/material/menu_anchor.dart b/packages/flutter/lib/src/material/menu_anchor.dart index 8d6958c84aa1..c479f8543718 100644 --- a/packages/flutter/lib/src/material/menu_anchor.dart +++ b/packages/flutter/lib/src/material/menu_anchor.dart @@ -315,7 +315,7 @@ class _MenuAnchorState extends State { _position?.isScrollingNotifier.removeListener(_handleScroll); _position = Scrollable.maybeOf(context)?.position; _position?.isScrollingNotifier.addListener(_handleScroll); - final Size newSize = MediaQuery.of(context).size; + final Size newSize = MediaQuery.sizeOf(context); if (_viewSize != null && newSize != _viewSize) { // Close the menus if the view changes size. _root._close(); @@ -3746,7 +3746,7 @@ class _MenuButtonDefaultsM3 extends ButtonStyle { const EdgeInsets.symmetric(horizontal: 12), const EdgeInsets.symmetric(horizontal: 8), const EdgeInsets.symmetric(horizontal: 4), - MediaQuery.maybeOf(context)?.textScaleFactor ?? 1, + MediaQuery.maybeTextScaleFactorOf(context) ?? 1, ); } } diff --git a/packages/flutter/lib/src/material/navigation_bar.dart b/packages/flutter/lib/src/material/navigation_bar.dart index 54fee797af24..e890eea37564 100644 --- a/packages/flutter/lib/src/material/navigation_bar.dart +++ b/packages/flutter/lib/src/material/navigation_bar.dart @@ -994,7 +994,7 @@ class _ClampTextScaleFactor extends StatelessWidget { Widget build(BuildContext context) { return MediaQuery( data: MediaQuery.of(context).copyWith( - textScaleFactor: clampDouble(MediaQuery.of(context).textScaleFactor, + textScaleFactor: clampDouble(MediaQuery.textScaleFactorOf(context), 0.0, upperLimit, ), diff --git a/packages/flutter/lib/src/material/outlined_button.dart b/packages/flutter/lib/src/material/outlined_button.dart index 0fb39f827e05..d60caaa75ab6 100644 --- a/packages/flutter/lib/src/material/outlined_button.dart +++ b/packages/flutter/lib/src/material/outlined_button.dart @@ -351,7 +351,7 @@ EdgeInsetsGeometry _scaledPadding(BuildContext context) { const EdgeInsets.symmetric(horizontal: 16), const EdgeInsets.symmetric(horizontal: 8), const EdgeInsets.symmetric(horizontal: 4), - MediaQuery.maybeOf(context)?.textScaleFactor ?? 1, + MediaQuery.textScaleFactorOf(context), ); } @@ -437,7 +437,7 @@ class _OutlinedButtonWithIconChild extends StatelessWidget { @override Widget build(BuildContext context) { - final double scale = MediaQuery.maybeOf(context)?.textScaleFactor ?? 1; + final double scale = MediaQuery.textScaleFactorOf(context); final double gap = scale <= 1 ? 8 : lerpDouble(8, 4, math.min(scale - 1, 1))!; return Row( mainAxisSize: MainAxisSize.min, diff --git a/packages/flutter/lib/src/material/popup_menu.dart b/packages/flutter/lib/src/material/popup_menu.dart index a48ad18b9b9d..0e2645052639 100644 --- a/packages/flutter/lib/src/material/popup_menu.dart +++ b/packages/flutter/lib/src/material/popup_menu.dart @@ -1292,7 +1292,7 @@ class PopupMenuButtonState extends State> { } bool get _canRequestFocus { - final NavigationMode mode = MediaQuery.maybeOf(context)?.navigationMode ?? NavigationMode.traditional; + final NavigationMode mode = MediaQuery.maybeNavigationModeOf(context) ?? NavigationMode.traditional; switch (mode) { case NavigationMode.traditional: return widget.enabled; diff --git a/packages/flutter/lib/src/material/range_slider.dart b/packages/flutter/lib/src/material/range_slider.dart index f9fca210c7a1..fd26eda179f5 100644 --- a/packages/flutter/lib/src/material/range_slider.dart +++ b/packages/flutter/lib/src/material/range_slider.dart @@ -619,7 +619,7 @@ class _RangeSliderState extends State with TickerProviderStateMixin // This size is used as the max bounds for the painting of the value // indicators. It must be kept in sync with the function with the same name // in slider.dart. - Size screenSize() => MediaQuery.of(context).size; + Size screenSize() => MediaQuery.sizeOf(context); return CompositedTransformTarget( link: _layerLink, @@ -628,7 +628,7 @@ class _RangeSliderState extends State with TickerProviderStateMixin divisions: widget.divisions, labels: widget.labels, sliderTheme: sliderTheme, - textScaleFactor: MediaQuery.of(context).textScaleFactor, + textScaleFactor: MediaQuery.textScaleFactorOf(context), screenSize: screenSize(), onChanged: (widget.onChanged != null) && (widget.max > widget.min) ? _handleChanged : null, onChangeStart: widget.onChangeStart != null ? _handleDragStart : null, @@ -704,7 +704,7 @@ class _RangeSliderRenderObjectWidget extends LeafRenderObjectWidget { textDirection: Directionality.of(context), semanticFormatterCallback: semanticFormatterCallback, platform: Theme.of(context).platform, - gestureSettings: MediaQuery.of(context).gestureSettings, + gestureSettings: MediaQuery.gestureSettingsOf(context), ); } @@ -726,7 +726,7 @@ class _RangeSliderRenderObjectWidget extends LeafRenderObjectWidget { ..textDirection = Directionality.of(context) ..semanticFormatterCallback = semanticFormatterCallback ..platform = Theme.of(context).platform - ..gestureSettings = MediaQuery.of(context).gestureSettings; + ..gestureSettings = MediaQuery.gestureSettingsOf(context); } } diff --git a/packages/flutter/lib/src/material/scaffold.dart b/packages/flutter/lib/src/material/scaffold.dart index 650c0c12e5ed..3e6c02b8ac7e 100644 --- a/packages/flutter/lib/src/material/scaffold.dart +++ b/packages/flutter/lib/src/material/scaffold.dart @@ -191,18 +191,18 @@ class ScaffoldMessengerState extends State with TickerProvide @override void didChangeDependencies() { - final MediaQueryData mediaQuery = MediaQuery.of(context); + final bool accessibleNavigation = MediaQuery.accessibleNavigationOf(context); // If we transition from accessible navigation to non-accessible navigation // and there is a SnackBar that would have timed out that has already // completed its timer, dismiss that SnackBar. If the timer hasn't finished // yet, let it timeout as normal. if ((_accessibleNavigation ?? false) - && !mediaQuery.accessibleNavigation + && !accessibleNavigation && _snackBarTimer != null && !_snackBarTimer!.isActive) { hideCurrentSnackBar(reason: SnackBarClosedReason.timeout); } - _accessibleNavigation = mediaQuery.accessibleNavigation; + _accessibleNavigation = accessibleNavigation; super.didChangeDependencies(); } @@ -568,8 +568,7 @@ class ScaffoldMessengerState extends State with TickerProvide @override Widget build(BuildContext context) { assert(debugCheckHasMediaQuery(context)); - final MediaQueryData mediaQuery = MediaQuery.of(context); - _accessibleNavigation = mediaQuery.accessibleNavigation; + _accessibleNavigation = MediaQuery.accessibleNavigationOf(context); if (_snackBars.isNotEmpty) { final ModalRoute? route = ModalRoute.of(context); @@ -582,8 +581,7 @@ class ScaffoldMessengerState extends State with TickerProvide _snackBarController!.status == AnimationStatus.completed, ); // Look up MediaQuery again in case the setting changed. - final MediaQueryData mediaQuery = MediaQuery.of(context); - if (mediaQuery.accessibleNavigation && snackBar.action != null) { + if (snackBar.action != null && MediaQuery.accessibleNavigationOf(context)) { return; } hideCurrentSnackBar(reason: SnackBarClosedReason.timeout); @@ -1824,11 +1822,11 @@ class Scaffold extends StatefulWidget { /// drawer. /// /// By default, the value used is 20.0 added to the padding edge of - /// `MediaQuery.of(context).padding` that corresponds to the surrounding + /// `MediaQuery.paddingOf(context)` that corresponds to the surrounding /// [TextDirection]. This ensures that the drag area for notched devices is /// not obscured. For example, if `TextDirection.of(context)` is set to /// [TextDirection.ltr], 20.0 will be added to - /// `MediaQuery.of(context).padding.left`. + /// `MediaQuery.paddingOf(context).left`. final double? drawerEdgeDragWidth; /// Determines if the [Scaffold.drawer] can be opened with a drag @@ -2761,7 +2759,6 @@ class ScaffoldState extends State with TickerProviderStateMixin, Resto Widget build(BuildContext context) { assert(debugCheckHasMediaQuery(context)); assert(debugCheckHasDirectionality(context)); - final MediaQueryData mediaQuery = MediaQuery.of(context); final ThemeData themeData = Theme.of(context); final TextDirection textDirection = Directionality.of(context); @@ -2796,7 +2793,7 @@ class ScaffoldState extends State with TickerProviderStateMixin, Resto } if (widget.appBar != null) { - final double topPadding = widget.primary ? mediaQuery.padding.top : 0.0; + final double topPadding = widget.primary ? MediaQuery.paddingOf(context).top : 0.0; _appBarMaxHeight = AppBar.preferredHeightFor(context, widget.appBar!.preferredSize) + topPadding; assert(_appBarMaxHeight! >= 0.0 && _appBarMaxHeight!.isFinite); _addIfNonNull( @@ -2973,14 +2970,14 @@ class ScaffoldState extends State with TickerProviderStateMixin, Resto } // The minimum insets for contents of the Scaffold to keep visible. - final EdgeInsets minInsets = mediaQuery.padding.copyWith( - bottom: _resizeToAvoidBottomInset ? mediaQuery.viewInsets.bottom : 0.0, + final EdgeInsets minInsets = MediaQuery.paddingOf(context).copyWith( + bottom: _resizeToAvoidBottomInset ? MediaQuery.viewInsetsOf(context).bottom : 0.0, ); // The minimum viewPadding for interactive elements positioned by the // Scaffold to keep within safe interactive areas. - final EdgeInsets minViewPadding = mediaQuery.viewPadding.copyWith( - bottom: _resizeToAvoidBottomInset && mediaQuery.viewInsets.bottom != 0.0 ? 0.0 : null, + final EdgeInsets minViewPadding = MediaQuery.viewPaddingOf(context).copyWith( + bottom: _resizeToAvoidBottomInset && MediaQuery.viewInsetsOf(context).bottom != 0.0 ? 0.0 : null, ); // extendBody locked when keyboard is open diff --git a/packages/flutter/lib/src/material/scrollbar.dart b/packages/flutter/lib/src/material/scrollbar.dart index e282ccc02e8f..bf1be77054f0 100644 --- a/packages/flutter/lib/src/material/scrollbar.dart +++ b/packages/flutter/lib/src/material/scrollbar.dart @@ -427,7 +427,7 @@ class _MaterialScrollbarState extends RawScrollbarState<_MaterialScrollbar> { ..crossAxisMargin = _scrollbarTheme.crossAxisMargin ?? (_useAndroidScrollbar ? 0.0 : _kScrollbarMargin) ..mainAxisMargin = _scrollbarTheme.mainAxisMargin ?? 0.0 ..minLength = _scrollbarTheme.minThumbLength ?? _kScrollbarMinLength - ..padding = MediaQuery.of(context).padding + ..padding = MediaQuery.paddingOf(context) ..scrollbarOrientation = widget.scrollbarOrientation ..ignorePointer = !enableGestures; } diff --git a/packages/flutter/lib/src/material/selectable_text.dart b/packages/flutter/lib/src/material/selectable_text.dart index ccbe7a8e1f93..aeb5a3a36711 100644 --- a/packages/flutter/lib/src/material/selectable_text.dart +++ b/packages/flutter/lib/src/material/selectable_text.dart @@ -659,7 +659,7 @@ class _SelectableTextState extends State implements TextSelectio cursorColor = widget.cursorColor ?? selectionStyle.cursorColor ?? cupertinoTheme.primaryColor; selectionColor = selectionStyle.selectionColor ?? cupertinoTheme.primaryColor.withOpacity(0.40); cursorRadius ??= const Radius.circular(2.0); - cursorOffset = Offset(iOSHorizontalOffset / MediaQuery.of(context).devicePixelRatio, 0); + cursorOffset = Offset(iOSHorizontalOffset / MediaQuery.devicePixelRatioOf(context), 0); break; case TargetPlatform.macOS: @@ -671,7 +671,7 @@ class _SelectableTextState extends State implements TextSelectio cursorColor = widget.cursorColor ?? selectionStyle.cursorColor ?? cupertinoTheme.primaryColor; selectionColor = selectionStyle.selectionColor ?? cupertinoTheme.primaryColor.withOpacity(0.40); cursorRadius ??= const Radius.circular(2.0); - cursorOffset = Offset(iOSHorizontalOffset / MediaQuery.of(context).devicePixelRatio, 0); + cursorOffset = Offset(iOSHorizontalOffset / MediaQuery.devicePixelRatioOf(context), 0); break; case TargetPlatform.android: @@ -700,7 +700,7 @@ class _SelectableTextState extends State implements TextSelectio if (effectiveTextStyle == null || effectiveTextStyle.inherit) { effectiveTextStyle = defaultTextStyle.style.merge(widget.style ?? _controller._textSpan.style); } - if (MediaQuery.boldTextOverride(context)) { + if (MediaQuery.boldTextOf(context)) { effectiveTextStyle = effectiveTextStyle.merge(const TextStyle(fontWeight: FontWeight.bold)); } final Widget child = RepaintBoundary( diff --git a/packages/flutter/lib/src/material/slider.dart b/packages/flutter/lib/src/material/slider.dart index 3703f9030d53..5cae288f7d68 100644 --- a/packages/flutter/lib/src/material/slider.dart +++ b/packages/flutter/lib/src/material/slider.dart @@ -826,7 +826,7 @@ class _SliderState extends State with TickerProviderStateMixin { // This size is used as the max bounds for the painting of the value // indicators It must be kept in sync with the function with the same name // in range_slider.dart. - Size screenSize() => MediaQuery.of(context).size; + Size screenSize() => MediaQuery.sizeOf(context); VoidCallback? handleDidGainAccessibilityFocus; switch (theme.platform) { @@ -847,7 +847,7 @@ class _SliderState extends State with TickerProviderStateMixin { } final Map shortcutMap; - switch (MediaQuery.of(context).navigationMode) { + switch (MediaQuery.navigationModeOf(context)) { case NavigationMode.directional: shortcutMap = _directionalNavShortcutMap; break; @@ -861,8 +861,8 @@ class _SliderState extends State with TickerProviderStateMixin { // This needs to be updated when accessibility // guidelines are available on the material specs page // https://m3.material.io/components/sliders/accessibility. - ? math.min(MediaQuery.of(context).textScaleFactor, 1.3) - : MediaQuery.of(context).textScaleFactor; + ? math.min(MediaQuery.textScaleFactorOf(context), 1.3) + : MediaQuery.textScaleFactorOf(context); return Semantics( container: true, @@ -994,7 +994,7 @@ class _SliderRenderObjectWidget extends LeafRenderObjectWidget { platform: Theme.of(context).platform, hasFocus: hasFocus, hovering: hovering, - gestureSettings: MediaQuery.of(context).gestureSettings, + gestureSettings: MediaQuery.gestureSettingsOf(context), ); } @@ -1018,7 +1018,7 @@ class _SliderRenderObjectWidget extends LeafRenderObjectWidget { ..platform = Theme.of(context).platform ..hasFocus = hasFocus ..hovering = hovering - ..gestureSettings = MediaQuery.of(context).gestureSettings; + ..gestureSettings = MediaQuery.gestureSettingsOf(context); // Ticker provider cannot change since there's a 1:1 relationship between // the _SliderRenderObjectWidget object and the _SliderState object. } diff --git a/packages/flutter/lib/src/material/snack_bar.dart b/packages/flutter/lib/src/material/snack_bar.dart index 6dfea7e2dac4..993450fbbff6 100644 --- a/packages/flutter/lib/src/material/snack_bar.dart +++ b/packages/flutter/lib/src/material/snack_bar.dart @@ -485,7 +485,7 @@ class _SnackBarState extends State { @override Widget build(BuildContext context) { assert(debugCheckHasMediaQuery(context)); - final MediaQueryData mediaQueryData = MediaQuery.of(context); + final bool accessibleNavigation = MediaQuery.accessibleNavigationOf(context); assert(widget.animation != null); final ThemeData theme = Theme.of(context); final ColorScheme colorScheme = theme.colorScheme; @@ -599,7 +599,7 @@ class _SnackBarState extends State { final EdgeInsets margin = widget.margin?.resolve(TextDirection.ltr) ?? snackBarTheme.insetPadding ?? defaults.insetPadding!; - final double snackBarWidth = widget.width ?? mediaQueryData.size.width - (margin.left + margin.right); + final double snackBarWidth = widget.width ?? MediaQuery.sizeOf(context).width - (margin.left + margin.right); // Action and Icon will overflow to a new line if their width is greater // than one quarter of the total Snack Bar width. final bool actionLineOverflow = @@ -675,7 +675,7 @@ class _SnackBarState extends State { color: backgroundColor, child: Theme( data: effectiveTheme, - child: mediaQueryData.accessibleNavigation || theme.useMaterial3 + child: accessibleNavigation || theme.useMaterial3 ? snackBar : FadeTransition( opacity: fadeOutAnimation, @@ -723,7 +723,7 @@ class _SnackBarState extends State { ); final Widget snackBarTransition; - if (mediaQueryData.accessibleNavigation) { + if (accessibleNavigation) { snackBarTransition = snackBar; } else if (isFloatingSnackBar && !theme.useMaterial3) { snackBarTransition = FadeTransition( diff --git a/packages/flutter/lib/src/material/text_button.dart b/packages/flutter/lib/src/material/text_button.dart index 75afee366339..03e07176eb0a 100644 --- a/packages/flutter/lib/src/material/text_button.dart +++ b/packages/flutter/lib/src/material/text_button.dart @@ -249,7 +249,7 @@ class TextButton extends ButtonStyleButton { /// each state and "others" means all other states. /// /// The `textScaleFactor` is the value of - /// `MediaQuery.of(context).textScaleFactor` and the names of the + /// `MediaQuery.textScaleFactorOf(context)` and the names of the /// EdgeInsets constructors and `EdgeInsetsGeometry.lerp` have been /// abbreviated for readability. /// @@ -382,7 +382,7 @@ EdgeInsetsGeometry _scaledPadding(BuildContext context) { const EdgeInsets.all(8), const EdgeInsets.symmetric(horizontal: 8), const EdgeInsets.symmetric(horizontal: 4), - MediaQuery.maybeOf(context)?.textScaleFactor ?? 1, + MediaQuery.textScaleFactorOf(context), ); } @@ -495,7 +495,7 @@ class _TextButtonWithIcon extends TextButton { const EdgeInsets.all(8), const EdgeInsets.symmetric(horizontal: 4), const EdgeInsets.symmetric(horizontal: 4), - MediaQuery.maybeOf(context)?.textScaleFactor ?? 1, + MediaQuery.textScaleFactorOf(context), ); return super.defaultStyleOf(context).copyWith( padding: MaterialStatePropertyAll(scaledPadding), @@ -514,7 +514,7 @@ class _TextButtonWithIconChild extends StatelessWidget { @override Widget build(BuildContext context) { - final double scale = MediaQuery.maybeOf(context)?.textScaleFactor ?? 1; + final double scale = MediaQuery.textScaleFactorOf(context); final double gap = scale <= 1 ? 8 : lerpDouble(8, 4, math.min(scale - 1, 1))!; return Row( mainAxisSize: MainAxisSize.min, diff --git a/packages/flutter/lib/src/material/text_field.dart b/packages/flutter/lib/src/material/text_field.dart index c72828c92a28..4da189a647e9 100644 --- a/packages/flutter/lib/src/material/text_field.dart +++ b/packages/flutter/lib/src/material/text_field.dart @@ -967,7 +967,7 @@ class _TextFieldState extends State with RestorationMixin implements } bool get _canRequestFocus { - final NavigationMode mode = MediaQuery.maybeOf(context)?.navigationMode ?? NavigationMode.traditional; + final NavigationMode mode = MediaQuery.maybeNavigationModeOf(context) ?? NavigationMode.traditional; switch (mode) { case NavigationMode.traditional: return _isEnabled; @@ -1215,7 +1215,7 @@ class _TextFieldState extends State with RestorationMixin implements cursorColor = widget.cursorColor ?? selectionStyle.cursorColor ?? cupertinoTheme.primaryColor; selectionColor = selectionStyle.selectionColor ?? cupertinoTheme.primaryColor.withOpacity(0.40); cursorRadius ??= const Radius.circular(2.0); - cursorOffset = Offset(iOSHorizontalOffset / MediaQuery.of(context).devicePixelRatio, 0); + cursorOffset = Offset(iOSHorizontalOffset / MediaQuery.devicePixelRatioOf(context), 0); autocorrectionTextRectColor = selectionColor; break; @@ -1228,7 +1228,7 @@ class _TextFieldState extends State with RestorationMixin implements cursorColor = widget.cursorColor ?? selectionStyle.cursorColor ?? cupertinoTheme.primaryColor; selectionColor = selectionStyle.selectionColor ?? cupertinoTheme.primaryColor.withOpacity(0.40); cursorRadius ??= const Radius.circular(2.0); - cursorOffset = Offset(iOSHorizontalOffset / MediaQuery.of(context).devicePixelRatio, 0); + cursorOffset = Offset(iOSHorizontalOffset / MediaQuery.devicePixelRatioOf(context), 0); handleDidGainAccessibilityFocus = () { // Automatically activate the TextField when it receives accessibility focus. if (!_effectiveFocusNode.hasFocus && _effectiveFocusNode.canRequestFocus) { diff --git a/packages/flutter/lib/src/material/text_selection_toolbar.dart b/packages/flutter/lib/src/material/text_selection_toolbar.dart index a024968c389d..19bf219dffa2 100644 --- a/packages/flutter/lib/src/material/text_selection_toolbar.dart +++ b/packages/flutter/lib/src/material/text_selection_toolbar.dart @@ -99,7 +99,7 @@ class TextSelectionToolbar extends StatelessWidget { final Offset anchorBelowPadded = anchorBelow + const Offset(0.0, _kToolbarContentDistanceBelow); - final double paddingAbove = MediaQuery.of(context).padding.top + final double paddingAbove = MediaQuery.paddingOf(context).top + _kToolbarScreenPadding; final double availableHeight = anchorAbovePadded.dy - _kToolbarContentDistance - paddingAbove; final bool fitsAbove = _kToolbarHeight <= availableHeight; diff --git a/packages/flutter/lib/src/material/time.dart b/packages/flutter/lib/src/material/time.dart index 5b9e31945a66..679e80a2616e 100644 --- a/packages/flutter/lib/src/material/time.dart +++ b/packages/flutter/lib/src/material/time.dart @@ -108,7 +108,7 @@ class TimeOfDay { final MaterialLocalizations localizations = MaterialLocalizations.of(context); return localizations.formatTimeOfDay( this, - alwaysUse24HourFormat: MediaQuery.of(context).alwaysUse24HourFormat, + alwaysUse24HourFormat: MediaQuery.alwaysUse24HourFormatOf(context), ); } diff --git a/packages/flutter/lib/src/material/time_picker.dart b/packages/flutter/lib/src/material/time_picker.dart index b9f33ea6bd9b..bc9f517ef822 100644 --- a/packages/flutter/lib/src/material/time_picker.dart +++ b/packages/flutter/lib/src/material/time_picker.dart @@ -149,7 +149,7 @@ class _TimePickerHeader extends StatelessWidget { assert(debugCheckHasMediaQuery(context)); final ThemeData themeData = Theme.of(context); final TimeOfDayFormat timeOfDayFormat = MaterialLocalizations.of(context).timeOfDayFormat( - alwaysUse24HourFormat: MediaQuery.of(context).alwaysUse24HourFormat, + alwaysUse24HourFormat: MediaQuery.alwaysUse24HourFormatOf(context), ); final MaterialLocalizations localizations = MaterialLocalizations.of(context); final String timePickerDialHelpText = themeData.useMaterial3 @@ -339,7 +339,7 @@ class _HourControl extends StatelessWidget { @override Widget build(BuildContext context) { assert(debugCheckHasMediaQuery(context)); - final bool alwaysUse24HourFormat = MediaQuery.of(context).alwaysUse24HourFormat; + final bool alwaysUse24HourFormat = MediaQuery.alwaysUse24HourFormatOf(context); final MaterialLocalizations localizations = MaterialLocalizations.of(context); final String formattedHour = localizations.formatHour( fragmentContext.selectedTime, @@ -581,7 +581,7 @@ class _DayPeriodControl extends StatelessWidget { side: borderSide, ); - final double buttonTextScaleFactor = math.min(MediaQuery.of(context).textScaleFactor, 2.0); + final double buttonTextScaleFactor = math.min(MediaQuery.textScaleFactorOf(context), 2.0); final Widget amButton = Material( color: MaterialStateProperty.resolveAs(backgroundColor, amStates), @@ -990,7 +990,7 @@ class _DialState extends State<_Dial> with SingleTickerProviderStateMixin { late ThemeData themeData; late MaterialLocalizations localizations; - late MediaQueryData media; + late bool alwaysUse24HourFormat; _DialPainter? painter; @override @@ -999,7 +999,7 @@ class _DialState extends State<_Dial> with SingleTickerProviderStateMixin { assert(debugCheckHasMediaQuery(context)); themeData = Theme.of(context); localizations = MaterialLocalizations.of(context); - media = MediaQuery.of(context); + alwaysUse24HourFormat = MediaQuery.alwaysUse24HourFormatOf(context); } @override @@ -1209,7 +1209,7 @@ class _DialState extends State<_Dial> with SingleTickerProviderStateMixin { _TappableLabel _buildTappableLabel(TextTheme textTheme, Color color, int value, String label, VoidCallback onTap) { final TextStyle style = textTheme.bodyLarge!.copyWith(color: color); - final double labelScaleFactor = math.min(MediaQuery.of(context).textScaleFactor, 2.0); + final double labelScaleFactor = math.min(MediaQuery.textScaleFactorOf(context), 2.0); return _TappableLabel( value: value, painter: TextPainter( @@ -1227,7 +1227,7 @@ class _DialState extends State<_Dial> with SingleTickerProviderStateMixin { textTheme, color, timeOfDay.hour, - localizations.formatHour(timeOfDay, alwaysUse24HourFormat: media.alwaysUse24HourFormat), + localizations.formatHour(timeOfDay, alwaysUse24HourFormat: alwaysUse24HourFormat), () { _selectHour(timeOfDay.hour); }, @@ -1240,7 +1240,7 @@ class _DialState extends State<_Dial> with SingleTickerProviderStateMixin { textTheme, color, timeOfDay.hour, - localizations.formatHour(timeOfDay, alwaysUse24HourFormat: media.alwaysUse24HourFormat), + localizations.formatHour(timeOfDay, alwaysUse24HourFormat: alwaysUse24HourFormat), () { _selectHour(timeOfDay.hour); }, @@ -1406,7 +1406,7 @@ class _TimePickerInputState extends State<_TimePickerInput> with RestorationMixi return null; } - if (MediaQuery.of(context).alwaysUse24HourFormat) { + if (MediaQuery.alwaysUse24HourFormatOf(context)) { if (newHour >= 0 && newHour < 24) { return newHour; } @@ -1494,8 +1494,7 @@ class _TimePickerInputState extends State<_TimePickerInput> with RestorationMixi @override Widget build(BuildContext context) { assert(debugCheckHasMediaQuery(context)); - final MediaQueryData media = MediaQuery.of(context); - final TimeOfDayFormat timeOfDayFormat = MaterialLocalizations.of(context).timeOfDayFormat(alwaysUse24HourFormat: media.alwaysUse24HourFormat); + final TimeOfDayFormat timeOfDayFormat = MaterialLocalizations.of(context).timeOfDayFormat(alwaysUse24HourFormat: MediaQuery.alwaysUse24HourFormatOf(context)); final bool use24HourDials = hourFormat(of: timeOfDayFormat) != HourFormat.h; final ThemeData theme = Theme.of(context); final TextStyle hourMinuteStyle = TimePickerTheme.of(context).hourMinuteTextStyle ?? theme.textTheme.displayMedium!; @@ -1759,7 +1758,7 @@ class _HourMinuteTextFieldState extends State<_HourMinuteTextField> with Restora } String get _formattedValue { - final bool alwaysUse24HourFormat = MediaQuery.of(context).alwaysUse24HourFormat; + final bool alwaysUse24HourFormat = MediaQuery.alwaysUse24HourFormatOf(context); final MaterialLocalizations localizations = MaterialLocalizations.of(context); return !widget.isHour ? localizations.formatMinute(widget.selectedTime) : localizations.formatHour( widget.selectedTime, @@ -1805,7 +1804,7 @@ class _HourMinuteTextFieldState extends State<_HourMinuteTextField> with Restora // // TODO(rami-a): Once https://github.com/flutter/flutter/issues/67571 is // resolved, remove the window check for semantics being enabled on web. - final String? hintText = MediaQuery.of(context).accessibleNavigation || WidgetsBinding.instance.window.semanticsEnabled + final String? hintText = MediaQuery.accessibleNavigationOf(context) || WidgetsBinding.instance.window.semanticsEnabled ? widget.semanticHintText : (focusNode.hasFocus ? null : _formattedValue); inputDecoration = inputDecoration.copyWith( @@ -2146,11 +2145,10 @@ class _TimePickerDialogState extends State with RestorationMix return; } - final MediaQueryData media = MediaQuery.of(context); final MaterialLocalizations localizations = MaterialLocalizations.of(context); _announceToAccessibility( context, - localizations.formatTimeOfDay(widget.initialTime, alwaysUse24HourFormat: media.alwaysUse24HourFormat), + localizations.formatTimeOfDay(widget.initialTime, alwaysUse24HourFormat: MediaQuery.alwaysUse24HourFormatOf(context)), ); _announcedInitialTime.value = true; } @@ -2195,12 +2193,12 @@ class _TimePickerDialogState extends State with RestorationMix } Size _dialogSize(BuildContext context) { - final Orientation orientation = MediaQuery.of(context).orientation; + final Orientation orientation = MediaQuery.orientationOf(context); final ThemeData theme = Theme.of(context); // Constrain the textScaleFactor to prevent layout issues. Since only some // parts of the time picker scale up with textScaleFactor, we cap the factor // to 1.1 as that provides enough space to reasonably fit all the content. - final double textScaleFactor = math.min(MediaQuery.of(context).textScaleFactor, 1.1); + final double textScaleFactor = math.min(MediaQuery.textScaleFactorOf(context), 1.1); final double timePickerWidth; final double timePickerHeight; @@ -2234,12 +2232,11 @@ class _TimePickerDialogState extends State with RestorationMix @override Widget build(BuildContext context) { assert(debugCheckHasMediaQuery(context)); - final MediaQueryData media = MediaQuery.of(context); - final TimeOfDayFormat timeOfDayFormat = localizations.timeOfDayFormat(alwaysUse24HourFormat: media.alwaysUse24HourFormat); + final TimeOfDayFormat timeOfDayFormat = localizations.timeOfDayFormat(alwaysUse24HourFormat: MediaQuery.alwaysUse24HourFormatOf(context)); final bool use24HourDials = hourFormat(of: timeOfDayFormat) != HourFormat.h; final ThemeData theme = Theme.of(context); final ShapeBorder shape = TimePickerTheme.of(context).shape ?? _kDefaultShape; - final Orientation orientation = media.orientation; + final Orientation orientation = MediaQuery.orientationOf(context); final Widget actions = Row( children: [ diff --git a/packages/flutter/lib/src/material/tooltip.dart b/packages/flutter/lib/src/material/tooltip.dart index 344514d29597..b0be65d286ef 100644 --- a/packages/flutter/lib/src/material/tooltip.dart +++ b/packages/flutter/lib/src/material/tooltip.dart @@ -884,7 +884,7 @@ class _TooltipOverlay extends StatelessWidget { ); } return Positioned.fill( - bottom: MediaQuery.maybeOf(context)?.viewInsets.bottom ?? 0.0, + bottom: MediaQuery.maybeViewInsetsOf(context)?.bottom ?? 0.0, child: CustomSingleChildLayout( delegate: _TooltipPositionDelegate( target: target, diff --git a/packages/flutter/lib/src/widgets/actions.dart b/packages/flutter/lib/src/widgets/actions.dart index 262ecf212d47..664971b46682 100644 --- a/packages/flutter/lib/src/widgets/actions.dart +++ b/packages/flutter/lib/src/widgets/actions.dart @@ -1230,7 +1230,7 @@ class _FocusableActionDetectorState extends State { } bool canRequestFocus(FocusableActionDetector target) { - final NavigationMode mode = MediaQuery.maybeOf(context)?.navigationMode ?? NavigationMode.traditional; + final NavigationMode mode = MediaQuery.maybeNavigationModeOf(context) ?? NavigationMode.traditional; switch (mode) { case NavigationMode.traditional: return target.enabled; @@ -1271,7 +1271,7 @@ class _FocusableActionDetectorState extends State { } bool get _canRequestFocus { - final NavigationMode mode = MediaQuery.maybeOf(context)?.navigationMode ?? NavigationMode.traditional; + final NavigationMode mode = MediaQuery.maybeNavigationModeOf(context) ?? NavigationMode.traditional; switch (mode) { case NavigationMode.traditional: return widget.enabled; diff --git a/packages/flutter/lib/src/widgets/drag_target.dart b/packages/flutter/lib/src/widgets/drag_target.dart index 259603dc12d0..b3c280a23d9f 100644 --- a/packages/flutter/lib/src/widgets/drag_target.dart +++ b/packages/flutter/lib/src/widgets/drag_target.dart @@ -447,7 +447,7 @@ class _DraggableState extends State> { @override void didChangeDependencies() { - _recognizer!.gestureSettings = MediaQuery.maybeOf(context)?.gestureSettings; + _recognizer!.gestureSettings = MediaQuery.maybeGestureSettingsOf(context); super.didChangeDependencies(); } diff --git a/packages/flutter/lib/src/widgets/editable_text.dart b/packages/flutter/lib/src/widgets/editable_text.dart index bc17cce872a0..e782b44881ff 100644 --- a/packages/flutter/lib/src/widgets/editable_text.dart +++ b/packages/flutter/lib/src/widgets/editable_text.dart @@ -2409,7 +2409,7 @@ class EditableTextState extends State with AutomaticKeepAliveClien } // Hide the text selection toolbar on mobile when orientation changes. - final Orientation orientation = MediaQuery.of(context).orientation; + final Orientation orientation = MediaQuery.orientationOf(context); if (_lastOrientation == null) { _lastOrientation = orientation; return; @@ -3583,7 +3583,7 @@ class EditableTextState extends State with AutomaticKeepAliveClien @override TextEditingValue get textEditingValue => _value; - double get _devicePixelRatio => MediaQuery.of(context).devicePixelRatio; + double get _devicePixelRatio => MediaQuery.devicePixelRatioOf(context); @override void userUpdateTextEditingValue(TextEditingValue value, SelectionChangedCause? cause) { diff --git a/packages/flutter/lib/src/widgets/gesture_detector.dart b/packages/flutter/lib/src/widgets/gesture_detector.dart index 20006d9f4571..098634dee50c 100644 --- a/packages/flutter/lib/src/widgets/gesture_detector.dart +++ b/packages/flutter/lib/src/widgets/gesture_detector.dart @@ -1017,7 +1017,7 @@ class GestureDetector extends StatelessWidget { @override Widget build(BuildContext context) { final Map gestures = {}; - final DeviceGestureSettings? gestureSettings = MediaQuery.maybeOf(context)?.gestureSettings; + final DeviceGestureSettings? gestureSettings = MediaQuery.maybeGestureSettingsOf(context); if (onTapDown != null || onTapUp != null || diff --git a/packages/flutter/lib/src/widgets/image.dart b/packages/flutter/lib/src/widgets/image.dart index b78300494c53..4336f0ab3f61 100644 --- a/packages/flutter/lib/src/widgets/image.dart +++ b/packages/flutter/lib/src/widgets/image.dart @@ -53,7 +53,7 @@ export 'package:flutter/painting.dart' show ImageConfiguration createLocalImageConfiguration(BuildContext context, { Size? size }) { return ImageConfiguration( bundle: DefaultAssetBundle.of(context), - devicePixelRatio: MediaQuery.maybeOf(context)?.devicePixelRatio ?? 1.0, + devicePixelRatio: MediaQuery.maybeDevicePixelRatioOf(context) ?? 1.0, locale: Localizations.maybeLocaleOf(context), textDirection: Directionality.maybeOf(context), size: size, @@ -1106,7 +1106,7 @@ class _ImageState extends State with WidgetsBindingObserver { } void _updateInvertColors() { - _invertColors = MediaQuery.maybeOf(context)?.invertColors + _invertColors = MediaQuery.maybeInvertColorsOf(context) ?? SemanticsBinding.instance.accessibilityFeatures.invertColors; } diff --git a/packages/flutter/lib/src/widgets/media_query.dart b/packages/flutter/lib/src/widgets/media_query.dart index af6658954c13..a8fdda349ddd 100644 --- a/packages/flutter/lib/src/widgets/media_query.dart +++ b/packages/flutter/lib/src/widgets/media_query.dart @@ -12,6 +12,7 @@ import 'basic.dart'; import 'binding.dart'; import 'debug.dart'; import 'framework.dart'; +import 'inherited_model.dart'; // Examples can assume: // late BuildContext context; @@ -25,6 +26,52 @@ enum Orientation { landscape } +/// Specifies a part of MediaQueryData to depend on. +/// +/// [MediaQuery] contains a large number of related properties. Widgets frequently +/// depend on only a few of these attributes. For example, a widget that needs to +/// rebuild when the [MediaQueryData.textScaleFactor] changes does not need to +/// be notified when the [MediaQueryData.size] changes. Specifying an aspect avoids +/// unnecessary rebuilds. +enum _MediaQueryAspect { + /// Specifies the aspect corresponding to [MediaQueryData.size]. + size, + /// Specifies the aspect corresponding to [MediaQueryData.orientation]. + orientation, + /// Specifies the aspect corresponding to [MediaQueryData.devicePixelRatio]. + devicePixelRatio, + /// Specifies the aspect corresponding to [MediaQueryData.textScaleFactor]. + textScaleFactor, + /// Specifies the aspect corresponding to [MediaQueryData.platformBrightness]. + platformBrightness, + /// Specifies the aspect corresponding to [MediaQueryData.padding]. + padding, + /// Specifies the aspect corresponding to [MediaQueryData.viewInsets]. + viewInsets, + /// Specifies the aspect corresponding to [MediaQueryData.systemGestureInsets]. + systemGestureInsets, + /// Specifies the aspect corresponding to [MediaQueryData.viewPadding]. + viewPadding, + /// Specifies the aspect corresponding to [MediaQueryData.alwaysUse24HourFormat]. + alwaysUse24HourFormat, + /// Specifies the aspect corresponding to [MediaQueryData.accessibleNavigation]. + accessibleNavigation, + /// Specifies the aspect corresponding to [MediaQueryData.invertColors]. + invertColors, + /// Specifies the aspect corresponding to [MediaQueryData.highContrast]. + highContrast, + /// Specifies the aspect corresponding to [MediaQueryData.disableAnimations]. + disableAnimations, + /// Specifies the aspect corresponding to [MediaQueryData.boldText]. + boldText, + /// Specifies the aspect corresponding to [MediaQueryData.navigationMode]. + navigationMode, + /// Specifies the aspect corresponding to [MediaQueryData.gestureSettings]. + gestureSettings, + /// Specifies the aspect corresponding to [MediaQueryData.displayFeatures]. + displayFeatures, +} + /// Information about a piece of media (e.g., a window). /// /// For example, the [MediaQueryData.size] property contains the width and @@ -178,6 +225,8 @@ class MediaQueryData { /// See also: /// /// * [FlutterView.physicalSize], which returns the size in physical pixels. + /// * [MediaQuery.sizeOf], a method to find and depend on the size defined + /// for a [BuildContext]. final Size size; /// The number of device pixels for each logical pixel. This number might not @@ -192,7 +241,7 @@ class MediaQueryData { /// /// See also: /// - /// * [MediaQuery.textScaleFactorOf], a convenience method which returns the + /// * [MediaQuery.textScaleFactorOf], a method to find and depend on the /// textScaleFactor defined for a [BuildContext]. final double textScaleFactor; @@ -203,6 +252,11 @@ class MediaQueryData { /// /// Not all platforms necessarily support a concept of brightness mode. Those /// platforms will report [Brightness.light] in this property. + /// + /// See also: + /// + /// * [MediaQuery.platformBrightnessOf], a method to find and depend on the + /// platformBrightness defined for a [BuildContext]. final Brightness platformBrightness; /// The parts of the display that are completely obscured by system UI, @@ -690,7 +744,7 @@ class MediaQueryData { /// * [WidgetsApp] and [MaterialApp], which introduce a [MediaQuery] and keep /// it up to date with the current screen metrics as they change. /// * [MediaQueryData], the data structure that represents the metrics. -class MediaQuery extends InheritedWidget { +class MediaQuery extends InheritedModel<_MediaQueryAspect> { /// Creates a widget that provides [MediaQueryData] to its descendants. /// /// The [data] and [child] arguments must not be null. @@ -873,6 +927,11 @@ class MediaQuery extends InheritedWidget { /// examples). When that information changes, your widget will be scheduled to /// be rebuilt, keeping your widget up-to-date. /// + /// If the widget only requires a subset of properties of the [MediaQueryData] + /// object, it is preferred to use the specific methods (for example: + /// [MediaQuery.sizeOf] and [MediaQuery.paddingOf]), as those methods will not + /// cause a widget to rebuild when unrelated properties are updated. + /// /// Typical usage is as follows: /// /// ```dart @@ -889,8 +948,12 @@ class MediaQuery extends InheritedWidget { /// [MediaQuery] ancestor, it returns null instead. static MediaQueryData of(BuildContext context) { assert(context != null); + return _of(context); + } + + static MediaQueryData _of(BuildContext context, [_MediaQueryAspect? aspect]) { assert(debugCheckHasMediaQuery(context)); - return context.dependOnInheritedWidgetOfExactType()!.data; + return InheritedModel.inheritFrom(context, aspect: aspect)!.data; } /// The data from the closest instance of this class that encloses the given @@ -907,6 +970,11 @@ class MediaQuery extends InheritedWidget { /// examples). When that information changes, your widget will be scheduled to /// be rebuilt, keeping your widget up-to-date. /// + /// If the widget only requires a subset of properties of the [MediaQueryData] + /// object, it is preferred to use the specific methods (for example: + /// [MediaQuery.maybeSizeOf] and [MediaQuery.maybePaddingOf]), as those methods + /// will not cause a widget to rebuild when unrelated properties are updated. + /// /// Typical usage is as follows: /// /// ```dart @@ -922,23 +990,182 @@ class MediaQuery extends InheritedWidget { /// instead of returning null. static MediaQueryData? maybeOf(BuildContext context) { assert(context != null); - return context.dependOnInheritedWidgetOfExactType()?.data; + return _maybeOf(context); } - /// Returns textScaleFactor for the nearest MediaQuery ancestor or 1.0, if - /// no such ancestor exists. - static double textScaleFactorOf(BuildContext context) { - return MediaQuery.maybeOf(context)?.textScaleFactor ?? 1.0; + static MediaQueryData? _maybeOf(BuildContext context, [_MediaQueryAspect? aspect]) { + return InheritedModel.inheritFrom(context, aspect: aspect)?.data; } + /// Returns size for the nearest MediaQuery ancestor or + /// throws an exception, if no such ancestor exists. + /// + /// Use of this method will cause the given [context] to rebuild any time that + /// the [MediaQueryData.size] property of the ancestor [MediaQuery] changes. + static Size sizeOf(BuildContext context) => _of(context, _MediaQueryAspect.size).size; + + /// Returns size for the nearest MediaQuery ancestor or + /// null, if no such ancestor exists. + /// + /// Use of this method will cause the given [context] to rebuild any time that + /// the [MediaQueryData.size] property of the ancestor [MediaQuery] changes. + static Size? maybeSizeOf(BuildContext context) => _maybeOf(context, _MediaQueryAspect.size)?.size; + + /// Returns orientation for the nearest MediaQuery ancestor or + /// throws an exception, if no such ancestor exists. + /// + /// Use of this method will cause the given [context] to rebuild any time that + /// the [MediaQueryData.orientation] property of the ancestor [MediaQuery] changes. + static Orientation orientationOf(BuildContext context) => _of(context, _MediaQueryAspect.orientation).orientation; + + /// Returns orientation for the nearest MediaQuery ancestor or + /// null, if no such ancestor exists. + /// + /// Use of this method will cause the given [context] to rebuild any time that + /// the [MediaQueryData.orientation] property of the ancestor [MediaQuery] changes. + static Orientation? maybeOrientationOf(BuildContext context) => _maybeOf(context, _MediaQueryAspect.orientation)?.orientation; + + /// Returns devicePixelRatio for the nearest MediaQuery ancestor or + /// throws an exception, if no such ancestor exists. + /// + /// Use of this method will cause the given [context] to rebuild any time that + /// the [MediaQueryData.devicePixelRatio] property of the ancestor [MediaQuery] changes. + static double devicePixelRatioOf(BuildContext context) => _of(context, _MediaQueryAspect.devicePixelRatio).devicePixelRatio; + + /// Returns devicePixelRatio for the nearest MediaQuery ancestor or + /// null, if no such ancestor exists. + /// + /// Use of this method will cause the given [context] to rebuild any time that + /// the [MediaQueryData.devicePixelRatio] property of the ancestor [MediaQuery] changes. + static double? maybeDevicePixelRatioOf(BuildContext context) => _maybeOf(context, _MediaQueryAspect.devicePixelRatio)?.devicePixelRatio; + + /// Returns textScaleFactor for the nearest MediaQuery ancestor or + /// 1.0, if no such ancestor exists. + /// + /// Use of this method will cause the given [context] to rebuild any time that + /// the [MediaQueryData.textScaleFactor] property of the ancestor [MediaQuery] changes. + static double textScaleFactorOf(BuildContext context) => maybeTextScaleFactorOf(context) ?? 1.0; + + /// Returns textScaleFactor for the nearest MediaQuery ancestor or + /// null, if no such ancestor exists. + /// + /// Use of this method will cause the given [context] to rebuild any time that + /// the [MediaQueryData.textScaleFactor] property of the ancestor [MediaQuery] changes. + static double? maybeTextScaleFactorOf(BuildContext context) => _maybeOf(context, _MediaQueryAspect.textScaleFactor)?.textScaleFactor; + /// Returns platformBrightness for the nearest MediaQuery ancestor or /// [Brightness.light], if no such ancestor exists. /// /// Use of this method will cause the given [context] to rebuild any time that - /// any property of the ancestor [MediaQuery] changes. - static Brightness platformBrightnessOf(BuildContext context) { - return MediaQuery.maybeOf(context)?.platformBrightness ?? Brightness.light; - } + /// the [MediaQueryData.platformBrightness] property of the ancestor + /// [MediaQuery] changes. + static Brightness platformBrightnessOf(BuildContext context) => maybePlatformBrightnessOf(context) ?? Brightness.light; + + /// Returns platformBrightness for the nearest MediaQuery ancestor or + /// null, if no such ancestor exists. + /// + /// Use of this method will cause the given [context] to rebuild any time that + /// the [MediaQueryData.platformBrightness] property of the ancestor + /// [MediaQuery] changes. + static Brightness? maybePlatformBrightnessOf(BuildContext context) => _maybeOf(context, _MediaQueryAspect.platformBrightness)?.platformBrightness; + + /// Returns padding for the nearest MediaQuery ancestor or + /// throws an exception, if no such ancestor exists. + /// + /// Use of this method will cause the given [context] to rebuild any time that + /// the [MediaQueryData.padding] property of the ancestor [MediaQuery] changes. + static EdgeInsets paddingOf(BuildContext context) => _of(context, _MediaQueryAspect.padding).padding; + + /// Returns viewInsets for the nearest MediaQuery ancestor or + /// null, if no such ancestor exists. + /// + /// Use of this method will cause the given [context] to rebuild any time that + /// the [MediaQueryData.viewInsets] property of the ancestor [MediaQuery] changes. + static EdgeInsets? maybePaddingOf(BuildContext context) => _maybeOf(context, _MediaQueryAspect.padding)?.padding; + + /// Returns viewInsets for the nearest MediaQuery ancestor or + /// throws an exception, if no such ancestor exists. + /// + /// Use of this method will cause the given [context] to rebuild any time that + /// the [MediaQueryData.viewInsets] property of the ancestor [MediaQuery] changes. + static EdgeInsets viewInsetsOf(BuildContext context) => _of(context, _MediaQueryAspect.viewInsets).viewInsets; + + /// Returns viewInsets for the nearest MediaQuery ancestor or + /// null, if no such ancestor exists. + /// + /// Use of this method will cause the given [context] to rebuild any time that + /// the [MediaQueryData.viewInsets] property of the ancestor [MediaQuery] changes. + static EdgeInsets? maybeViewInsetsOf(BuildContext context) => _maybeOf(context, _MediaQueryAspect.viewInsets)?.viewInsets; + + /// Returns systemGestureInsets for the nearest MediaQuery ancestor or + /// throws an exception, if no such ancestor exists. + /// + /// Use of this method will cause the given [context] to rebuild any time that + /// the [MediaQueryData.systemGestureInsets] property of the ancestor [MediaQuery] changes. + static EdgeInsets systemGestureInsetsOf(BuildContext context) => _of(context, _MediaQueryAspect.systemGestureInsets).systemGestureInsets; + + /// Returns systemGestureInsets for the nearest MediaQuery ancestor or + /// null, if no such ancestor exists. + /// + /// Use of this method will cause the given [context] to rebuild any time that + /// the [MediaQueryData.systemGestureInsets] property of the ancestor [MediaQuery] changes. + static EdgeInsets? maybeSystemGestureInsetsOf(BuildContext context) => _maybeOf(context, _MediaQueryAspect.systemGestureInsets)?.systemGestureInsets; + + /// Returns viewPadding for the nearest MediaQuery ancestor or + /// throws an exception, if no such ancestor exists. + /// + /// Use of this method will cause the given [context] to rebuild any time that + /// the [MediaQueryData.viewPadding] property of the ancestor [MediaQuery] changes. + static EdgeInsets viewPaddingOf(BuildContext context) => _of(context, _MediaQueryAspect.viewPadding).viewPadding; + + /// Returns viewPadding for the nearest MediaQuery ancestor or + /// null, if no such ancestor exists. + /// + /// Use of this method will cause the given [context] to rebuild any time that + /// the [MediaQueryData.viewPadding] property of the ancestor [MediaQuery] changes. + static EdgeInsets? maybeViewPaddingOf(BuildContext context) => _maybeOf(context, _MediaQueryAspect.viewPadding)?.viewPadding; + + /// Returns alwaysUse for the nearest MediaQuery ancestor or + /// throws an exception, if no such ancestor exists. + /// + /// Use of this method will cause the given [context] to rebuild any time that + /// the [MediaQueryData.devicePixelRatio] property of the ancestor [MediaQuery] changes. + static bool alwaysUse24HourFormatOf(BuildContext context) => _of(context, _MediaQueryAspect.alwaysUse24HourFormat).alwaysUse24HourFormat; + + /// Returns alwaysUse24HourFormat for the nearest MediaQuery ancestor or + /// null, if no such ancestor exists. + /// + /// Use of this method will cause the given [context] to rebuild any time that + /// the [MediaQueryData.alwaysUse24HourFormat] property of the ancestor [MediaQuery] changes. + static bool? maybeAlwaysUse24HourFormatOf(BuildContext context) => _maybeOf(context, _MediaQueryAspect.alwaysUse24HourFormat)?.alwaysUse24HourFormat; + + /// Returns accessibleNavigationOf for the nearest MediaQuery ancestor or + /// throws an exception, if no such ancestor exists. + /// + /// Use of this method will cause the given [context] to rebuild any time that + /// the [MediaQueryData.accessibleNavigation] property of the ancestor [MediaQuery] changes. + static bool accessibleNavigationOf(BuildContext context) => _of(context, _MediaQueryAspect.accessibleNavigation).accessibleNavigation; + + /// Returns accessibleNavigation for the nearest MediaQuery ancestor or + /// null, if no such ancestor exists. + /// + /// Use of this method will cause the given [context] to rebuild any time that + /// the [MediaQueryData.accessibleNavigation] property of the ancestor [MediaQuery] changes. + static bool? maybeAccessibleNavigationOf(BuildContext context) => _maybeOf(context, _MediaQueryAspect.accessibleNavigation)?.accessibleNavigation; + + /// Returns invertColorsOf for the nearest MediaQuery ancestor or + /// throws an exception, if no such ancestor exists. + /// + /// Use of this method will cause the given [context] to rebuild any time that + /// the [MediaQueryData.invertColors] property of the ancestor [MediaQuery] changes. + static bool invertColorsOf(BuildContext context) => _of(context, _MediaQueryAspect.invertColors).invertColors; + + /// Returns invertColors for the nearest MediaQuery ancestor or + /// null, if no such ancestor exists. + /// + /// Use of this method will cause the given [context] to rebuild any time that + /// the [MediaQueryData.invertColors] property of the ancestor [MediaQuery] changes. + static bool? maybeInvertColorsOf(BuildContext context) => _maybeOf(context, _MediaQueryAspect.invertColors)?.invertColors; /// Returns highContrast for the nearest MediaQuery ancestor or false, if no /// such ancestor exists. @@ -947,15 +1174,102 @@ class MediaQuery extends InheritedWidget { /// /// * [MediaQueryData.highContrast], which indicates the platform's /// desire to increase contrast. - static bool highContrastOf(BuildContext context) { - return MediaQuery.maybeOf(context)?.highContrast ?? false; - } + /// + /// Use of this method will cause the given [context] to rebuild any time that + /// the [MediaQueryData.highContrast] property of the ancestor [MediaQuery] changes. + static bool highContrastOf(BuildContext context) => maybeHighContrastOf(context) ?? false; + + /// Returns highContrast for the nearest MediaQuery ancestor or + /// null, if no such ancestor exists. + /// + /// Use of this method will cause the given [context] to rebuild any time that + /// the [MediaQueryData.highContrast] property of the ancestor [MediaQuery] changes. + static bool? maybeHighContrastOf(BuildContext context) => _maybeOf(context, _MediaQueryAspect.highContrast)?.highContrast; + + /// Returns disableAnimations for the nearest MediaQuery ancestor or + /// [Brightness.light], if no such ancestor exists. + /// + /// Use of this method will cause the given [context] to rebuild any time that + /// the [MediaQueryData.disableAnimations] property of the ancestor + /// [MediaQuery] changes. + static bool disableAnimationsOf(BuildContext context) => _of(context, _MediaQueryAspect.disableAnimations).disableAnimations; + + /// Returns disableAnimations for the nearest MediaQuery ancestor or + /// null, if no such ancestor exists. + /// + /// Use of this method will cause the given [context] to rebuild any time that + /// the [MediaQueryData.disableAnimations] property of the ancestor [MediaQuery] changes. + static bool? maybeDisableAnimationsOf(BuildContext context) => _maybeOf(context, _MediaQueryAspect.disableAnimations)?.disableAnimations; + /// Returns the boldText accessibility setting for the nearest MediaQuery - /// ancestor, or false if no such ancestor exists. - static bool boldTextOverride(BuildContext context) { - return MediaQuery.maybeOf(context)?.boldText ?? false; - } + /// ancestor or false, if no such ancestor exists. + /// + /// Use of this method will cause the given [context] to rebuild any time that + /// the [MediaQueryData.boldText] property of the ancestor [MediaQuery] changes. + static bool boldTextOf(BuildContext context) => maybeBoldTextOf(context) ?? false; + + /// Returns the boldText accessibility setting for the nearest MediaQuery + /// ancestor or false, if no such ancestor exists. + /// + /// Use of this method will cause the given [context] to rebuild any time that + /// the [MediaQueryData.boldText] property of the ancestor [MediaQuery] changes. + /// + /// Deprecated in favor of [boldTextOf]. + @Deprecated( + 'Migrate to boldTextOf. ' + 'This feature was deprecated after v3.5.0-9.0.pre.' + ) + static bool boldTextOverride(BuildContext context) => boldTextOf(context); + + /// Returns the boldText accessibility setting for the nearest MediaQuery + /// ancestor or null, if no such ancestor exists. + /// + /// Use of this method will cause the given [context] to rebuild any time that + /// the [MediaQueryData.boldText] property of the ancestor [MediaQuery] changes. + static bool? maybeBoldTextOf(BuildContext context) => _maybeOf(context, _MediaQueryAspect.boldText)?.boldText; + + /// Returns navigationMode for the nearest MediaQuery ancestor or + /// throws an exception, if no such ancestor exists. + /// + /// Use of this method will cause the given [context] to rebuild any time that + /// the [MediaQueryData.navigationMode] property of the ancestor [MediaQuery] changes. + static NavigationMode navigationModeOf(BuildContext context) => _of(context, _MediaQueryAspect.navigationMode).navigationMode; + + /// Returns navigationMode for the nearest MediaQuery ancestor or + /// null, if no such ancestor exists. + /// + /// Use of this method will cause the given [context] to rebuild any time that + /// the [MediaQueryData.navigationMode] property of the ancestor [MediaQuery] changes. + static NavigationMode? maybeNavigationModeOf(BuildContext context) => _maybeOf(context, _MediaQueryAspect.navigationMode)?.navigationMode; + + /// Returns gestureSettings for the nearest MediaQuery ancestor or + /// throws an exception, if no such ancestor exists. + /// + /// Use of this method will cause the given [context] to rebuild any time that + /// the [MediaQueryData.gestureSettings] property of the ancestor [MediaQuery] changes. + static DeviceGestureSettings gestureSettingsOf(BuildContext context) => _of(context, _MediaQueryAspect.gestureSettings).gestureSettings; + + /// Returns gestureSettings for the nearest MediaQuery ancestor or + /// null, if no such ancestor exists. + /// + /// Use of this method will cause the given [context] to rebuild any time that + /// the [MediaQueryData.gestureSettings] property of the ancestor [MediaQuery] changes. + static DeviceGestureSettings? maybeGestureSettingsOf(BuildContext context) => _maybeOf(context, _MediaQueryAspect.gestureSettings)?.gestureSettings; + + /// Returns displayFeatures for the nearest MediaQuery ancestor or + /// throws an exception, if no such ancestor exists. + /// + /// Use of this method will cause the given [context] to rebuild any time that + /// the [MediaQueryData.displayFeatures] property of the ancestor [MediaQuery] changes. + static List displayFeaturesOf(BuildContext context) => _of(context, _MediaQueryAspect.displayFeatures).displayFeatures; + + /// Returns displayFeatures for the nearest MediaQuery ancestor or + /// null, if no such ancestor exists. + /// + /// Use of this method will cause the given [context] to rebuild any time that + /// the [MediaQueryData.displayFeatures] property of the ancestor [MediaQuery] changes. + static List? maybeDisplayFeaturesOf(BuildContext context) => _maybeOf(context, _MediaQueryAspect.displayFeatures)?.displayFeatures; @override bool updateShouldNotify(MediaQuery oldWidget) => data != oldWidget.data; @@ -965,6 +1279,27 @@ class MediaQuery extends InheritedWidget { super.debugFillProperties(properties); properties.add(DiagnosticsProperty('data', data, showName: false)); } + + @override + bool updateShouldNotifyDependent(MediaQuery oldWidget, Set dependencies) { + return (data.size != oldWidget.data.size && dependencies.contains(_MediaQueryAspect.size)) + || (data.orientation != oldWidget.data.orientation && dependencies.contains(_MediaQueryAspect.orientation)) + || (data.devicePixelRatio != oldWidget.data.devicePixelRatio && dependencies.contains(_MediaQueryAspect.devicePixelRatio)) + || (data.textScaleFactor != oldWidget.data.textScaleFactor && dependencies.contains(_MediaQueryAspect.textScaleFactor)) + || (data.platformBrightness != oldWidget.data.platformBrightness && dependencies.contains(_MediaQueryAspect.platformBrightness)) + || (data.viewInsets != oldWidget.data.viewInsets && dependencies.contains(_MediaQueryAspect.viewInsets)) + || (data.systemGestureInsets != oldWidget.data.systemGestureInsets && dependencies.contains(_MediaQueryAspect.systemGestureInsets)) + || (data.viewPadding != oldWidget.data.viewPadding && dependencies.contains(_MediaQueryAspect.viewPadding)) + || (data.alwaysUse24HourFormat != oldWidget.data.alwaysUse24HourFormat && dependencies.contains(_MediaQueryAspect.alwaysUse24HourFormat)) + || (data.accessibleNavigation != oldWidget.data.accessibleNavigation && dependencies.contains(_MediaQueryAspect.accessibleNavigation)) + || (data.invertColors != oldWidget.data.invertColors && dependencies.contains(_MediaQueryAspect.invertColors)) + || (data.highContrast != oldWidget.data.highContrast && dependencies.contains(_MediaQueryAspect.highContrast)) + || (data.disableAnimations != oldWidget.data.disableAnimations && dependencies.contains(_MediaQueryAspect.disableAnimations)) + || (data.boldText != oldWidget.data.boldText && dependencies.contains(_MediaQueryAspect.boldText)) + || (data.navigationMode != oldWidget.data.navigationMode && dependencies.contains(_MediaQueryAspect.navigationMode)) + || (data.gestureSettings != oldWidget.data.gestureSettings && dependencies.contains(_MediaQueryAspect.gestureSettings)) + || (data.displayFeatures != oldWidget.data.displayFeatures && dependencies.contains(_MediaQueryAspect.displayFeatures)); + } } /// Describes the navigation mode to be set by a [MediaQuery] widget. @@ -972,7 +1307,7 @@ class MediaQuery extends InheritedWidget { /// The different modes indicate the type of navigation to be used in a widget /// subtree for those widgets sensitive to it. /// -/// Use `MediaQuery.of(context).navigationMode` to determine the navigation mode +/// Use `MediaQuery.navigationModeOf(context)` to determine the navigation mode /// in effect for the given context. Use a [MediaQuery] widget to set the /// navigation mode for its descendant widgets. enum NavigationMode { diff --git a/packages/flutter/lib/src/widgets/overscroll_indicator.dart b/packages/flutter/lib/src/widgets/overscroll_indicator.dart index b8c5eba7fb49..3af6e58f9a05 100644 --- a/packages/flutter/lib/src/widgets/overscroll_indicator.dart +++ b/packages/flutter/lib/src/widgets/overscroll_indicator.dart @@ -774,7 +774,7 @@ class _StretchingOverscrollIndicatorState extends State( onNotification: _handleScrollNotification, diff --git a/packages/flutter/lib/src/widgets/reorderable_list.dart b/packages/flutter/lib/src/widgets/reorderable_list.dart index 6521aed13e68..388e340d92af 100644 --- a/packages/flutter/lib/src/widgets/reorderable_list.dart +++ b/packages/flutter/lib/src/widgets/reorderable_list.dart @@ -1163,7 +1163,7 @@ class ReorderableDragStartListener extends StatelessWidget { } void _startDragging(BuildContext context, PointerDownEvent event) { - final DeviceGestureSettings? gestureSettings = MediaQuery.maybeOf(context)?.gestureSettings; + final DeviceGestureSettings? gestureSettings = MediaQuery.maybeGestureSettingsOf(context); final SliverReorderableListState? list = SliverReorderableList.maybeOf(context); list?.startItemDragReorder( index: index, diff --git a/packages/flutter/lib/src/widgets/safe_area.dart b/packages/flutter/lib/src/widgets/safe_area.dart index f7cd26bde82d..fbdca2467354 100644 --- a/packages/flutter/lib/src/widgets/safe_area.dart +++ b/packages/flutter/lib/src/widgets/safe_area.dart @@ -93,11 +93,10 @@ class SafeArea extends StatelessWidget { @override Widget build(BuildContext context) { assert(debugCheckHasMediaQuery(context)); - final MediaQueryData data = MediaQuery.of(context); - EdgeInsets padding = data.padding; + EdgeInsets padding = MediaQuery.paddingOf(context); // Bottom padding has been consumed - i.e. by the keyboard if (maintainBottomViewPadding) { - padding = padding.copyWith(bottom: data.viewPadding.bottom); + padding = padding.copyWith(bottom: MediaQuery.viewPaddingOf(context).bottom); } return Padding( @@ -192,7 +191,7 @@ class SliverSafeArea extends StatelessWidget { @override Widget build(BuildContext context) { assert(debugCheckHasMediaQuery(context)); - final EdgeInsets padding = MediaQuery.of(context).padding; + final EdgeInsets padding = MediaQuery.paddingOf(context); return SliverPadding( padding: EdgeInsets.only( left: math.max(left ? padding.left : 0.0, minimum.left), diff --git a/packages/flutter/lib/src/widgets/scrollable.dart b/packages/flutter/lib/src/widgets/scrollable.dart index 7f07f2494ab0..0ddcbc6143a6 100644 --- a/packages/flutter/lib/src/widgets/scrollable.dart +++ b/packages/flutter/lib/src/widgets/scrollable.dart @@ -461,7 +461,7 @@ class ScrollableState extends State with TickerProviderStateMixin, R late ScrollBehavior _configuration; ScrollPhysics? _physics; ScrollController? _fallbackScrollController; - MediaQueryData? _mediaQueryData; + DeviceGestureSettings? _mediaQueryGestureSettings; ScrollController get _effectiveScrollController => widget.controller ?? _fallbackScrollController!; @@ -516,7 +516,7 @@ class ScrollableState extends State with TickerProviderStateMixin, R @override void didChangeDependencies() { - _mediaQueryData = MediaQuery.maybeOf(context); + _mediaQueryGestureSettings = MediaQuery.maybeGestureSettingsOf(context); _updatePosition(); super.didChangeDependencies(); } @@ -635,7 +635,7 @@ class ScrollableState extends State with TickerProviderStateMixin, R ..maxFlingVelocity = _physics?.maxFlingVelocity ..velocityTrackerBuilder = _configuration.velocityTrackerBuilder(context) ..dragStartBehavior = widget.dragStartBehavior - ..gestureSettings = _mediaQueryData?.gestureSettings; + ..gestureSettings = _mediaQueryGestureSettings; }, ), }; @@ -656,7 +656,7 @@ class ScrollableState extends State with TickerProviderStateMixin, R ..maxFlingVelocity = _physics?.maxFlingVelocity ..velocityTrackerBuilder = _configuration.velocityTrackerBuilder(context) ..dragStartBehavior = widget.dragStartBehavior - ..gestureSettings = _mediaQueryData?.gestureSettings; + ..gestureSettings = _mediaQueryGestureSettings; }, ), }; diff --git a/packages/flutter/lib/src/widgets/scrollbar.dart b/packages/flutter/lib/src/widgets/scrollbar.dart index 4a2aea20a607..467e82a3a50d 100644 --- a/packages/flutter/lib/src/widgets/scrollbar.dart +++ b/packages/flutter/lib/src/widgets/scrollbar.dart @@ -1675,7 +1675,7 @@ class RawScrollbarState extends State with TickerProv ..textDirection = Directionality.of(context) ..thickness = widget.thickness ?? _kScrollbarThickness ..radius = widget.radius - ..padding = widget.padding ?? MediaQuery.of(context).padding + ..padding = widget.padding ?? MediaQuery.paddingOf(context) ..scrollbarOrientation = widget.scrollbarOrientation ..mainAxisMargin = widget.mainAxisMargin ..shape = widget.shape diff --git a/packages/flutter/lib/src/widgets/selectable_region.dart b/packages/flutter/lib/src/widgets/selectable_region.dart index 0422a86cd977..a12be125aaf9 100644 --- a/packages/flutter/lib/src/widgets/selectable_region.dart +++ b/packages/flutter/lib/src/widgets/selectable_region.dart @@ -350,7 +350,7 @@ class SelectableRegionState extends State with TextSelectionDe } // Hide the text selection toolbar on mobile when orientation changes. - final Orientation orientation = MediaQuery.of(context).orientation; + final Orientation orientation = MediaQuery.orientationOf(context); if (_lastOrientation == null) { _lastOrientation = orientation; return; diff --git a/packages/flutter/lib/src/widgets/snapshot_widget.dart b/packages/flutter/lib/src/widgets/snapshot_widget.dart index a2d642201396..854512f1f452 100644 --- a/packages/flutter/lib/src/widgets/snapshot_widget.dart +++ b/packages/flutter/lib/src/widgets/snapshot_widget.dart @@ -141,7 +141,7 @@ class SnapshotWidget extends SingleChildRenderObjectWidget { return _RenderSnapshotWidget( controller: controller, mode: mode, - devicePixelRatio: MediaQuery.of(context).devicePixelRatio, + devicePixelRatio: MediaQuery.devicePixelRatioOf(context), painter: painter, autoresize: autoresize, ); @@ -153,7 +153,7 @@ class SnapshotWidget extends SingleChildRenderObjectWidget { (renderObject as _RenderSnapshotWidget) ..controller = controller ..mode = mode - ..devicePixelRatio = MediaQuery.of(context).devicePixelRatio + ..devicePixelRatio = MediaQuery.devicePixelRatioOf(context) ..painter = painter ..autoresize = autoresize; } diff --git a/packages/flutter/lib/src/widgets/text.dart b/packages/flutter/lib/src/widgets/text.dart index 622f1a50e11c..2ee0e0c5badf 100644 --- a/packages/flutter/lib/src/widgets/text.dart +++ b/packages/flutter/lib/src/widgets/text.dart @@ -598,7 +598,7 @@ class Text extends StatelessWidget { if (style == null || style!.inherit) { effectiveTextStyle = defaultTextStyle.style.merge(style); } - if (MediaQuery.boldTextOverride(context)) { + if (MediaQuery.boldTextOf(context)) { effectiveTextStyle = effectiveTextStyle!.merge(const TextStyle(fontWeight: FontWeight.bold)); } final SelectionRegistrar? registrar = SelectionContainer.maybeOf(context); diff --git a/packages/flutter/test/cupertino/action_sheet_test.dart b/packages/flutter/test/cupertino/action_sheet_test.dart index 1b849c990392..80b1fc413e57 100644 --- a/packages/flutter/test/cupertino/action_sheet_test.dart +++ b/packages/flutter/test/cupertino/action_sheet_test.dart @@ -362,7 +362,7 @@ void main() { await tester.pumpWidget( createAppWithButtonThatLaunchesActionSheet( Builder(builder: (BuildContext context) { - screenHeight = MediaQuery.of(context).size.height; + screenHeight = MediaQuery.sizeOf(context).height; return MediaQuery( data: MediaQuery.of(context).copyWith(textScaleFactor: 3.0), child: CupertinoActionSheet( diff --git a/packages/flutter/test/cupertino/dialog_test.dart b/packages/flutter/test/cupertino/dialog_test.dart index 791cd20e1b6d..d1e1b2b92993 100644 --- a/packages/flutter/test/cupertino/dialog_test.dart +++ b/packages/flutter/test/cupertino/dialog_test.dart @@ -588,7 +588,7 @@ void main() { await tester.pumpWidget( createAppWithButtonThatLaunchesDialog( dialogBuilder: (BuildContext context) { - dividerWidth = 1.0 / MediaQuery.of(context).devicePixelRatio; + dividerWidth = 1.0 / MediaQuery.devicePixelRatioOf(context); return CupertinoAlertDialog( title: const Text('The Title'), content: const Text('The message'), @@ -633,7 +633,7 @@ void main() { await tester.pumpWidget( createAppWithButtonThatLaunchesDialog( dialogBuilder: (BuildContext context) { - dividerThickness = 1.0 / MediaQuery.of(context).devicePixelRatio; + dividerThickness = 1.0 / MediaQuery.devicePixelRatioOf(context); return CupertinoAlertDialog( title: const Text('The Title'), content: const Text('The message'), @@ -841,7 +841,7 @@ void main() { await tester.pumpWidget( createAppWithButtonThatLaunchesDialog( dialogBuilder: (BuildContext context) { - dividerThickness = 1.0 / MediaQuery.of(context).devicePixelRatio; + dividerThickness = 1.0 / MediaQuery.devicePixelRatioOf(context); return CupertinoAlertDialog( title: const Text('The Title'), content: const Text('The message'), diff --git a/packages/flutter/test/cupertino/magnifier_test.dart b/packages/flutter/test/cupertino/magnifier_test.dart index 845de0ff6e9f..0a7e5e31eb21 100644 --- a/packages/flutter/test/cupertino/magnifier_test.dart +++ b/packages/flutter/test/cupertino/magnifier_test.dart @@ -117,14 +117,14 @@ void main() { caretRect: reasonableTextField, // The tap position is far out of the right side of the app. globalGesturePosition: - Offset(MediaQuery.of(context).size.width + 100, 0), + Offset(MediaQuery.sizeOf(context).width + 100, 0), ), ), ); // Should be less than the right edge, since we have padding. expect(getMagnifierPosition(tester).dx, - lessThan(MediaQuery.of(context).size.width)); + lessThan(MediaQuery.sizeOf(context).width)); }); testWidgets('should have some vertical drag', (WidgetTester tester) async { @@ -150,7 +150,7 @@ void main() { caretRect: reasonableTextField, // The tap position is dragBelow units below the text field. globalGesturePosition: Offset( - MediaQuery.of(context).size.width / 2, + MediaQuery.sizeOf(context).width / 2, dragPositionBelowTextField), ), ), @@ -184,7 +184,7 @@ void main() { caretRect: reasonableTextField, // The tap position is dragBelow units below the text field. globalGesturePosition: Offset( - MediaQuery.of(context).size.width / 2, reasonableTextField.top), + MediaQuery.sizeOf(context).width / 2, reasonableTextField.top), ), ); @@ -223,7 +223,7 @@ void main() { fieldBounds: reasonableTextField, caretRect: reasonableTextField, // The tap position is dragBelow units below the text field. - globalGesturePosition: Offset(MediaQuery.of(context).size.width / 2, reasonableTextField.top), + globalGesturePosition: Offset(MediaQuery.sizeOf(context).width / 2, reasonableTextField.top), ), ); @@ -247,7 +247,7 @@ void main() { currentLineBoundaries: reasonableTextField, fieldBounds: reasonableTextField, caretRect: reasonableTextField, - globalGesturePosition: Offset(MediaQuery.of(context).size.width / 2, + globalGesturePosition: Offset(MediaQuery.sizeOf(context).width / 2, reasonableTextField.top)); await tester.pumpAndSettle(); diff --git a/packages/flutter/test/cupertino/tab_scaffold_test.dart b/packages/flutter/test/cupertino/tab_scaffold_test.dart index 6d17327fded4..9e76489704ff 100644 --- a/packages/flutter/test/cupertino/tab_scaffold_test.dart +++ b/packages/flutter/test/cupertino/tab_scaffold_test.dart @@ -501,7 +501,7 @@ void main() { items: List.generate(2, tabGenerator), ), tabBuilder: (BuildContext context, int index) { - contentPadding = MediaQuery.of(context).padding; + contentPadding = MediaQuery.paddingOf(context); return const Placeholder(); }, ), diff --git a/packages/flutter/test/cupertino/text_selection_toolbar_test.dart b/packages/flutter/test/cupertino/text_selection_toolbar_test.dart index 5052d1ee9128..5f236968ea00 100644 --- a/packages/flutter/test/cupertino/text_selection_toolbar_test.dart +++ b/packages/flutter/test/cupertino/text_selection_toolbar_test.dart @@ -26,10 +26,10 @@ class _CustomCupertinoTextSelectionControls extends CupertinoTextSelectionContro ValueNotifier? clipboardStatus, Offset? lastSecondaryTapDownPosition, ) { - final MediaQueryData mediaQuery = MediaQuery.of(context); + final EdgeInsets mediaQueryPadding = MediaQuery.paddingOf(context); final double anchorX = (selectionMidpoint.dx + globalEditableRegion.left).clamp( - _kArrowScreenPadding + mediaQuery.padding.left, - mediaQuery.size.width - mediaQuery.padding.right - _kArrowScreenPadding, + _kArrowScreenPadding + mediaQueryPadding.left, + MediaQuery.sizeOf(context).width - mediaQueryPadding.right - _kArrowScreenPadding, ); final Offset anchorAbove = Offset( anchorX, diff --git a/packages/flutter/test/material/app_test.dart b/packages/flutter/test/material/app_test.dart index add149302a3c..9d6c9c735229 100644 --- a/packages/flutter/test/material/app_test.dart +++ b/packages/flutter/test/material/app_test.dart @@ -475,7 +475,7 @@ void main() { double? textScaleFactor; await tester.pumpWidget(MaterialApp( home: Builder(builder:(BuildContext context) { - textScaleFactor = MediaQuery.of(context).textScaleFactor; + textScaleFactor = MediaQuery.textScaleFactorOf(context); return Container(); }), )); diff --git a/packages/flutter/test/material/flexible_space_bar_test.dart b/packages/flutter/test/material/flexible_space_bar_test.dart index c55e4252cc71..29629167ccf4 100644 --- a/packages/flutter/test/material/flexible_space_bar_test.dart +++ b/packages/flutter/test/material/flexible_space_bar_test.dart @@ -438,7 +438,7 @@ void main() { home: Scaffold( body: Builder( builder: (BuildContext context) { - width = MediaQuery.of(context).size.width; + width = MediaQuery.sizeOf(context).width; return CustomScrollView( slivers: [ SliverAppBar( diff --git a/packages/flutter/test/material/popup_menu_test.dart b/packages/flutter/test/material/popup_menu_test.dart index 7b28d8836183..92bcba1cf331 100644 --- a/packages/flutter/test/material/popup_menu_test.dart +++ b/packages/flutter/test/material/popup_menu_test.dart @@ -2443,7 +2443,7 @@ void main() { value: 1, child: Builder( builder: (BuildContext context) { - mediaQueryPadding = MediaQuery.of(context).padding; + mediaQueryPadding = MediaQuery.paddingOf(context); return Text('-1-' * 500); // A long long text string. }, ), diff --git a/packages/flutter/test/material/scaffold_test.dart b/packages/flutter/test/material/scaffold_test.dart index d63ed5476f3e..c0a7187c277a 100644 --- a/packages/flutter/test/material/scaffold_test.dart +++ b/packages/flutter/test/material/scaffold_test.dart @@ -925,7 +925,7 @@ void main() { extendBody: extendBody, body: Builder( builder: (BuildContext context) { - mediaQueryBottom = MediaQuery.of(context).padding.bottom; + mediaQueryBottom = MediaQuery.paddingOf(context).bottom; return Container(key: bodyKey); }, ), @@ -998,7 +998,7 @@ void main() { ), body: Builder( builder: (BuildContext context) { - mediaQueryTop = MediaQuery.of(context).padding.top; + mediaQueryTop = MediaQuery.paddingOf(context).top; return Container(key: bodyKey); }, ), @@ -1957,7 +1957,7 @@ void main() { MaterialApp( home: Builder( builder: (BuildContext context) { - screenWidth = MediaQuery.of(context).size.width; + screenWidth = MediaQuery.sizeOf(context).width; return Scaffold( endDrawer: const Drawer( child: Text('Drawer'), diff --git a/packages/flutter/test/material/snack_bar_test.dart b/packages/flutter/test/material/snack_bar_test.dart index 4d5a847b878d..7b8009f6759c 100644 --- a/packages/flutter/test/material/snack_bar_test.dart +++ b/packages/flutter/test/material/snack_bar_test.dart @@ -229,7 +229,7 @@ void main() { home: Scaffold( body: Builder( builder: (BuildContext context) { - width = MediaQuery.of(context).size.width; + width = MediaQuery.sizeOf(context).width; return GestureDetector( key: tapTarget, diff --git a/packages/flutter/test/widgets/list_view_test.dart b/packages/flutter/test/widgets/list_view_test.dart index af9486a80358..66dcaf47d554 100644 --- a/packages/flutter/test/widgets/list_view_test.dart +++ b/packages/flutter/test/widgets/list_view_test.dart @@ -502,7 +502,7 @@ void main() { children: [ const Text('top', textDirection: TextDirection.ltr), Builder(builder: (BuildContext context) { - innerMediaQueryPadding = MediaQuery.of(context).padding; + innerMediaQueryPadding = MediaQuery.paddingOf(context); return Container(); }), ], diff --git a/packages/flutter/test/widgets/media_query_test.dart b/packages/flutter/test/widgets/media_query_test.dart index 63069bf5c4a1..d5d66b38fc33 100644 --- a/packages/flutter/test/widgets/media_query_test.dart +++ b/packages/flutter/test/widgets/media_query_test.dart @@ -5,7 +5,7 @@ import 'dart:ui' show Brightness, DisplayFeature, DisplayFeatureState, DisplayFeatureType, GestureSettings, ViewConfiguration; import 'package:flutter/gestures.dart'; -import 'package:flutter/widgets.dart'; +import 'package:flutter/material.dart'; import 'package:flutter_test/flutter_test.dart'; void main() { @@ -686,21 +686,21 @@ void main() { expect(insideHighContrast, true); }); - testWidgets('MediaQuery.boldTextOverride', (WidgetTester tester) async { + testWidgets('MediaQuery.boldTextOf', (WidgetTester tester) async { late bool outsideBoldTextOverride; late bool insideBoldTextOverride; await tester.pumpWidget( Builder( builder: (BuildContext context) { - outsideBoldTextOverride = MediaQuery.boldTextOverride(context); + outsideBoldTextOverride = MediaQuery.boldTextOf(context); return MediaQuery( data: const MediaQueryData( boldText: true, ), child: Builder( builder: (BuildContext context) { - insideBoldTextOverride = MediaQuery.boldTextOverride(context); + insideBoldTextOverride = MediaQuery.boldTextOf(context); return Container(); }, ), @@ -937,4 +937,80 @@ void main() { expect(MediaQueryData.fromWindow(tester.binding.window).gestureSettings.touchSlop, closeTo(33.33, 0.1)); // Repeating, of course tester.binding.window.viewConfigurationTestValue = null; }); + + testWidgets('MediaQuery can be partially depended-on', (WidgetTester tester) async { + MediaQueryData data = const MediaQueryData( + size: Size(800, 600), + textScaleFactor: 1.1 + ); + + int sizeBuildCount = 0; + int textScaleFactorBuildCount = 0; + + final Widget showSize = Builder( + builder: (BuildContext context) { + sizeBuildCount++; + return Text('size: ${MediaQuery.sizeOf(context)}'); + } + ); + + final Widget showTextScaleFactor = Builder( + builder: (BuildContext context) { + textScaleFactorBuildCount++; + return Text('textScaleFactor: ${MediaQuery.textScaleFactorOf(context).toStringAsFixed(1)}'); + } + ); + + final Widget page = StatefulBuilder( + builder: (BuildContext context, StateSetter setState) { + return MediaQuery( + data: data, + child: Center( + child: Column( + children: [ + showSize, + showTextScaleFactor, + ElevatedButton( + onPressed: () { + setState(() { + data = data.copyWith(size: Size(data.size.width + 100, data.size.height)); + }); + }, + child: const Text('Increase width by 100') + ), + ElevatedButton( + onPressed: () { + setState(() { + data = data.copyWith(textScaleFactor: data.textScaleFactor + 0.1); + }); + }, + child: const Text('Increase textScaleFactor by 0.1') + ) + ] + ) + ) + ); + }, + ); + + await tester.pumpWidget(MaterialApp(home: page)); + expect(find.text('size: Size(800.0, 600.0)'), findsOneWidget); + expect(find.text('textScaleFactor: 1.1'), findsOneWidget); + expect(sizeBuildCount, 1); + expect(textScaleFactorBuildCount, 1); + + await tester.tap(find.text('Increase width by 100')); + await tester.pumpAndSettle(); + expect(find.text('size: Size(900.0, 600.0)'), findsOneWidget); + expect(find.text('textScaleFactor: 1.1'), findsOneWidget); + expect(sizeBuildCount, 2); + expect(textScaleFactorBuildCount, 1); + + await tester.tap(find.text('Increase textScaleFactor by 0.1')); + await tester.pumpAndSettle(); + expect(find.text('size: Size(900.0, 600.0)'), findsOneWidget); + expect(find.text('textScaleFactor: 1.2'), findsOneWidget); + expect(sizeBuildCount, 2); + expect(textScaleFactorBuildCount, 2); + }); } diff --git a/packages/flutter/test/widgets/render_object_widget_test.dart b/packages/flutter/test/widgets/render_object_widget_test.dart index c4e107232042..39241cb18666 100644 --- a/packages/flutter/test/widgets/render_object_widget_test.dart +++ b/packages/flutter/test/widgets/render_object_widget_test.dart @@ -28,7 +28,7 @@ class TestOrientedBox extends SingleChildRenderObjectWidget { const TestOrientedBox({ super.key, super.child }); Decoration _getDecoration(BuildContext context) { - final Orientation orientation = MediaQuery.of(context).orientation; + final Orientation orientation = MediaQuery.orientationOf(context); switch (orientation) { case Orientation.landscape: return const BoxDecoration(color: Color(0xFF00FF00)); diff --git a/packages/flutter/test_fixes/cupertino/cupertino.dart b/packages/flutter/test_fixes/cupertino/cupertino.dart index 4ea5c635a44c..9ee91e3ab067 100644 --- a/packages/flutter/test_fixes/cupertino/cupertino.dart +++ b/packages/flutter/test_fixes/cupertino/cupertino.dart @@ -235,4 +235,7 @@ void main() { scrollBehavior.buildViewportChrome(context, child, axisDirection); final CupertinoScrollBehavior cupertinoScrollBehavior = CupertinoScrollBehavior(); cupertinoScrollBehavior.buildViewportChrome(context, child, axisDirection); + + // Changes made in https://github.com/flutter/flutter/pull/114459 + MediaQuery.boldTextOverride(context); } diff --git a/packages/flutter/test_fixes/cupertino/cupertino.dart.expect b/packages/flutter/test_fixes/cupertino/cupertino.dart.expect index 28ed8b9042d3..c9ef40bc623c 100644 --- a/packages/flutter/test_fixes/cupertino/cupertino.dart.expect +++ b/packages/flutter/test_fixes/cupertino/cupertino.dart.expect @@ -235,4 +235,7 @@ void main() { scrollBehavior.buildOverscrollIndicator(context, child, axisDirection); final CupertinoScrollBehavior cupertinoScrollBehavior = CupertinoScrollBehavior(); cupertinoScrollBehavior.buildOverscrollIndicator(context, child, axisDirection); + + // Changes made in https://github.com/flutter/flutter/pull/114459 + MediaQuery.boldTextOf(context); } diff --git a/packages/flutter/test_fixes/material/material.dart b/packages/flutter/test_fixes/material/material.dart index dffef1322f3a..c5c7a9af5421 100644 --- a/packages/flutter/test_fixes/material/material.dart +++ b/packages/flutter/test_fixes/material/material.dart @@ -307,4 +307,7 @@ void main() { ScrollbarThemeData scrollbarTheme = ScrollbarThemeData(showTrackOnHover: nowShowing); scrollbarTheme.copyWith(showTrackOnHover: nowShowing); scrollbarTheme.showTrackOnHover; + + // Changes made in https://github.com/flutter/flutter/pull/114459 + MediaQuery.boldTextOverride(context); } diff --git a/packages/flutter/test_fixes/material/material.dart.expect b/packages/flutter/test_fixes/material/material.dart.expect index 290e79075d47..50f044ab94e3 100644 --- a/packages/flutter/test_fixes/material/material.dart.expect +++ b/packages/flutter/test_fixes/material/material.dart.expect @@ -303,4 +303,7 @@ void main() { ScrollbarThemeData scrollbarTheme = ScrollbarThemeData(trackVisibility: nowShowing); scrollbarTheme.copyWith(trackVisibility: nowShowing); scrollbarTheme.trackVisibility; + + // Changes made in https://github.com/flutter/flutter/pull/114459 + MediaQuery.boldTextOf(context); } diff --git a/packages/flutter/test_fixes/widgets/widgets.dart b/packages/flutter/test_fixes/widgets/widgets.dart index b3f777b086e1..88481964fbd7 100644 --- a/packages/flutter/test_fixes/widgets/widgets.dart +++ b/packages/flutter/test_fixes/widgets/widgets.dart @@ -150,4 +150,7 @@ void main() { // Changes made in https://github.com/flutter/flutter/pull/78588 final ScrollBehavior scrollBehavior = ScrollBehavior(); scrollBehavior.buildViewportChrome(context, child, axisDirection); + + // Changes made in https://github.com/flutter/flutter/pull/114459 + MediaQuery.boldTextOverride(context); } diff --git a/packages/flutter/test_fixes/widgets/widgets.dart.expect b/packages/flutter/test_fixes/widgets/widgets.dart.expect index 79255c646515..82ad2e146656 100644 --- a/packages/flutter/test_fixes/widgets/widgets.dart.expect +++ b/packages/flutter/test_fixes/widgets/widgets.dart.expect @@ -150,4 +150,7 @@ void main() { // Changes made in https://github.com/flutter/flutter/pull/78588 final ScrollBehavior scrollBehavior = ScrollBehavior(); scrollBehavior.buildOverscrollIndicator(context, child, axisDirection); + + // Changes made in https://github.com/flutter/flutter/pull/114459 + MediaQuery.boldTextOf(context); }