“matchMedia” support in Dart

前端 未结 1 845
感动是毒
感动是毒 2021-01-15 04:41

How to use window.matchMedia in Dart?

I have found corresponding method:

MediaQueryList matchMedia(String          


        
相关标签:
1条回答
  • 2021-01-15 05:13

    As pointed in a comment it doesn't seem to be implemented in Dart for now.

    However you can use dart:js to do that like this :

    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');
      }
    }
    
    0 讨论(0)
提交回复
热议问题