Skip to content

xiaomaxuetu/flutter_jsbridge_plugin

 
 

Repository files navigation

Add JsBridge Plugin to the WebView

A Flutter plugin that provides a JsBridge on WebView widget.

This plugin must introduce webview_flutter

Usage

Add flutter_jsbridge_plugin as a dependency in your pubspec.yaml file.

dependencies:
  flutter_jsbridge_plugin:
    git:
      url: git://github.com/xiaomaxuetu/flutter_jsbridge_plugin
      ref: master

Init JsBridge and register handler

...
final JsBridge _jsBridge = JsBridge();
...
WebView(
    initialUrl: "https://www.baidu.com?timeStamp=${new DateTime.now().millisecondsSinceEpoch}",
    javascriptMode: JavascriptMode.unrestricted,
    onWebViewCreated: (WebViewController webViewController) async {
        _jsBridge.loadJs(webViewController);
        _controller.complete(webViewController);
        _jsBridge.registerHandler("getToken", onCallBack: (data, func) {
            // return token to js
            func({"token": "token"});
        });
        _jsBridge.registerHandler("IAPpayment", onCallBack: (data, func) {
            print("js call flutter iap");
        });
        _jsBridge.registerHandler("back", onCallBack: (data, func) {
            print("js call flutter back");
        });
    },
    navigationDelegate: (NavigationRequest request) {
        if (_jsBridge.handlerUrl(request.url)) {
            return NavigationDecision.navigate;
        }
        return NavigationDecision.prevent;
    },
    onPageStarted: (url) {
        _jsBridge.init();
    },
))

JS Usage

js file

About

No description, website, or topics provided.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • Dart 66.7%
  • Java 10.9%
  • Ruby 6.9%
  • JavaScript 6.8%
  • Objective-C 5.0%
  • HTML 3.7%