flutter js交互传参

加载网页的webView

WebView(
              initialUrl:
              'http://test/h5atui//#/mobileMaps?lng=${
      
      CommonConfig.lng}&lat=${
      
      CommonConfig.lat}',
              javascriptMode: JavascriptMode.unrestricted,
              onWebViewCreated: (controller) {
    
    
                _webViewController = controller;
              },
              onProgress: (process){
    
    
                setState(() {
    
    
                  _processPercent = process / 100;
                });
              },
              onPageFinished: (url) {
    
    },
              javascriptChannels: <JavascriptChannel>[
                _postUpGo(),
                _positioningGo(),
              ].toSet(),
            )

1.js传参给移动端

_postUpGo() {
    
    
    return JavascriptChannel(
        name: 'postUpGo',
        onMessageReceived: (JavascriptMessage message) {
    
    
          dynamic value = json.decode(message.message);
          print('value= $value');
          var itemType = value['itemType'];
          var itemId = value['itemId'];
          var type = value['type'];
          var wantType = value['wantType'];
       ///根据获取的参数做跳转之类的相应操作
        });
  }

2.移动端传参数给web

_positioningGo() {
    
    
    return JavascriptChannel(
        name: 'positioningGo',
        onMessageReceived: (JavascriptMessage message) async {
    
    
          var latLng = await LocationUtil().refreshAddressFromLatLng();
          if(_webViewController!=null && latLng!=null){
    
    
          //移动端调用js函数handleCheckChangeEnd并传参
            _webViewController.runJavascript('handleCheckChangeEnd("$latLng")');
          }
        });
  }

猜你喜欢

转载自blog.csdn.net/androidhyf/article/details/131659699