溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

Flutter中如何加載并預(yù)覽本地的html文件的方法

發(fā)布時(shí)間:2020-09-07 06:18:55 來(lái)源:腳本之家 閱讀:295 作者:任重道遠(yuǎn) 欄目:移動(dòng)開(kāi)發(fā)

直接進(jìn)入主題,大概步驟如下

在 assets 創(chuàng)建需要訪問(wèn) html 文件,如下

Flutter中如何加載并預(yù)覽本地的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ù)覽的效果如下

Flutter中如何加載并預(yù)覽本地的html文件的方法

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持億速云。

向AI問(wèn)一下細(xì)節(jié)

免責(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)容。

AI