Merge pull request #1336 from krisgiesing/mediaquery
Add device pixel ratio to MediaQuery
This commit is contained in:
@@ -77,15 +77,11 @@ class _MaterialAppState extends State<MaterialApp> implements BindingObserver {
|
||||
|
||||
GlobalObjectKey _navigator;
|
||||
|
||||
Size _size;
|
||||
EdgeDims _padding;
|
||||
LocaleQueryData _localeData;
|
||||
|
||||
void initState() {
|
||||
super.initState();
|
||||
_navigator = new GlobalObjectKey(this);
|
||||
_size = ui.window.size;
|
||||
_padding = _getPadding(ui.window);
|
||||
didChangeLocale(ui.window.locale);
|
||||
WidgetFlutterBinding.instance.addObserver(this);
|
||||
}
|
||||
@@ -106,10 +102,10 @@ class _MaterialAppState extends State<MaterialApp> implements BindingObserver {
|
||||
return result;
|
||||
}
|
||||
|
||||
void didChangeSize(Size size) {
|
||||
void didChangeMetrics() {
|
||||
setState(() {
|
||||
_size = size;
|
||||
_padding = _getPadding(ui.window);
|
||||
// The properties of ui.window have changed. We use them in our build
|
||||
// function, so we need setState(), but we don't cache anything locally.
|
||||
});
|
||||
}
|
||||
|
||||
@@ -150,7 +146,11 @@ class _MaterialAppState extends State<MaterialApp> implements BindingObserver {
|
||||
|
||||
ThemeData theme = config.theme ?? new ThemeData.fallback();
|
||||
Widget result = new MediaQuery(
|
||||
data: new MediaQueryData(size: _size, padding: _padding),
|
||||
data: new MediaQueryData(
|
||||
size: ui.window.size,
|
||||
devicePixelRatio: ui.window.devicePixelRatio,
|
||||
padding: _getPadding(ui.window)
|
||||
),
|
||||
child: new LocaleQuery(
|
||||
data: _localeData,
|
||||
child: new AnimatedTheme(
|
||||
|
||||
@@ -14,7 +14,7 @@ import 'framework.dart';
|
||||
|
||||
class BindingObserver {
|
||||
bool didPopRoute() => false;
|
||||
void didChangeSize(Size size) { }
|
||||
void didChangeMetrics() { }
|
||||
void didChangeLocale(ui.Locale locale) { }
|
||||
void didChangeAppLifecycleState(ui.AppLifecycleState state) { }
|
||||
}
|
||||
@@ -62,7 +62,7 @@ class WidgetFlutterBinding extends BindingBase with Scheduler, Gesturer, Rendere
|
||||
void handleMetricsChanged() {
|
||||
super.handleMetricsChanged();
|
||||
for (BindingObserver observer in _observers)
|
||||
observer.didChangeSize(ui.window.size);
|
||||
observer.didChangeMetrics();
|
||||
}
|
||||
|
||||
void handleLocaleChanged() {
|
||||
|
||||
@@ -16,11 +16,16 @@ enum Orientation {
|
||||
|
||||
/// The result of a media query.
|
||||
class MediaQueryData {
|
||||
const MediaQueryData({ this.size, this.padding });
|
||||
const MediaQueryData({ this.size, this.devicePixelRatio, this.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 EdgeDims padding;
|
||||
|
||||
@@ -34,10 +39,15 @@ class MediaQueryData {
|
||||
return false;
|
||||
MediaQueryData typedOther = other;
|
||||
return typedOther.size == size
|
||||
&& typedOther.padding == padding;
|
||||
&& typedOther.padding == padding
|
||||
&& typedOther.devicePixelRatio == devicePixelRatio;
|
||||
}
|
||||
|
||||
int get hashCode => size.hashCode;
|
||||
int get hashCode => hashValues(
|
||||
size.hashCode,
|
||||
padding.hashCode,
|
||||
devicePixelRatio.hashCode
|
||||
);
|
||||
|
||||
String toString() => '$runtimeType($size, $orientation)';
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user