Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

I want to inject a button to HTML, then process interaction between webview and flutter #426

Closed
codlin opened this issue May 24, 2019 · 3 comments

Comments

@codlin
Copy link

codlin commented May 24, 2019

I want to inject a button to HTML, if user click the button , flutter should be pop a snack bar, ,how to do these? thanks.

@adammesa
Copy link

adammesa commented May 31, 2019

Injecting the button can be done via javascript, which in turn can inject your button.

As mentioned in the readme file of this project, the WebView is above the regular widget tree, no widgets (like a snackbar) can be displayed above the WebView. My recommendation for this would be to inject the appropriate JS, HTML, and CSS to create a pseudo snackbar within the page you're visiting.

If you wanted your button press to be registered by your flutter code (not just the sandbox of JS that you've injected), I haven't seen any good solutions yet. The only thing I've thought that may work is by encoding the response in the WebView URL. For example, your button on page page.html would link to page.html&trigger=clicked, and I assume that the URL changed function will be triggered. Listen on it, and check for the encoded response and run your own code as appropriate. If someone knows a better workaround than this, please let me know.

Javascript injection function:

  // Loads the given JS class from assets
  Future<String> loadJS(String name) async {
    var givenJS = rootBundle.loadString('path/to/declared/assets/$name.js');
    return givenJS.then((String js) {
      flutterWebViewPlugin.onStateChanged.listen((viewState) async {
        if (viewState.type == WebViewState.finishLoad) {
          flutterWebViewPlugin.evalJavascript(js);
        }
      });
    });
  }

This would evaluate your javascript located at $name.js - allowing you to use Javascript to create your button.

@charafau
Copy link
Collaborator

@muffinjello thanks again. adding button with js and listeing to address changes is probably the only way of doing this. closing

@codlin
Copy link
Author

codlin commented May 31, 2019

@muffinjello @charafau Great! thank you very much.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants