From 2fccf4d4714970c90800daa37b63ca7220de03d4 Mon Sep 17 00:00:00 2001 From: Michael Goderbauer Date: Tue, 31 Jan 2023 09:55:54 -0800 Subject: [PATCH] Remove MediaQuery from WidgetsApp (#119377) * Remove MediaQuery from WidgetsApp * more * fix test * more deprecation notices. --- packages/flutter/lib/src/cupertino/app.dart | 17 ++- packages/flutter/lib/src/material/app.dart | 17 ++- packages/flutter/lib/src/widgets/app.dart | 42 +++--- .../flutter/test/material/debug_test.dart | 2 - .../flutter/test/material/snack_bar_test.dart | 126 +++++++++--------- packages/flutter/test/widgets/app_test.dart | 49 ------- 6 files changed, 116 insertions(+), 137 deletions(-) diff --git a/packages/flutter/lib/src/cupertino/app.dart b/packages/flutter/lib/src/cupertino/app.dart index 8aface4f80..633cd08dab 100644 --- a/packages/flutter/lib/src/cupertino/app.dart +++ b/packages/flutter/lib/src/cupertino/app.dart @@ -176,6 +176,11 @@ class CupertinoApp extends StatefulWidget { this.actions, this.restorationScopeId, this.scrollBehavior, + @Deprecated( + 'Remove this parameter as it is now ignored. ' + 'CupertinoApp never introduces its own MediaQuery; the View widget takes care of that. ' + 'This feature was deprecated after v3.7.0-29.0.pre.' + ) this.useInheritedMediaQuery = false, }) : routeInformationProvider = null, routeInformationParser = null, @@ -212,6 +217,11 @@ class CupertinoApp extends StatefulWidget { this.actions, this.restorationScopeId, this.scrollBehavior, + @Deprecated( + 'Remove this parameter as it is now ignored. ' + 'CupertinoApp never introduces its own MediaQuery; the View widget takes care of that. ' + 'This feature was deprecated after v3.7.0-29.0.pre.' + ) this.useInheritedMediaQuery = false, }) : assert(routerDelegate != null || routerConfig != null), navigatorObservers = null, @@ -408,6 +418,11 @@ class CupertinoApp extends StatefulWidget { final ScrollBehavior? scrollBehavior; /// {@macro flutter.widgets.widgetsApp.useInheritedMediaQuery} + @Deprecated( + 'This setting is now ignored. ' + 'CupertinoApp never introduces its own MediaQuery; the View widget takes care of that. ' + 'This feature was deprecated after v3.7.0-29.0.pre.' + ) final bool useInheritedMediaQuery; @override @@ -541,7 +556,6 @@ class _CupertinoAppState extends State { shortcuts: widget.shortcuts, actions: widget.actions, restorationScopeId: widget.restorationScopeId, - useInheritedMediaQuery: widget.useInheritedMediaQuery, ); } return WidgetsApp( @@ -576,7 +590,6 @@ class _CupertinoAppState extends State { shortcuts: widget.shortcuts, actions: widget.actions, restorationScopeId: widget.restorationScopeId, - useInheritedMediaQuery: widget.useInheritedMediaQuery, ); } diff --git a/packages/flutter/lib/src/material/app.dart b/packages/flutter/lib/src/material/app.dart index 10ccaf73e2..ce49ccde87 100644 --- a/packages/flutter/lib/src/material/app.dart +++ b/packages/flutter/lib/src/material/app.dart @@ -241,6 +241,11 @@ class MaterialApp extends StatefulWidget { this.actions, this.restorationScopeId, this.scrollBehavior, + @Deprecated( + 'Remove this parameter as it is now ignored. ' + 'MaterialApp never introduces its own MediaQuery; the View widget takes care of that. ' + 'This feature was deprecated after v3.7.0-29.0.pre.' + ) this.useInheritedMediaQuery = false, }) : routeInformationProvider = null, routeInformationParser = null, @@ -285,6 +290,11 @@ class MaterialApp extends StatefulWidget { this.actions, this.restorationScopeId, this.scrollBehavior, + @Deprecated( + 'Remove this parameter as it is now ignored. ' + 'MaterialApp never introduces its own MediaQuery; the View widget takes care of that. ' + 'This feature was deprecated after v3.7.0-29.0.pre.' + ) this.useInheritedMediaQuery = false, }) : assert(routerDelegate != null || routerConfig != null), navigatorObservers = null, @@ -733,6 +743,11 @@ class MaterialApp extends StatefulWidget { final bool debugShowMaterialGrid; /// {@macro flutter.widgets.widgetsApp.useInheritedMediaQuery} + @Deprecated( + 'This setting is now ignored. ' + 'MaterialApp never introduces its own MediaQuery; the View widget takes care of that. ' + 'This feature was deprecated after v3.7.0-29.0.pre.' + ) final bool useInheritedMediaQuery; @override @@ -983,7 +998,6 @@ class _MaterialAppState extends State { shortcuts: widget.shortcuts, actions: widget.actions, restorationScopeId: widget.restorationScopeId, - useInheritedMediaQuery: widget.useInheritedMediaQuery, ); } @@ -1019,7 +1033,6 @@ class _MaterialAppState extends State { shortcuts: widget.shortcuts, actions: widget.actions, restorationScopeId: widget.restorationScopeId, - useInheritedMediaQuery: widget.useInheritedMediaQuery, ); } diff --git a/packages/flutter/lib/src/widgets/app.dart b/packages/flutter/lib/src/widgets/app.dart index 9e915a79c0..06482436d4 100644 --- a/packages/flutter/lib/src/widgets/app.dart +++ b/packages/flutter/lib/src/widgets/app.dart @@ -241,9 +241,6 @@ typedef InitialRouteListFactory = List> Function(String initialRo /// It is used by both [MaterialApp] and [CupertinoApp] to implement base /// functionality for an app. /// -/// Builds a [MediaQuery] using [MediaQuery.fromWindow]. To use an inherited -/// [MediaQuery] instead, set [useInheritedMediaQuery] to true. -/// /// Find references to many of the widgets that [WidgetsApp] wraps in the "See /// also" section. /// @@ -343,6 +340,11 @@ class WidgetsApp extends StatefulWidget { this.shortcuts, this.actions, this.restorationScopeId, + @Deprecated( + 'Remove this parameter as it is now ignored. ' + 'WidgetsApp never introduces its own MediaQuery; the View widget takes care of that. ' + 'This feature was deprecated after v3.7.0-29.0.pre.' + ) this.useInheritedMediaQuery = false, }) : assert( home == null || @@ -437,6 +439,11 @@ class WidgetsApp extends StatefulWidget { this.shortcuts, this.actions, this.restorationScopeId, + @Deprecated( + 'Remove this parameter as it is now ignored. ' + 'WidgetsApp never introduces its own MediaQuery; the View widget takes care of that. ' + 'This feature was deprecated after v3.7.0-29.0.pre.' + ) this.useInheritedMediaQuery = false, }) : assert((){ if (routerConfig != null) { @@ -1153,11 +1160,16 @@ class WidgetsApp extends StatefulWidget { final String? restorationScopeId; /// {@template flutter.widgets.widgetsApp.useInheritedMediaQuery} - /// If true, an inherited MediaQuery will be used. If one is not available, - /// or this is false, one will be built from the window. + /// Deprecated. This setting is not ignored. /// - /// Cannot be null, defaults to false. + /// The widget never introduces its own [MediaQuery]; the [View] widget takes + /// care of that. /// {@endtemplate} + @Deprecated( + 'This setting is now ignored. ' + 'WidgetsApp never introduces its own MediaQuery; the View widget takes care of that. ' + 'This feature was deprecated after v3.7.0-29.0.pre.' + ) final bool useInheritedMediaQuery; /// If true, forces the performance overlay to be visible in all instances. @@ -1724,18 +1736,6 @@ class _WidgetsAppState extends State with WidgetsBindingObserver { assert(_debugCheckLocalizations(appLocale)); - Widget child = Localizations( - locale: appLocale, - delegates: _localizationsDelegates.toList(), - child: title, - ); - - if (!widget.useInheritedMediaQuery || MediaQuery.maybeOf(context) == null) { - child = MediaQuery.fromWindow( - child: child, - ); - } - return RootRestorationScope( restorationId: widget.restorationScopeId, child: SharedAppData( @@ -1754,7 +1754,11 @@ class _WidgetsAppState extends State with WidgetsBindingObserver { policy: ReadingOrderTraversalPolicy(), child: TapRegionSurface( child: ShortcutRegistrar( - child: child, + child: Localizations( + locale: appLocale, + delegates: _localizationsDelegates.toList(), + child: title, + ), ), ), ), diff --git a/packages/flutter/test/material/debug_test.dart b/packages/flutter/test/material/debug_test.dart index c9f7a3bcb4..e89a65c595 100644 --- a/packages/flutter/test/material/debug_test.dart +++ b/packages/flutter/test/material/debug_test.dart @@ -192,8 +192,6 @@ void main() { ' _LocalizationsScope-[GlobalKey#00000]\n' ' Semantics\n' ' Localizations\n' - ' MediaQuery\n' - ' _MediaQueryFromView\n' ' Semantics\n' ' _FocusInheritedScope\n' ' Focus\n' diff --git a/packages/flutter/test/material/snack_bar_test.dart b/packages/flutter/test/material/snack_bar_test.dart index bc1c699c67..cbd65422cd 100644 --- a/packages/flutter/test/material/snack_bar_test.dart +++ b/packages/flutter/test/material/snack_bar_test.dart @@ -1530,71 +1530,71 @@ void main() { expect(snackBarBottomLeft, equals(bottomNavigationBarTopLeft)); }, ); - - testWidgets( - 'Padding of $behavior is not consumed by viewInsets', - (WidgetTester tester) async { - final Widget child = MaterialApp( - home: Scaffold( - resizeToAvoidBottomInset: false, - floatingActionButton: FloatingActionButton( - child: const Icon(Icons.send), - onPressed: () {}, - ), - body: Builder( - builder: (BuildContext context) { - return GestureDetector( - onTap: () { - ScaffoldMessenger.of(context).showSnackBar( - SnackBar( - content: const Text('I am a snack bar.'), - duration: const Duration(seconds: 2), - action: SnackBarAction(label: 'ACTION', onPressed: () {}), - behavior: behavior, - ), - ); - }, - child: const Text('X'), - ); - }, - ), - ), - ); - - await tester.pumpWidget( - MediaQuery( - data: const MediaQueryData( - padding: EdgeInsets.only(bottom: 20.0), - ), - child: child, - ), - ); - await tester.tap(find.text('X')); - await tester.pumpAndSettle(); // Show snackbar - final Offset initialBottomLeft = tester.getBottomLeft(find.byType(SnackBar)); - final Offset initialBottomRight = tester.getBottomRight(find.byType(SnackBar)); - // Consume bottom padding - as if by the keyboard opening - await tester.pumpWidget( - MediaQuery( - data: const MediaQueryData( - viewPadding: EdgeInsets.all(20), - viewInsets: EdgeInsets.all(100), - ), - child: child, - ), - ); - await tester.tap(find.text('X')); - await tester.pumpAndSettle(); // Have the SnackBar fully animate out. - - final Offset finalBottomLeft = tester.getBottomLeft(find.byType(SnackBar)); - final Offset finalBottomRight = tester.getBottomRight(find.byType(SnackBar)); - - expect(initialBottomLeft, finalBottomLeft); - expect(initialBottomRight, finalBottomRight); - }, - ); } + testWidgets( + 'Padding of ${SnackBarBehavior.fixed} is not consumed by viewInsets', + (WidgetTester tester) async { + final Widget child = MaterialApp( + home: Scaffold( + resizeToAvoidBottomInset: false, + floatingActionButton: FloatingActionButton( + child: const Icon(Icons.send), + onPressed: () {}, + ), + body: Builder( + builder: (BuildContext context) { + return GestureDetector( + onTap: () { + ScaffoldMessenger.of(context).showSnackBar( + SnackBar( + content: const Text('I am a snack bar.'), + duration: const Duration(seconds: 2), + action: SnackBarAction(label: 'ACTION', onPressed: () {}), + behavior: SnackBarBehavior.fixed, + ), + ); + }, + child: const Text('X'), + ); + }, + ), + ), + ); + + await tester.pumpWidget( + MediaQuery( + data: const MediaQueryData( + padding: EdgeInsets.only(bottom: 20.0), + ), + child: child, + ), + ); + await tester.tap(find.text('X')); + await tester.pumpAndSettle(); // Show snackbar + final Offset initialBottomLeft = tester.getBottomLeft(find.byType(SnackBar)); + final Offset initialBottomRight = tester.getBottomRight(find.byType(SnackBar)); + // Consume bottom padding - as if by the keyboard opening + await tester.pumpWidget( + MediaQuery( + data: const MediaQueryData( + viewPadding: EdgeInsets.all(20), + viewInsets: EdgeInsets.all(100), + ), + child: child, + ), + ); + await tester.tap(find.text('X')); + await tester.pumpAndSettle(); // Have the SnackBar fully animate out. + + final Offset finalBottomLeft = tester.getBottomLeft(find.byType(SnackBar)); + final Offset finalBottomRight = tester.getBottomRight(find.byType(SnackBar)); + + expect(initialBottomLeft, finalBottomLeft); + expect(initialBottomRight, finalBottomRight); + }, + ); + testWidgets( '${SnackBarBehavior.fixed} should align SnackBar with the bottom of Scaffold ' 'when Scaffold has a FloatingActionButton', diff --git a/packages/flutter/test/widgets/app_test.dart b/packages/flutter/test/widgets/app_test.dart index 6d58a4afd2..15ab3f9d4f 100644 --- a/packages/flutter/test/widgets/app_test.dart +++ b/packages/flutter/test/widgets/app_test.dart @@ -595,55 +595,6 @@ void main() { expect(tester.takeException(), "Warning: This application's locale, C_UTF-8, is not supported by all of its localization delegates."); }); - testWidgets('WidgetsApp creates a MediaQuery if `useInheritedMediaQuery` is set to false', (WidgetTester tester) async { - late BuildContext capturedContext; - await tester.pumpWidget( - WidgetsApp( - builder: (BuildContext context, Widget? child) { - capturedContext = context; - return const Placeholder(); - }, - color: const Color(0xFF123456), - ), - ); - expect(MediaQuery.of(capturedContext), isNotNull); - }); - - testWidgets('WidgetsApp does not create MediaQuery if `useInheritedMediaQuery` is set to true and one is available', (WidgetTester tester) async { - late BuildContext capturedContext; - final UniqueKey uniqueKey = UniqueKey(); - await tester.pumpWidget( - MediaQuery( - key: uniqueKey, - data: const MediaQueryData(), - child: WidgetsApp( - useInheritedMediaQuery: true, - builder: (BuildContext context, Widget? child) { - capturedContext = context; - return const Placeholder(); - }, - color: const Color(0xFF123456), - ), - ), - ); - expect(capturedContext.dependOnInheritedWidgetOfExactType()?.key, uniqueKey); - }); - - testWidgets('WidgetsApp does create a MediaQuery if `useInheritedMediaQuery` is set to true and none is available', (WidgetTester tester) async { - late BuildContext capturedContext; - await tester.pumpWidget( - WidgetsApp( - useInheritedMediaQuery: true, - builder: (BuildContext context, Widget? child) { - capturedContext = context; - return const Placeholder(); - }, - color: const Color(0xFF123456), - ), - ); - expect(MediaQuery.of(capturedContext), isNotNull); - }); - testWidgets("WidgetsApp doesn't have dependency on MediaQuery", (WidgetTester tester) async { int routeBuildCount = 0;