Coder Social home page Coder Social logo

Comments (13)

jnschulze avatar jnschulze commented on August 15, 2024 1

On Windows, you could use something like

import 'dart:io';
import 'package:path/path.dart' as p;

String getAssetFileUrl(String asset) {
  final assetsDirectory = p.join(
      p.dirname(Platform.resolvedExecutable), 'data', 'flutter_assets', asset);

  return Uri.file(assetsDirectory).toString();
}

from flutter-webview-windows.

jnschulze avatar jnschulze commented on August 15, 2024 1
String getAssetFileUrl(String asset) {

What would be the value of string asset in parameter,

controller.loadUrl(getAssetFileUrl('assets/test.html'));

from flutter-webview-windows.

CodingWithTashi avatar CodingWithTashi commented on August 15, 2024 1

@yisar I don't think there is eample project on this, let me know if you need. I will post example to this
Updated: Here is the medium link

from flutter-webview-windows.

jnschulze avatar jnschulze commented on August 15, 2024
  1. though I am able to load html content, Js file is't working using (loadStringContent function),

Do you mean scripts that are loaded from the HTML (string) document or a directly run script using executeScript?

It might be an issue with the URLs of the resources you want to load. You’re using relative URLs, right?
I don’t know what they’re relative to. Might be the current working directory.

If the document is placed in a file anyway, why don’t you just load it via loadUrl('file://...')?

  1. Also communication between dart and html js is't working and vice versa.

If the feature is implemented please provide example for each cases, if not I hope this feature will come soon

On the dart side, messages can be posted to the webview with postWebMessage and received by listening to the webMessage stream.

On the web side, you need to use window.chrome.webview.addEventListener('message', handler); (see https://docs.microsoft.com/en-us/microsoft-edge/webview2/reference/win32/icorewebview2?view=webview2-1.0.864.35#postwebmessageasjson and https://docs.microsoft.com/en-us/microsoft-edge/webview2/reference/win32/icorewebview2?view=webview2-1.0.864.35#add_webmessagereceived)

from flutter-webview-windows.

CodingWithTashi avatar CodingWithTashi commented on August 15, 2024

Thank you @jnschulze for the respond, let me elaborate my approach:

  1. I have test.html which has few html content and link to js and css, eg <script src="test.js"></script>, though I am able to load the html I am not able to trigger .js function.
    So I meant script from html, not executeScript and all document are placed inside app assets folder.

  2. For communication,
    a. Say I have button in flutter app bar when I click on app bar button it should call function defined in js file and display alert as such
    b. When I click html button in webview, it should able to call dart function and display toast or snackbar or something
    PS. I was able to do in webview for mobile and web and now trying on window desktop

from flutter-webview-windows.

jnschulze avatar jnschulze commented on August 15, 2024
  1. I have test.html which has few html content and link to js and css, eg <script src="test.js"></script>, though I am able to load the html I am not able to trigger .js function.
    So I meant script from html, not executeScript and all document are placed inside app assets folder.

Okay, I’d just use loadUrl as opposed to loadStringContent then.

from flutter-webview-windows.

CodingWithTashi avatar CodingWithTashi commented on August 15, 2024

Okay, I’d just use loadUrl as opposed to loadStringContent then.

what would be the url then I tried "file://assets/test.html" and didt work

from flutter-webview-windows.

CodingWithTashi avatar CodingWithTashi commented on August 15, 2024
String getAssetFileUrl(String asset) {

What would be the value of string asset in parameter,
I think it would be nice if you include this in example project in your repo too for loading html content in webview, for now I can see only url in your code.

from flutter-webview-windows.

CodingWithTashi avatar CodingWithTashi commented on August 15, 2024

@jnschulze thanks, it worked. Loading html,js content from assets.You can close this issue, But I was still wondering, How can we communicate between webview to dart and vice versa as mentioned above. Is it possible with current version or can you do that in future.

from flutter-webview-windows.

jnschulze avatar jnschulze commented on August 15, 2024

It's possible by posting a message to the current document as described above.

from flutter-webview-windows.

yisar avatar yisar commented on August 15, 2024

@jnschulze

On the dart side, messages can be posted to the webview with postWebMessage and received by listening to the webMessage stream.

Is there a complete demo here?

from flutter-webview-windows.

jnschulze avatar jnschulze commented on August 15, 2024

@jnschulze

On the dart side, messages can be posted to the webview with postWebMessage and received by listening to the webMessage stream.

Is there a complete demo here?

Unfortunately not, but using it should be straightforward and won’t take more than five minutes to wire up.

from flutter-webview-windows.

Xiangjinxiu avatar Xiangjinxiu commented on August 15, 2024

image
I use loadUrl(getAssetFileUrl('assets/test.html')) it work the image show
but when use loadStringContent(html) the image doesn,t show
I want to use loadStringContent how to fix this problem

from flutter-webview-windows.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.