How to request fullscreen in compiled dart

后端 未结 2 1642
小鲜肉
小鲜肉 2021-01-07 01:57

I\'m playing around with a Dart app trying to get fullscreen mode to work. My HTML (excluding boilerplate):

Clicking this
相关标签:
2条回答
  • 2021-01-07 02:28

    As pointed out in the comments (thanks Günter!), this is a known issue. #12 in that thread posted a good workaround, edited by me to be a bit more generic:

    import 'dart:js';
    void fullscreenWorkaround(Element element) {
      var elem = new JsObject.fromBrowserObject(element);
    
      if (elem.hasProperty("requestFullscreen")) {
        elem.callMethod("requestFullscreen");
      }
      else {
        List<String> vendors = ['moz', 'webkit', 'ms', 'o'];
        for (String vendor in vendors) {
          String vendorFullscreen = "${vendor}RequestFullscreen";
          if (vendor == 'moz') {
            vendorFullscreen = "${vendor}RequestFullScreen";
          }
          if (elem.hasProperty(vendorFullscreen)) {
            elem.callMethod(vendorFullscreen);
            return;
          }
        }
      }
    }
    

    I used this in my code, and replaced this call

    div.requestFullscreen();
    

    with

    fullscreenWorkaround(div);
    

    which worked great. Tested and working compiled on Chrome and IE.

    0 讨论(0)
  • 2021-01-07 02:39

    Here is an extended version of Tobbe hack to use the whole fullscreen API.

    import 'dart:html';
    import 'dart:js';
    
    // Workaround for https://code.google.com/p/dart/issues/detail?id=4136
    class FullscreenWorkaround {
      static void requestFullscreen(Element element) {
        _callMethods(element, [
          'requestFullscreen',
          'webkitRequestFullscreen',
          'mozRequestFullScreen',
          'msRequestFullscreen',
          'oRequestFullscreen'
        ]);
      }
    
      static void exitFullscreen() {
        _callMethods(document, [
          'exitFullscreen',
          'webkitExitFullscreen',
          'mozCancelFullScreen',
          'msExitFullscreen',
          'oExitFullscreen'
        ]);
      }
    
      static bool get fullscreenEnabled {
        var result = _getProperty(document, [
          'fullscreenEnabled',
          'webkitFullscreenEnabled',
          'mozFullScreenEnabled',
          'msFullscreenEnabled',
          'oFullscreenEnabled'
        ]);
        return result != null ? result : false;
      }
    
      static get fullscreenElement {
        return _getProperty(document, [
          'fullscreenElement',
          'webkitFullscreenElement',
          'mozFullScreenElement',
          'msFullscreenElement',
          'oFullscreenElement'
        ]);
      }
    
      static _callMethods(browserObject, List methods) {
        var jsElem = new JsObject.fromBrowserObject(browserObject);
    
        for (String methodName in methods) {
          if (jsElem.hasProperty(methodName)) {
            return jsElem.callMethod(methodName);
          }
        }
      }
    
      static _getProperty(browserObject, List properties) {
        var jsElem = new JsObject.fromBrowserObject(browserObject);
    
        for (String propertyName in properties) {
          if (jsElem.hasProperty(propertyName)) {
            return jsElem[propertyName];
          }
        }
      }
    }
    
    0 讨论(0)
提交回复
热议问题