2

I am trying to show a webpage in webview via URL. I have tried flutter_webview_plugin plugin but it was not working when I run the project on the browser.

Is there any another way to show the webpage in the flutter web application?

Overcode
  • 4,074
  • 1
  • 21
  • 24
Dhanaji Yadav
  • 1,202
  • 1
  • 14
  • 22

3 Answers3

4

flutter_webview_plugin is to embed web pages inside an app. In flutter web you should use HtmlElementView widget. Most demos out there use IFrameElement to embed a webpage. You can check this easy_web_view package for handling both mobile and web platform automatically. It internally uses HTMLElementView and WebView automatically depending on the case of the deployment.

some example is available here

Update for adding onLoad listener

IFrameElement iframeElement = IFrameElement()
      ..src = 'url'
      ..style.border = 'none'
      ..onLoad.listen((event) {
        // perform you logic here.
      });

    ui.platformViewRegistry.registerViewFactory(
      'webpage',
      (int viewId) => iframeElement,
    );

    return Directionality(
      textDirection: TextDirection.ltr,
      child: Center(
        child: SizedBox(
          width: double.infinity,
          height: double.infinity,
          child: HtmlElementView(viewType: 'webpage'),
        ),
      ),
    );
Abhilash Chandran
  • 6,803
  • 3
  • 32
  • 50
  • How can i get PageLoadComplete callback from this library. – Dhanaji Yadav Apr 29 '20 at 18:08
  • 1
    If you need that, then you should go with `HtmlElementView` widget and use the `onLoad` stream of the `IFrameElement` from `dart:html`. You can see the docs [here](https://api.dart.dev/stable/2.8.0/dart-html/Element/onLoad.html). This is an EventStream which you can listen and do the logic you need inside the callback. – Abhilash Chandran Apr 29 '20 at 18:21
  • thak u sir for help, I have completed that code using HtmlEventView but i dont undersand where to add callback for PageLoad complete event, ui.platformViewRegistry.registerViewFactory( 'webpage', (int viewId) => IFrameElement() ..src = url ..style.border = 'none'); return Directionality( textDirection: TextDirection.ltr, child: Center( child: SizedBox( width: double.infinity, height: double.infinity, child: HtmlElementView( viewType: 'webpage'), ), ), ); – Dhanaji Yadav Apr 29 '20 at 18:48
  • Error: PlatformException(Unregistered factory, No factory registered for viewtype 'map_element', null, null) this error occurred – gowthaman C Nov 26 '20 at 06:35
4

if someone facing issue loading mobile side then go with this, its works in Flutter Android, Ios, Web :-

EasyWebView(
        height: 400,
        width: 1000,
        isHtml: false, // Use Html syntax
        isMarkdown: false, // Use markdown syntax
        convertToWidgets: true,
        src: Uri.dataFromString('<html><body><iframe allow="camera *;microphone *" height="100%" width="100%"'
            ' frameborder="0" src="$url"></iframe></body></html>', mimeType: 'text/html').toString(),
      ),

you guys can use any webview to load any web page in your web/app in flutter using Url in html just change src = "$url" :)

hio
  • 915
  • 8
  • 26
0

Answer for 2023

The flutter_inappwebview package just added support for the web!

You can read the web setup instructions in the official Flutter InappWebView docs but to summarize:

1. Add script to

<head>
  <!-- ... -->
    <script type="application/javascript" src="/assets/packages/flutter_inappwebview/assets/web/web_support.js" defer></script>
  <!-- ... -->
</head>

2. Use the InAppWebView Widget

Scaffold(
    body: SafeArea(
      child: InAppWebView(
        initialUrlRequest: URLRequest(url:WebUri('https://flutter.dev/')),
       )
    )
);

Note that there are some restrictions on what can be done in a WebView an Flutter web but for simply displaying content, this should work.

Code on the Rocks
  • 11,488
  • 3
  • 53
  • 61