01.Flutter在web平臺使用iframe標(biāo)簽

主要是針對web平臺,在web平臺中嵌入iframe標(biāo)簽详炬,將其他網(wǎng)站嵌入在自己的頁面當(dāng)中盐类。

第一部分,只在web平臺運行

1.注冊iframe組件

platformViewRegistry.registerViewFactory("iframe-webview", (_) {
      ......
    });

2. 創(chuàng)建自己的iframe組件呛谜,并設(shè)置參數(shù)在跳,寬高和加載的url

platformViewRegistry.registerViewFactory("iframe-webview", (_) {
      // 2. 創(chuàng)建自己的iframe組件,并設(shè)置參數(shù)隐岛,寬高和加載的url等
      return IFrameElement()
        ..style.height = '100%'
        ..style.width = '100%'
        ..src = url
        ..style.border = 'none';
    });

3. 設(shè)置組件所占大小猫妙,并創(chuàng)建HtmlElementView

SizedBox(
        width: double.infinity,
        height: double.infinity,
        child: HtmlElementView(
          viewType: "iframe-webview",
          onPlatformViewCreated: (int id) {
            // 組件創(chuàng)建回調(diào)
          },
        ))

完整代碼

import 'dart:html';
import 'dart:ui_web';

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

class IframeWebDemo extends StatefulWidget {
  @override
  State<StatefulWidget> createState() => _IframeWebDemoState();
}

class _IframeWebDemoState extends State<IframeWebDemo> {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: const Text(
          "iframe測試",
          style: TextStyle(fontSize: 20, color: Colors.white),
        ),
      ),
      body: _buildIframeWidget("https://flutter.dev"),
    );
  }

  Widget _buildIframeWidget(String url) {
    // var platformViewRegistry = PlatformViewRegistry();
    //注冊
    platformViewRegistry.registerViewFactory("iframe-webview", (_) {
      // 2. 創(chuàng)建自己的iframe組件,并設(shè)置參數(shù)聚凹,寬高和加載的url等
      return IFrameElement()
        ..style.height = '100%'
        ..style.width = '100%'
        ..src = url
        ..style.border = 'none';
    });

    // 3. 設(shè)置組件所占大小割坠,并創(chuàng)建HtmlElementView
    return SizedBox(
        width: double.infinity,
        height: double.infinity,
        child: HtmlElementView(
          viewType: "iframe-webview",
          onPlatformViewCreated: (int id) {
            // 組件創(chuàng)建回調(diào)
          },
        ));
  }
}

第二部分,如果有Android或IOS時運行會報錯

注:Android或IOS平臺妒牙,需要使用其他平臺實現(xiàn)韭脊,此處不做說明:

webview_flutterhttps://pub.dev/packages/webview_flutter

flutter_inappwebviewhttps://pub.dev/packages/flutter_inappwebview

1. 創(chuàng)建需要適配的類,如下:

webview_platform.dart:

import 'package:flutter/foundation.dart';
import 'package:flutter/widgets.dart';
import '_webview_api.dart' if(dart.library.html) '_webview_html.dart' as platform;

/// 獲取html的iframe組件单旁,只適用于web沪羔,外部使用時需要判斷[kIsWeb]
Widget buildWebViewWidgetPlatform(String url, {Function(int id)? onPlatformViewCreated}){
  return platform.buildWebViewWidget(url, onPlatformViewCreated: onPlatformViewCreated);
}

_webview_api.dart:

import 'package:flutter/widgets.dart';

Widget buildWebViewWidget(String url, {Function(int id)? onPlatformViewCreated}){
  throw UnsupportedError('No implementation of the [buildWebViewWidget] api provided');
}

_webview_html.dart

import 'dart:html';
import 'dart:ui_web';

import 'package:flutter/widgets.dart';

Widget buildWebViewWidget(String url, {Function(int id)? onPlatformViewCreated}) {
  var platformViewRegistry = PlatformViewRegistry();
  //注冊
  platformViewRegistry.registerViewFactory("iframe-webview", (_) {
    return IFrameElement()
      ..style.height = '100%'
      ..style.width = '100%'
      ..src = url
      ..style.border = 'none';
  });

  return SizedBox(
      width: double.infinity,
      height: double.infinity,
      child: HtmlElementView(
        viewType: "iframe-webview",
        onPlatformViewCreated: (int id) {
          onPlatformViewCreated?.call(id);
        },
      ));
}

2. 在原來創(chuàng)建和注冊iframe的地方修改為新的調(diào)用方式

import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
import 'package:flutter_demo/platform/webview/webview_platform.dart';

class IframeWebDemo extends StatefulWidget {
  @override
  State<StatefulWidget> createState() => _IframeWebDemoState();
}

class _IframeWebDemoState extends State<IframeWebDemo> {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: const Text(
          "iframe測試",
          style: TextStyle(fontSize: 20, color: Colors.white),
        ),
      ),
      body: kIsWeb ? buildWebViewWidgetPlatform("https://flutter.dev") : Text("其他平臺使用InappWebView等框架實現(xiàn)"),
    );
  }
}

注:以上示例已上傳Gitee,請參考:https://gitee.com/ttxz/flutter/blob/master/lib/pages/iframe_web_demo.dart

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末象浑,一起剝皮案震驚了整個濱河市蔫饰,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌愉豺,老刑警劉巖篓吁,帶你破解...
    沈念sama閱讀 216,372評論 6 498
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場離奇詭異蚪拦,居然都是意外死亡杖剪,警方通過查閱死者的電腦和手機冻押,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 92,368評論 3 392
  • 文/潘曉璐 我一進店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來盛嘿,“玉大人洛巢,你說我怎么就攤上這事〈握祝” “怎么了稿茉?”我有些...
    開封第一講書人閱讀 162,415評論 0 353
  • 文/不壞的土叔 我叫張陵,是天一觀的道長芥炭。 經(jīng)常有香客問我漓库,道長,這世上最難降的妖魔是什么园蝠? 我笑而不...
    開封第一講書人閱讀 58,157評論 1 292
  • 正文 為了忘掉前任渺蒿,我火速辦了婚禮,結(jié)果婚禮上彪薛,老公的妹妹穿的比我還像新娘蘸嘶。我一直安慰自己,他們只是感情好陪汽,可當(dāng)我...
    茶點故事閱讀 67,171評論 6 388
  • 文/花漫 我一把揭開白布训唱。 她就那樣靜靜地躺著,像睡著了一般挚冤。 火紅的嫁衣襯著肌膚如雪况增。 梳的紋絲不亂的頭發(fā)上,一...
    開封第一講書人閱讀 51,125評論 1 297
  • 那天训挡,我揣著相機與錄音澳骤,去河邊找鬼。 笑死澜薄,一個胖子當(dāng)著我的面吹牛为肮,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播肤京,決...
    沈念sama閱讀 40,028評論 3 417
  • 文/蒼蘭香墨 我猛地睜開眼颊艳,長吁一口氣:“原來是場噩夢啊……” “哼!你這毒婦竟也來了忘分?” 一聲冷哼從身側(cè)響起棋枕,我...
    開封第一講書人閱讀 38,887評論 0 274
  • 序言:老撾萬榮一對情侶失蹤,失蹤者是張志新(化名)和其女友劉穎妒峦,沒想到半個月后重斑,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 45,310評論 1 310
  • 正文 獨居荒郊野嶺守林人離奇死亡肯骇,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 37,533評論 2 332
  • 正文 我和宋清朗相戀三年窥浪,在試婚紗的時候發(fā)現(xiàn)自己被綠了祖很。 大學(xué)時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點故事閱讀 39,690評論 1 348
  • 序言:一個原本活蹦亂跳的男人離奇死亡漾脂,死狀恐怖假颇,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情符相,我是刑警寧澤拆融,帶...
    沈念sama閱讀 35,411評論 5 343
  • 正文 年R本政府宣布蠢琳,位于F島的核電站啊终,受9級特大地震影響,放射性物質(zhì)發(fā)生泄漏傲须。R本人自食惡果不足惜蓝牲,卻給世界環(huán)境...
    茶點故事閱讀 41,004評論 3 325
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望泰讽。 院中可真熱鬧例衍,春花似錦、人聲如沸已卸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,659評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽累澡。三九已至梦抢,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間愧哟,已是汗流浹背奥吩。 一陣腳步聲響...
    開封第一講書人閱讀 32,812評論 1 268
  • 我被黑心中介騙來泰國打工, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留蕊梧,地道東北人霞赫。 一個月前我還...
    沈念sama閱讀 47,693評論 2 368
  • 正文 我出身青樓,卻偏偏與公主長得像肥矢,于是被迫代替她去往敵國和親端衰。 傳聞我的和親對象是個殘疾皇子,可洞房花燭夜當(dāng)晚...
    茶點故事閱讀 44,577評論 2 353

推薦閱讀更多精彩內(nèi)容