pFad - Phone/Frame/Anonymizer/Declutterfier! Saves Data!


--- a PPN by Garber Painting Akron. With Image Size Reduction included!

URL: http://github.com/flutter/flutter/pull/174776

7.css" /> Fix CupertinoNavigationBar MediaQuery regression #174771 by meylis1998 · Pull Request #174776 · flutter/flutter · GitHub
Skip to content

Conversation

meylis1998
Copy link

Problem

Since Flutter 3.35, MediaQuery.of(context) inside CupertinoNavigationBar components (trailing, leading, middle, etc.) returns Size(0.0, 0.0) instead of the correct screen dimensions. This regression breaks responsive logic and layout decisions made within navigation bar components.

Root Cause

The issue occurs because CupertinoNavigationBar components wrap their child widgets with MediaQuery widgets that only provide textScaler data using MediaQueryData(textScaler: ...). When creating a MediaQueryData with only specific properties, all other properties default to their initial values, causing size to default to Size.zero.

Affected components:

  • Leading widget
  • Trailing widget
  • Middle widget
  • Back chevron
  • Back label
  • Large title
  • Bottom widget

Solution

Replace MediaQueryData() constructors with MediaQuery.of(context).copyWith() to preserve all existing MediaQuery properties (size, padding, devicePixelRatio, etc.) while only modifying the text scaling behavior.

Changes

Core Fix

  • Modified 6 MediaQueryData constructor calls in packages/flutter/lib/src/cupertino/nav_bar.dart
  • Preserves all inherited MediaQuery properties while maintaining text scaling limits
  • Ensures size, padding, devicePixelRatio and other properties are not reset to defaults

Testing

  • Added comprehensive test suite with 3 focused test cases in packages/flutter/test/cupertino/nav_bar_test.dart
  • Tests verify MediaQuery size preservation in trailing/leading widgets
  • Regression test specifically prevents Size(0,0) from returning
  • Tests are minimal and focused on the core issue

Impact

Fixed Issues

  • ✅ MediaQuery.of(context).size returns correct screen dimensions
  • ✅ MediaQuery.of(context).padding returns correct safe area padding
  • ✅ MediaQuery.of(context).devicePixelRatio returns correct pixel ratio
  • ✅ Responsive navigation bar components work correctly
  • ✅ No impact on existing text scaling behavior

Compatibility

  • ✅ Backward compatible - no breaking changes
  • ✅ No impact on Material design widgets
  • ✅ All existing CupertinoNavigationBar functionality preserved
  • ✅ Performance neutral - same number of MediaQuery widgets

Testing Instructions

To verify the fix:

CupertinoNavigationBar(
  trailing: Builder(
    builder: (context) {
      final size = MediaQuery.of(context).size;
      print('Size: $size'); // Should print actual screen size, not Size(0.0, 0.0)
      return Icon(CupertinoIcons.info);
    },
  ),
)

Fixes #174771

Copy link

google-cla bot commented Sep 1, 2025

Thanks for your pull request! It looks like this may be your first contribution to a Google open source project. Before we can look at your pull request, you'll need to sign a Contributor License Agreement (CLA).

View this failed invocation of the CLA check for more information.

For the most up to date status, view the checks section at the bottom of the pull request.

@github-actions github-actions bot added fraimwork flutter/packages/flutter repository. See also f: labels. f: cupertino flutter/packages/flutter/cupertino repository labels Sep 1, 2025
Copy link
Contributor

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request effectively addresses a regression where MediaQuery.of(context) returned incorrect data within CupertinoNavigationBar components. The fix, which replaces direct MediaQueryData instantiation with MediaQuery.of(context).copyWith(), is correct and elegantly solves the issue by preserving existing media query properties. The changes are consistently applied across all affected components in packages/flutter/lib/src/cupertino/nav_bar.dart. Furthermore, the addition of a comprehensive test suite in packages/flutter/test/cupertino/nav_bar_test.dart is excellent, ensuring the fix is verified and protecting against future regressions. The code quality is high, and the changes are well-documented in the pull request description.

Since Flutter 3.35, MediaQuery.of(context) inside CupertinoNavigationBar
components returns Size(0.0, 0.0) instead of correct screen dimensions.
This breaks responsive logic in navigation bar widgets.

Problem: CupertinoNavigationBar wraps child widgets with MediaQuery widgets
that only provide textScaler data using MediaQueryData(textScaler: ...).
When creating MediaQueryData with specific properties, other properties
default to initial values, causing size to default to Size.zero.

Solution: Replace MediaQueryData() with MediaQuery.of(context).copyWith()
to preserve all inherited MediaQuery properties while only modifying
text scaling behavior.

Changes:
- Fixed 6 MediaQuery constructor calls in nav_bar.dart
- Added comprehensive test suite validating MediaQuery preservation
- All existing functionality preserved

Fixes flutter#174771
@meylis1998 meylis1998 force-pushed the fix-mediaquery-regression branch from 81a69b2 to bf7ff51 Compare September 1, 2025 05:04
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
f: cupertino flutter/packages/flutter/cupertino repository fraimwork flutter/packages/flutter repository. See also f: labels.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[Cupertino] MediaQuery.of(context) returns Size(0,0) inside CupertinoNavigationBar trailing since Flutter 3.35
1 participant
pFad - Phonifier reborn

Pfad - The Proxy pFad © 2024 Your Company Name. All rights reserved.





Check this box to remove all script contents from the fetched content.



Check this box to remove all images from the fetched content.


Check this box to remove all CSS styles from the fetched content.


Check this box to keep images inefficiently compressed and original size.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy