搜索插件、代码仓与 APP 分类

插件详情v0.2.3

webviewx

功能丰富的跨平台 WebView,移动端使用 webview_flutter,Web 端使用 iframe。支持 JS 互操作。

AndroidiOSWeb
近 30 天下载
791
喜欢数
266
Pub 分数
140

安装插件

flutter pub add webviewx

插件介绍

基于插件公开信息、社区数据与项目使用情况整理的选型参考。

webviewx 是一个跨平台的 WebView 包,它在 Android 和 iOS 上利用 webview_flutter,在 Web 上使用 iframe,提供跨平台一致的 API。它支持 JavaScript 互操作,可实现 Dart 与网页内容之间的无缝通信。专为希望在 Flutter 应用中嵌入网页内容并具备丰富功能和平台兼容性的开发者设计。

适合场景

  • 在移动应用中嵌入网页内容
  • 集成基于Web的表单或仪表板
  • 通过 Web 组件实现混合应用开发
  • 支持从URL动态加载内容
  • 在Flutter应用中构建嵌入式Web功能

主要特点

  • 跨平台支持(Android、iOS、Web)
  • JavaScript 互操作功能
  • 在移动设备上使用原生 WebView,在 Web 上使用 iframe
  • 跨平台一致的 API
  • 轻量级且易于集成

适合对象

  • 开发混合 Flutter 应用的开发者
  • 需要嵌入网页内容的团队
  • 需要与网页视图进行 JS 通信的项目
  • 针对多个平台且共享UI逻辑的应用
  • 快速原型化集成网页的功能

使用建议

  • Web 平台使用 iframe,这可能会限制一些高级 WebView 功能
  • 加载外部内容时需要谨慎处理安全问题
  • 与原生 WebView 实现相比,自定义选项有限
  • 可能需要额外配置以处理复杂的网页交互
  • 依赖 webview_flutter 用于移动设备,该依赖项具有自身的限制

原始 README

以下为关联源码仓库的 README 原文快照,最新内容请访问源码仓库。

This package has been deprecated

webviewx

pub package

A feature-rich cross-platform webview using webview_flutter for mobile and iframe for web. JS interop-ready.

Getting started


Gallery

Mobile

Web


Basic usage

1. Create a WebViewXController inside your stateful widget
late WebViewXController webviewController;
2. Add the WebViewX widget inside the build method, and set the onWebViewCreated callback in order to retrieve the controller when the webview is initialized
WebViewX(
    initialContent: '<h2> Hello, world! </h2>',
    initialSourceType: SourceType.HTML,
    onWebViewCreated: (controller) => webviewController = controller,
    ...
    ... other options
);

Important !

If you need to add other widgets on top of the webview (e.g. inside a Stack widget), you MUST wrap those widgets with a WebViewAware widget. This does nothing on mobile, but on web it allows widgets on top to intercept gestures. Otherwise, those widgets may not be clickable and/or the iframe will behave weird (unexpected refresh/reload - this is a well known issue).

Also, if you add widgets on top of the webview, wrap them and then you notice that the iframe still reloads unexpectedly, you should check if there are other widgets that sit on top without being noticed, or try to wrap InkWell, GestureRecognizer or Button widgets to see which one causes the problem.

3. Interact with the controller (run the example app to check out some use cases)
webviewController.loadContent(
    'https://flutter.dev',
    SourceType.url,
);
webviewController.goBack();

webviewController.goForward();
...
...

Features

Note: For more detailed information about things such as EmbeddedJsContent, please visit each own's .dart file from the utils folder.

  • Widget properties

Feature Details
String initialContent Initial webview content
SourceType initialSourceType Initial webview content type (url, urlBypass or html)
String? userAgent User agent
double width Widget's width
double height Widget's height
Function(WebViewXController controller)? onWebViewCreated Callback that gets executed when the webview has initialized
Set<EmbeddedJsContent> jsContent A set of EmbeddedJsContent, which is an object that defines some javascript which will be embedded in the page, once loaded (check the example app)
Set<DartCallback> dartCallBacks A set of DartCallback, which is an object that defines a dart callback function, which will be called from javascript (check the example app)
bool ignoreAllGestures Boolean value that specifies if the widget should ignore all gestures right after it is initialized
JavascriptMode javascriptMode This specifies if Javascript should be allowed to execute, or not (allowed by default, you must allow it in order to use above features)
AutoMediaPlaybackPolicy initialMediaPlaybackPolicy This specifies if media content should be allowed to autoplay when initialized (i.e when the page is loaded)
void Function(String src)? onPageStarted Callback that gets executed when a page starts loading (e.g. after you change the content)
void Function(String src)? onPageFinished Callback that gets executed when a page finishes loading
NavigationDelegate? navigationDelegate Callback that, if not null, gets executed when the user clicks something in the webview (on Web it only works for SourceType.urlBypass, for now)
void Function(WebResourceError error)? onWebResourceError Callback that gets executed when there is an error when loading resources ( issues on web )
WebSpecificParams webSpecificParams This is an object that contains web-specific options. Theese are not available on mobile (yet)
MobileSpecificParams mobileSpecificParams This is an object that contains mobile-specific options. Theese are not available on web (yet)

  • Controller properties

Feature Usage
Load URL that allows iframe embedding webviewController.loadContent(URL, SourceType.URL)
Load URL that doesnt allow iframe embedding webviewController.loadContent(URL, SourceType.URL_BYPASS)
Load URL that doesnt allow iframe embedding, with headers webviewController.loadContent(URL, SourceType.URL_BYPASS, headers: {'x-something': 'value'})
Load HTML from string webviewController.loadContent(HTML, SourceType.HTML)
Load HTML from assets webviewController.loadContent(HTML, SourceType.HTML, fromAssets: true)
Check if you can go back in history webviewController.canGoBack()
Go back in history webviewController.goBack()
Check if you can go forward in history webviewController.canGoForward()
Go forward in history webviewController.goForward()
Reload current content webviewController.reload()
Check if all gestures are ignored webviewController.ignoringAllGestures
Set ignore all gestures webviewController.setIgnoreAllGestures(value)
Evaluate "raw" javascript code webviewController.evalRawJavascript(JS)
Evaluate "raw" javascript code in global context ("page") webviewController.evalRawJavascript(JS, inGlobalContext: true)
Call a JS method webviewController.callJsMethod(METHOD_NAME, PARAMS_LIST)
Retrieve webview's content webviewController.getContent()
Get scroll position on X axis webviewController.getScrollX()
Get scroll position on Y axis webviewController.getScrollY()
Scrolls by x on X axis and by y on Y axis webviewController.scrollBy(int x, int y)
Scrolls exactly to the position (x, y) webviewController.scrollTo(int x, int y)
Retrieves the inner page title webviewController.getTitle()
Clears cache webviewController.clearCache()

Limitations and notes

While this package aims to put together the best of both worlds, there are differences between web and mobile.

  • Running and building

    First, this package was being developed while the default web renderer was html. Now(Flutter 2, Dart 2.12), the default renderer is canvaskit.

    From my experience, this package does behave a little bit weird on canvaskit, so you should use the html renderer.

    To do this, you have to run your ordinary flutter run -d chrome command with the --web-renderer html extra argument, like this:

    flutter run -d chrome --web-renderer html
    

    for running and

    flutter build web --web-renderer html
    

    for building.

  • Diferences between Web and Mobile behaviour:

    See issues/#27

  • About content loading on Web

    To make the web version (iframe) work as it is, I had to use some of the code from x-frame bypass in order to make a request to a CORS proxy, which removes the headers that block iframe embeddings.

    This might seem like a hack, and it really is, but I couldn't find any other way to make the iframe behave similar to the mobile webview (which is some kind of an actual browser, that's why everything works there by default).

  • About Web navigation

    On web, the history navigation stack is built from scratch because I couldn't handle iframe's internal history the right way.


Known issues and TODOs

  • [ x ] On web, user-agent and headers only work when using SourceType.urlBypass, and they only have effect the first time being used (view/web.dart)

  • [ x ] On web, it should be possible to send any errors caught when loading an urlBypass to a dart callback, which will then be sent through the onWebResourceError callback, just like on the mobile version (utils/x_frame_options_bypass.dart)

  • [ x ] On web, it should be possible to add a custom proxy list without the js null-checking mess (utils/x_frame_options_bypass.dart)

  • [ ? ] Eventually (if possible), most if not all properties from WebSpecificParams and MobileSpecificParams should merge and theese two objects may disappear

  • [ x ] On mobile, the controller's value's source type becomes out of sync when moving back and forth in history. This happens because the url change is not yet intercepted and set the model accordingly (shouldn't be hard to fix).

  • [ ] On mobile, the controller's callJsMethod doesnt throw an error if the operation failed. Instead it only shows the error in console.

  • [ ] Add tests

  • List open, there may be others

Credits

This package wouldn't be possible without the following:

  • webview_flutter for the mobile version
  • easy_web_view for ideas and starting point for the web version
  • pointer_interceptor for fixing iframe issues when other widgets are on top of it (see above)
  • x-frame-bypass for allowing the iframe to bypass websites' X-Frame-Options: deny/same-origin headers, thus allowing us to load any webpage (just like on mobile)
  • https://cors.bridged.cc/ for the free CORS proxy
  • https://api.codetabs.com/ for the free CORS proxy
  • And last but not least, http://deversoft.ro (the company I work for) for motivating me throughout the development process

License

MIT

所属分类

依赖

依赖清单 5 项

源码仓库

使用此插件的项目

暂无相关数据。