WebView in Flutter Web

五迷三道 提交于 2020-01-05 08:34:47

问题


I am trying to display a web view in Flutter for Web but I got the following error :

PlatformException(Unregistered factory, No factory registered for viewtype 'plugins.flutter.io/webview', null)

Is there a way to display a WebView in Flutter Web ?


回答1:


You need at first perform platformViewRegistry:

  ui.platformViewRegistry.registerViewFactory(
  'hello-world-html',
  (int viewId) => IFrameElement()
    ..width = '640'
    ..height = '360'
    ..src = 'https://www.youtube.com/embed/IyFZznAk69U'
    ..style.border = 'none');

Look at that example. In that example old library was imported (on 29.09.19), but if you change 'flutter_web' on 'flutter' it have to work.

Also, you can use not only 'IFrameElement', it can be regular html:

    ui.platformViewRegistry.registerViewFactory("simple_div", (int viewId) {
  DivElement element = DivElement();
  ...
  return element;



回答2:


you can use this package: https://pub.dev/packages/flutter_webview_plugin

and here is an working example you can use:

import 'package:flutter/material.dart';
import 'package:flutter_webview_plugin/flutter_webview_plugin.dart';

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    var title = 'some title';
    return MaterialApp(
        title: title,
        home: WebviewScaffold(
          url: "yourwebsite.com",
          withZoom: false,
          withLocalStorage: true,
        ));
  }
}



回答3:


Answer by @mohamed-salah is helpful, however, I was only getting a loading symbol on my screen. We need put webview inside WillPopScope widget. Use the following code to properly load webview -

In pubspec.yaml add dependency

flutter_webview_plugin: ^0.3.9+1 // replace with latest version

In StatefulWidget class, use following code -

class _WebViewLoadingState extends State<Details> {
  final _webViewPlugin = FlutterWebviewPlugin();

  @override
  void initState() {
    // TODO: implement initState
    super.initState();
    // on pressing back button, exiting the screen instead of showing loading symbol
    _webViewPlugin.onDestroy.listen((_) {
      if (Navigator.canPop(context)) {
        // exiting the screen
        Navigator.of(context).pop();
      }
    });
  }

  @override
  Widget build(BuildContext context) {
    // WillPopScope will prevent loading
    return WillPopScope(
      child: WebviewScaffold(
        url: "https://www.google.com",
        withZoom: false,
        withLocalStorage: true,
        withJavascript: true,
        appCacheEnabled: true,
        appBar: AppBar(
          title: Text("Browser"),
        ),
      ),
      onWillPop: () {
        return _webViewPlugin.close();
      }
    );
  }
}


来源:https://stackoverflow.com/questions/58150503/webview-in-flutter-web

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!