您好,登錄后才能下訂單哦!
直接進(jìn)入主題,大概步驟如下
在 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()); } }
最終預(yù)覽的效果如下
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持億速云。
免責(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)容。