您好,登錄后才能下訂單哦!
本篇文章給大家分享的是有關(guān)Flutter中怎么加載并預(yù)覽本地的html文件,小編覺(jué)得挺實(shí)用的,因此分享給大家學(xué)習(xí),希望大家閱讀完這篇文章后可以有所收獲,話不多說(shuō),跟著小編一起來(lái)看看吧。
在 assets 創(chuàng)建需要訪問(wèn) html 文件,如下
這里創(chuàng)建一個(gè)files文件夾,專門(mén)來(lái)放這些靜態(tài) html 文件.
在 pubspec.yaml 中配置訪問(wèn)位置
assets: - assets/images/ - assets/files/
在 pubspec.yaml 添加 webview_flutter 插件依賴
webview_flutter: ^0.3.15+1 // 具體版本請(qǐng)查看官網(wǎng)
進(jìn)入實(shí)際的代碼操作
import 'dart:convert';import 'package:flutter/material.dart';import 'package:flutter/services.dart';import 'package:webview_flutter/webview_flutter.dart';class AgreementPage extends StatefulWidget { @override _AgreementPageState createState() => _AgreementPageState();}class _AgreementPageState extends State<AgreementPage> { WebViewController _webViewController; String filePath = 'assets/files/agreement.html'; @override Widget build(BuildContext context) { return Scaffold( appBar: AppBar(title: Text('享你來(lái)服務(wù)條款')), body: WebView( initialUrl: '', javascriptMode: JavascriptMode.unrestricted, onWebViewCreated: (WebViewController webViewController) { _webViewController = webViewController; _loadHtmlFromAssets(); }, ) ); } _loadHtmlFromAssets() async { String fileHtmlContents = await rootBundle.loadString(filePath); _webViewController.loadUrl(Uri.dataFromString(fileHtmlContents, mimeType: 'text/html', encoding: Encoding.getByName('utf-8')) .toString()); }}
以上就是Flutter中怎么加載并預(yù)覽本地的html文件,小編相信有部分知識(shí)點(diǎn)可能是我們?nèi)粘9ぷ鲿?huì)見(jiàn)到或用到的。希望你能通過(guò)這篇文章學(xué)到更多知識(shí)。更多詳情敬請(qǐng)關(guān)注億速云行業(yè)資訊頻道。
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。