1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
// Copyright 2015 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
import 'dart:ui' as ui;
import 'basic.dart';
import 'framework.dart';
/// Whether in portrait or landscape.
enum Orientation {
/// Taller than wide.
portrait,
/// Wider than tall.
landscape
}
/// The result of a media query.
class MediaQueryData {
const MediaQueryData({ this.size, this.devicePixelRatio, this.padding });
MediaQueryData.fromWindow(ui.Window window)
: size = window.size,
devicePixelRatio = window.devicePixelRatio,
padding = new EdgeInsets.fromWindowPadding(window.padding);
/// The size of the media (e.g, the size of the screen).
final Size size;
/// The number of device pixels for each logical pixel. This number might not
/// be a power of two. Indeed, it might not even be an integer. For example,
/// the Nexus 6 has a device pixel ratio of 3.5.
final double devicePixelRatio;
/// The padding around the edges of the media (e.g., the screen).
final EdgeInsets padding;
/// The orientation of the media (e.g., whether the device is in landscape or portrait mode).
Orientation get orientation {
return size.width > size.height ? Orientation.landscape : Orientation.portrait;
}
@override
bool operator==(Object other) {
if (other.runtimeType != runtimeType)
return false;
MediaQueryData typedOther = other;
return typedOther.size == size
&& typedOther.padding == padding
&& typedOther.devicePixelRatio == devicePixelRatio;
}
@override
int get hashCode => hashValues(
size.hashCode,
padding.hashCode,
devicePixelRatio.hashCode
);
@override
String toString() => '$runtimeType($size, $orientation)';
}
/// Establishes a subtree in which media queries resolve to the given data.
class MediaQuery extends InheritedWidget {
MediaQuery({
Key key,
this.data,
Widget child
}) : super(key: key, child: child) {
assert(child != null);
assert(data != null);
}
/// The result of media queries in this subtree.
final MediaQueryData data;
/// The data from the closest instance of this class that encloses the given context.
///
/// You can use this function to query the size an orientation of the screen.
/// When that information changes, your widget will be scheduled to be rebuilt,
/// keeping your widget up-to-date.
static MediaQueryData of(BuildContext context) {
MediaQuery query = context.inheritFromWidgetOfExactType(MediaQuery);
return query?.data ?? new MediaQueryData.fromWindow(ui.window);
}
@override
bool updateShouldNotify(MediaQuery old) => data != old.data;
@override
void debugFillDescription(List<String> description) {
super.debugFillDescription(description);
description.add('$data');
}
}