• Flutter在web项目中使用iframe


    需要把原来的app项目移植到web上面,在app中使用的是flutter_inappwebview这个库,推荐使用这个库,因为修复了一部分webview_flutter中存在的问题

    在web项目中flutter_inappwebview这个库不支持,所以需要自己封装一个web项目中的webview也就是使用iframe

    废话少说上代码

    import 'dart:html';
    import 'dart:js' as js;
    
    import 'dart:ui_web';
    
    import 'package:flutter/material.dart';
    import 'package:flutter/widgets.dart';
    import 'package:get/get.dart';
    import 'package:myapp/app/services/screen_adapter.dart';
    
    Widget buildWebViewWidget(String url,
        {Function(int id)? onPlatformViewCreated}) {
    
      /// 给js调用的函数
      void back(){
        Get.back();
      }
    
      var platformViewRegistry = PlatformViewRegistry();
      //注册
      platformViewRegistry.registerViewFactory('iframe-webview', (_) {
        DivElement _mainDiv = DivElement()
          ..style.width = '100%'
          ..style.height = '100%'
          ..style.position = 'relative';
    
        IFrameElement _iFameElement = IFrameElement()
          ..style.height = '100%'
          ..style.width = '100%'
          ..src = url
          ..style.border = 'none';
    
        ScriptElement scriptElement = ScriptElement();
        var script = """
              
               // 对话框js
              window.confirm = function(message, yesCallback, noCallback){
                 var message = message;
                 var choose = function(tag){
                    return document.querySelector(tag);
                 }
                 choose('.dialog-message').innerHtml = message;
                 choose(".wrap-dialog").className = "wrap-dialog";
                 choose("#dialog").οnclick= function(e){
                    if(e.target.className=="dialog-btn"){
                         choose(".wrap-dialog").className = "wrap-dialog dialog-hide";
                         yesCallback();
                     }else if (e.target.className=="dialog-btn dialog-ml50"){
                         choose(".wrap-dialog").className = "wrap-dialog dialog-hide";
                          noCallback();
                     }
                 };
              }
        
               // 返回按钮功能
               var drag = document.getElementById("floatBtn");
               var gapWidth = ${ScreenAdapter.width(5)}
               var itemWidth = ${ScreenAdapter.width(80)}
               var itemHeight =  ${ScreenAdapter.width(80)}
               var clientWidth = document.documentElement.clientWidth
               var clientHeight = document.documentElement.clientHeight
               var newLeft = 0
               var newTop = 0
               
                drag.addEventListener('click',function(e){
                  //e.stopPropagation();
                  var r = confirm('确定返回首页吗?', function(){
                     window.back();
                  }, function(){})
                })
              
                drag.addEventListener('touchstart', function(e){
                  // e.preventDefault();
                  //e.stopPropagation();
                  drag.style.transition = 'none';
                })
              
                drag.addEventListener('touchmove', function(e){
                  // e.preventDefault();
                 // e.stopPropagation();
                  if (e.targetTouches.length === 1) {
                       let touch = e.targetTouches[0]
                       newLeft = touch.clientX - itemWidth / 2;
                       newTop = touch.clientY - itemHeight / 2;
                       if(newLeft  < 0){
                          newLeft = 0
                       } 
                       if(newLeft > clientWidth - itemWidth - gapWidth){
                          newLeft > clientWidth - itemWidth - gapWidth
                       }
                       if(newTop < 0){
                          newTop=0;
                       }
                       if(newTop > clientHeight - itemHeight - gapWidth){
                         newTop = clientHeight - itemHeight - gapWidth
                       }
                       drag.style.left = newLeft + 'px'
                       drag.style.top = newTop + 'px'
                  }
                })
                drag.addEventListener('touchend', function (e) {
                     // e.preventDefault()
                     //e.stopPropagation();
                    drag.style.transition = 'all 0.3s'
                    if (newLeft > clientWidth / 2) {
                        newLeft = clientWidth - itemWidth - gapWidth
                    } else {
                        newLeft = gapWidth
                    }
                    drag.style.left = newLeft + 'px'
                })   
        """;
    
    
    
        scriptElement.innerHtml = script;
    
        /// 返回按钮div
        DivElement _div = DivElement()
          ..id = 'floatBtn'
          ..draggable = true
          ..style.width =  '${ScreenAdapter.width(80)}px'
          ..style.height =  '${ScreenAdapter.width(80)}px'
          // ..style.backgroundColor = 'red'
          ..style.backgroundImage = 'url(assets/assets/images/fanhui.png)'
          ..style.backgroundSize = 'cover'
          ..style.position = 'absolute'
          ..style.left = '${ScreenAdapter.width(5)}px'
          ..style.top = '0'
          ..style.transition = 'all 0.3s'
          ..style.zIndex = '9999';
    
    
        // 对话框样式
        StyleElement _style = StyleElement();
        _style.innerHtml = """
        html,
        body {
            margin: 0;
            padding: 0;
            font-family: "Microsoft YaHei";
        }
    
        .wrap-dialog {
            position: fixed;
            top: 0;
            left: 0;
            width: 100%;
            height: 100%;
            font-size: 16px;
            text-align: center;
            background-color: rgba(0, 0, 0, .4);
            z-index: 10000;
            display: flex;
            justify-content: center;
            align-items: center;
        }
    
        .dialog {
            position: relative;
            margin: 10% auto;
            width: 300px;
            background-color: #FFFFFF;
        }
    
        .dialog .dialog-header {
            height: 20px;
            padding: 10px;
            background-color: #22b9ff;
        }
    
        .dialog .dialog-body {
            height: 30px;
            padding: 20px;
        }
    
        .dialog .dialog-footer {
            padding: 8px;
            background-color: #f5f5f5;
        }
    
        .dialog-btn {
            width: 70px;
            padding: 2px;
            cursor: pointer;
        }
    
        .dialog-hide {
            display: none;
        }
    
        .dialog-ml50 {
            margin-left: 50px;
        }
        """;
        _mainDiv.append(_style);
    
        // 提示信息框dom
        DivElement _dialogDiv = DivElement();
        _dialogDiv.innerHtml = """
        
    提示
    确定要返回首页吗?
    """
    ; _mainDiv.append(scriptElement); _mainDiv.append(_dialogDiv); _mainDiv.append(_div); _mainDiv.append(_iFameElement); return _mainDiv; }); /// 注册返回函数 js.context['back'] = back; return SizedBox( width: double.infinity, height: double.infinity, child: HtmlElementView( viewType: 'iframe-webview', onPlatformViewCreated: (int id) { onPlatformViewCreated?.call(id); }, ), ); }
    • 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
    • 98
    • 99
    • 100
    • 101
    • 102
    • 103
    • 104
    • 105
    • 106
    • 107
    • 108
    • 109
    • 110
    • 111
    • 112
    • 113
    • 114
    • 115
    • 116
    • 117
    • 118
    • 119
    • 120
    • 121
    • 122
    • 123
    • 124
    • 125
    • 126
    • 127
    • 128
    • 129
    • 130
    • 131
    • 132
    • 133
    • 134
    • 135
    • 136
    • 137
    • 138
    • 139
    • 140
    • 141
    • 142
    • 143
    • 144
    • 145
    • 146
    • 147
    • 148
    • 149
    • 150
    • 151
    • 152
    • 153
    • 154
    • 155
    • 156
    • 157
    • 158
    • 159
    • 160
    • 161
    • 162
    • 163
    • 164
    • 165
    • 166
    • 167
    • 168
    • 169
    • 170
    • 171
    • 172
    • 173
    • 174
    • 175
    • 176
    • 177
    • 178
    • 179
    • 180
    • 181
    • 182
    • 183
    • 184
    • 185
    • 186
    • 187
    • 188
    • 189
    • 190
    • 191
    • 192
    • 193
    • 194
    • 195
    • 196
    • 197
    • 198
    • 199
    • 200
    • 201
    • 202
    • 203
    • 204
    • 205
    • 206
    • 207
    • 208
    • 209
    • 210
    • 211
    • 212
    • 213
    • 214
    • 215
    • 216
    • 217
    • 218
    • 219
    • 220
    • 221
    • 222
    • 223
    • 224
    • 225
    • 226
    • 227
    • 228
    • 229
    • 230
    • 231
    • 232
    • 233
    • 234
    • 235
    • 236
    • 237
    • 238
    • 239

    使用方法

    直接判断是否是web,GetPlatform.isWeb ,如果是则使用上面封装的,不是则调用其他平台的

    if(GetPlatform.isWeb)
       buildWebViewWidgetPlatform(controller.url,
           onPlatformViewCreated: (id) {
               controller.isLoading.value = false;
       })
    
    • 1
    • 2
    • 3
    • 4
    • 5

    参考网址

    https://juejin.cn/post/7294638699417042954

  • 相关阅读:
    世界儿童日,周大福真诚关爱儿童成长
    APS高级排产助取暖器企业实现生产计划管理效率的提升
    【Unity ShaderGraph】| 制作一个 高级流体水球效果
    【IO流系列】字符流练习(拷贝、文件加密、修改文件数据)
    电商平台运用会员体系运营的好处以及注意事项
    windows和docker环境下springboot整合gdal3.x
    编程为什么那么难
    Linux防火墙命令
    MySQL TIMESTAMPDIFF函数的使用场景
    电脑重装系统桌面图标变成白色文件怎么恢复?
  • 原文地址:https://blog.csdn.net/qq_28710983/article/details/134499182