media-queries - "matchMedia"Dart 支持

标签 media-queries dart

如何在 Dart 中使用 window.matchMedia?

我找到了相应的方法:

MediaQueryList matchMedia(String query)

和“MediaQueryList”方法:

void addListener(MediaQueryListListener listener)

但是:MediaQueryListListener没有构造函数,看起来像某种生成的 stub 。

我有JS example :

var mq = window.matchMedia( "(min-width: 500px)" );

// media query event handler
if (matchMedia) {
    var mq = window.matchMedia("(min-width: 500px)");
    mq.addListener(WidthChange);
    WidthChange(mq);
}

// media query change
function WidthChange(mq) {

    if (mq.matches) {
        // window width is at least 500px
    }
    else {
        // window width is less than 500px
    }

}

并且它有很好的支持http://caniuse.com/#feat=matchmedia

最佳答案

pointed in a comment目前似乎还没有在 Dart 中实现。

但是您可以使用 dart:js 来执行此操作:

import 'dart:js';

main() {
  if (context['matchMedia'] != null) {
    final mq = context.callMethod('matchMedia', ['(min-width: 500px)']);
    mq.callMethod('addListener', [widthChange]);
    widthChange(mq);
  }
}
widthChange(mq) {
  if (mq['matches']) {
    print('window width is at least 500px');
  } else {
    print('window width is less than 500px');
  }
}

关于media-queries - "matchMedia"Dart 支持,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19745361/

相关文章:

没有内联 CSS 的 JQuery 切换(向左滑动)

html - 媒体查询覆盖桌面样式

dart - 在 flutter 中处理多点触控

flutter - 如何在 FutureBuilder 中显示对话框?

css - 媒体查询被一般声明覆盖

html - 如何处理 Gmail 中 HTML 电子邮件中的媒体查询?

css - 用于创建移动版本的媒体查询和 "display: none"

android - 找不到 Flutter 项目的 lint-gradle-api-26.1.2.jar

android - 如何向 Flutter 相机添加闪光灯切换按钮 (flutter_camera_ml_vision)

dart - 如何在 Flutter 中清除 AnimatedList 中的所有项目