Skip to content

Commit a1b1386

Browse files
authored
More documentation for MediaQuery and friends (#130509)
Fixes flutter/flutter#11697
1 parent 64edc55 commit a1b1386

File tree

2 files changed

+21
-5
lines changed

2 files changed

+21
-5
lines changed

packages/flutter/lib/src/widgets/media_query.dart

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -219,6 +219,10 @@ class MediaQueryData {
219219
/// this method again when it changes to keep the constructed [MediaQueryData]
220220
/// updated.
221221
///
222+
/// In general, [MediaQuery.of] is the appropriate way to obtain
223+
/// [MediaQueryData] from a widget. This `fromView` constructor is primarily
224+
/// for use in the implementation of the framework itself.
225+
///
222226
/// See also:
223227
///
224228
/// * [MediaQuery.fromView], which constructs [MediaQueryData] from a provided
@@ -262,9 +266,12 @@ class MediaQueryData {
262266
/// It is considered bad practice to cache and later use the size returned
263267
/// by `MediaQuery.of(context).size`. It will make the application non responsive
264268
/// and might lead to unexpected behaviors.
265-
/// For instance, during startup, especially in release mode, the first returned
266-
/// size might be (0,0). The size will be updated when the native platform
267-
/// reports the actual resolution.
269+
///
270+
/// For instance, during startup, especially in release mode, the first
271+
/// returned size might be (0,0). The size will be updated when the native
272+
/// platform reports the actual resolution. Using [MediaQuery.of] will ensure
273+
/// that when the size changes, any widgets depending on the size are
274+
/// automatically rebuilt.
268275
///
269276
/// See the article on [Creating responsive and adaptive
270277
/// apps](https://docs.flutter.dev/development/ui/layout/adaptive-responsive)

packages/flutter/lib/src/widgets/view.dart

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,15 @@ import 'media_query.dart';
2121
/// The provided [child] is wrapped in a [MediaQuery] constructed from the given
2222
/// [view].
2323
///
24+
/// For most use cases, using [MediaQuery.of] is a more appropriate way of
25+
/// obtaining the information that a [FlutterView] exposes. For example, using
26+
/// [MediaQuery] will expose the _logical_ device size ([MediaQueryData.size])
27+
/// rather than the physical size ([FlutterView.physicalSize]). Similarly, while
28+
/// [FlutterView.padding] conveys the information from the operating system, the
29+
/// [MediaQueryData.padding] further adjusts this information to be aware of the
30+
/// context of the widget; e.g. the [Scaffold] widget adjusts the values for its
31+
/// various children.
32+
///
2433
/// Each [FlutterView] can be associated with at most one [View] widget in the
2534
/// widget tree. Two or more [View] widgets configured with the same
2635
/// [FlutterView] must never exist within the same widget tree at the same time.
@@ -93,7 +102,7 @@ class View extends StatelessWidget {
93102
/// The method creates a dependency on the `context`, which will be informed
94103
/// when the identity of the [FlutterView] changes (i.e. the `context` is
95104
/// moved to render into a different [FlutterView] then before). The context
96-
/// will not be informed when the properties on the [FlutterView] itself
105+
/// will not be informed when the _properties_ on the [FlutterView] itself
97106
/// change their values. To access the property values of a [FlutterView] it
98107
/// is best practise to use [MediaQuery.maybeOf] instead, which will ensure
99108
/// that the `context` is informed when the view properties change.
@@ -113,7 +122,7 @@ class View extends StatelessWidget {
113122
/// The method creates a dependency on the `context`, which will be informed
114123
/// when the identity of the [FlutterView] changes (i.e. the `context` is
115124
/// moved to render into a different [FlutterView] then before). The context
116-
/// will not be informed when the properties on the [FlutterView] itself
125+
/// will not be informed when the _properties_ on the [FlutterView] itself
117126
/// change their values. To access the property values of a [FlutterView] it
118127
/// is best practise to use [MediaQuery.of] instead, which will ensure that
119128
/// the `context` is informed when the view properties change.

0 commit comments

Comments
 (0)