Skip to content

Commit 16b4ae4

Browse files
authored
Replace usages of MediaQuery.of(context).property with MediaQuery.propertyOf(context) (#184211)
This change replaces the following usages: - `MediaQuery.of(context).size.width` with `MediaQuery.widthOf(context)` - `MediaQuery.of(context).size.height` with `MediaQuery.heightOf(context)` ## Pre-launch Checklist - [x] I read the [Contributor Guide] and followed the process outlined there for submitting PRs. - [x] I read the [AI contribution guidelines] and understand my responsibilities, or I am not using AI tools. - [x] I read the [Tree Hygiene] wiki page, which explains my responsibilities. - [x] I read and followed the [Flutter Style Guide], including [Features we expect every widget to implement]. - [ ] I signed the [CLA]. - [ ] I listed at least one issue that this PR fixes in the description above. - [ ] I updated/added relevant documentation (doc comments with `///`). - [x] I added new tests to check the change I am making, or this PR is [test-exempt]. - [x] I followed the [breaking change policy] and added [Data Driven Fixes] where supported. - [x] All existing and new tests are passing. If you need help, consider asking for advice on the #hackers-new channel on [Discord]. **Note**: The Flutter team is currently trialing the use of [Gemini Code Assist for GitHub](https://developers.google.com/gemini-code-assist/docs/review-github-code). Comments from the `gemini-code-assist` bot should not be taken as authoritative feedback from the Flutter team. If you find its comments useful you can update your code accordingly, but if you are unsure or disagree with the feedback, please feel free to wait for a Flutter team member's review for guidance on which automated comments should be addressed. <!-- Links --> [Contributor Guide]: https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md#overview [AI contribution guidelines]: https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md#ai-contribution-guidelines [Tree Hygiene]: https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md [test-exempt]: https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md#tests [Flutter Style Guide]: https://github.com/flutter/flutter/blob/main/docs/contributing/Style-guide-for-Flutter-repo.md [Features we expect every widget to implement]: https://github.com/flutter/flutter/blob/main/docs/contributing/Style-guide-for-Flutter-repo.md#features-we-expect-every-widget-to-implement [CLA]: https://cla.developers.google.com/ [flutter/tests]: https://github.com/flutter/tests [breaking change policy]: https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md#handling-breaking-changes [Discord]: https://github.com/flutter/flutter/blob/main/docs/contributing/Chat.md [Data Driven Fixes]: https://github.com/flutter/flutter/blob/main/docs/contributing/Data-driven-Fixes.md
1 parent 9667987 commit 16b4ae4

23 files changed

Lines changed: 38 additions & 39 deletions

File tree

dev/benchmarks/macrobenchmarks/lib/src/large_image_changer.dart

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,8 @@ class _LargeImageChangerState extends State<LargeImageChangerPage> {
2424
super.didChangeDependencies();
2525
currentImage = ResizeImage(
2626
const ExactAssetImage('assets/999x1000.png'),
27-
width: (MediaQuery.of(context).size.width * 2).toInt() + imageIndex,
28-
height: (MediaQuery.of(context).size.height * 2).toInt() + imageIndex,
27+
width: (MediaQuery.widthOf(context) * 2).toInt() + imageIndex,
28+
height: (MediaQuery.heightOf(context) * 2).toInt() + imageIndex,
2929
allowUpscaling: true,
3030
);
3131
_timer?.cancel();
@@ -35,8 +35,8 @@ class _LargeImageChangerState extends State<LargeImageChangerPage> {
3535
imageIndex = (imageIndex + 1) % 6;
3636
currentImage = ResizeImage(
3737
const ExactAssetImage('assets/999x1000.png'),
38-
width: (MediaQuery.of(context).size.width * 2).toInt() + imageIndex,
39-
height: (MediaQuery.of(context).size.height * 2).toInt() + imageIndex,
38+
width: (MediaQuery.widthOf(context) * 2).toInt() + imageIndex,
39+
height: (MediaQuery.heightOf(context) * 2).toInt() + imageIndex,
4040
allowUpscaling: true,
4141
);
4242
});

dev/benchmarks/macrobenchmarks/lib/src/multi_widget_construction.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ class _MultiWidgetConstructTableState extends State<MultiWidgetConstructTable>
6060
builder: (BuildContext context, _) {
6161
final int totalLength = widget.rowCount * widget.columnCount;
6262
final int widgetCounter = counter * totalLength;
63-
final double height = MediaQuery.of(context).size.height / widget.rowCount;
63+
final double height = MediaQuery.heightOf(context) / widget.rowCount;
6464
final double colorPosition = _controller.value;
6565
final int c1Position = colorPosition.floor();
6666
final Color c1 = colorList[c1Position % colorList.length][900]!;

dev/benchmarks/macrobenchmarks/lib/src/very_long_picture_scrolling.dart

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ class VeryLongPictureScrollingPerfState extends State<VeryLongPictureScrollingPe
2424

2525
@override
2626
Widget build(BuildContext context) {
27+
final Size size = MediaQuery.sizeOf(context);
2728
return Scaffold(
2829
appBar: AppBar(
2930
actions: <Widget>[
@@ -52,9 +53,8 @@ class VeryLongPictureScrollingPerfState extends State<VeryLongPictureScrollingPe
5253
],
5354
),
5455
backgroundColor: Colors.transparent,
55-
body: SizedBox(
56-
width: MediaQuery.of(context).size.width,
57-
height: MediaQuery.of(context).size.height,
56+
body: SizedBox.fromSize(
57+
size: size,
5858
child: useList
5959
? ListView.builder(
6060
key: const ValueKey<String>('vlp_list_view_scrollable'),
@@ -74,8 +74,8 @@ class VeryLongPictureScrollingPerfState extends State<VeryLongPictureScrollingPe
7474
key: const ValueKey<String>('vlp_single_child_scrollable'),
7575
scrollDirection: Axis.horizontal,
7676
child: SizedBox(
77-
width: MediaQuery.of(context).size.width * 20,
78-
height: MediaQuery.of(context).size.height,
77+
width: size.width * 20,
78+
height: size.height,
7979
child: RepaintBoundary(
8080
child: CustomPaint(
8181
isComplex: true,

dev/integration_tests/flutter_gallery/lib/demo/pesto_demo.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -335,7 +335,7 @@ class _RecipePageState extends State<RecipePage> {
335335
height: 24.0 / 15.0,
336336
);
337337

338-
double _getAppBarHeight(BuildContext context) => MediaQuery.of(context).size.height * 0.3;
338+
double _getAppBarHeight(BuildContext context) => MediaQuery.heightOf(context) * 0.3;
339339

340340
@override
341341
Widget build(BuildContext context) {

dev/integration_tests/flutter_gallery/lib/demo/shrine/supplemental/asymmetric_view.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ class AsymmetricView extends StatelessWidget {
2626
// helpers for creating the index of the product list that will correspond
2727
// to the index of the list of columns.
2828
return List<SizedBox>.generate(_listItemCount(products!.length), (int index) {
29-
double width = .59 * MediaQuery.of(context).size.width;
29+
double width = .59 * MediaQuery.widthOf(context);
3030
Widget column;
3131
if (index.isEven) {
3232
/// Even cases

dev/integration_tests/new_gallery/lib/pages/category_list_item.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -167,7 +167,7 @@ class _CategoryHeader extends StatelessWidget {
167167
color: colorScheme.onBackground,
168168
clipBehavior: Clip.antiAlias,
169169
child: SizedBox(
170-
width: MediaQuery.of(context).size.width,
170+
width: MediaQuery.widthOf(context),
171171
child: InkWell(
172172
// Makes integration tests possible.
173173
key: ValueKey<String>('${category.name}CategoryHeader'),

dev/integration_tests/new_gallery/lib/pages/home.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -697,7 +697,7 @@ class _MobileCarouselState extends State<_MobileCarousel>
697697
super.didChangeDependencies();
698698
// The viewPortFraction is calculated as the width of the device minus the
699699
// padding.
700-
final double width = MediaQuery.of(context).size.width;
700+
final double width = MediaQuery.widthOf(context);
701701
const double padding = _carouselItemMobileMargin * 2;
702702
_controller = PageController(
703703
initialPage: _currentPage.value,

dev/integration_tests/new_gallery/lib/studies/rally/home.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -311,7 +311,7 @@ class _RallyTabState extends State<_RallyTab> with SingleTickerProviderStateMixi
311311
// units and dividing it into the screen width. Each unexpanded tab is 1
312312
// unit, and there is always 1 expanded tab which is 1 unit + any extra
313313
// space determined by the multiplier.
314-
final double width = MediaQuery.of(context).size.width;
314+
final double width = MediaQuery.widthOf(context);
315315
const expandedTitleWidthMultiplier = 2;
316316
final double unitWidth = width / (tabCount + expandedTitleWidthMultiplier);
317317

dev/integration_tests/new_gallery/lib/studies/reply/adaptive_nav.dart

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -559,8 +559,8 @@ class _MobileNavState extends State<_MobileNav> with TickerProviderStateMixin {
559559
child: FadeTransition(
560560
opacity: _drawerCurve,
561561
child: Container(
562-
height: MediaQuery.of(context).size.height,
563-
width: MediaQuery.of(context).size.width,
562+
height: MediaQuery.heightOf(context),
563+
width: MediaQuery.widthOf(context),
564564
color: Theme.of(context).bottomSheetTheme.modalBackgroundColor,
565565
),
566566
),

dev/integration_tests/new_gallery/lib/studies/shrine/login.dart

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,7 @@ import 'theme.dart';
1818
const double _horizontalPadding = 24.0;
1919

2020
double desktopLoginScreenMainAreaWidth({required BuildContext context}) {
21-
return min(
22-
360 * reducedTextScale(context),
23-
MediaQuery.of(context).size.width - 2 * _horizontalPadding,
24-
);
21+
return min(360 * reducedTextScale(context), MediaQuery.widthOf(context) - 2 * _horizontalPadding);
2522
}
2623

2724
class LoginPage extends StatelessWidget {

0 commit comments

Comments
 (0)