- html文件放置
首先,將.html文件拖進(jìn)工程,我是放在最外級目錄,和pubspec.yaml同級;
然后,打開pubspec.yaml,在 assets: 下添加該文件,如:
assets:
- membership_agreement.html
- 加載本地html文件
import 'package:flutter/services.dart' show rootBundle;
//讀取文件
Future<String> _getFile() async {
//此html即為文件名'membership_agreement.html'
return await rootBundle.loadString(widget.html);
}
//讀取html數(shù)據(jù)
body: FutureBuilder<String>(
future: _getFile(),
builder: (context, snapshot) {
if (snapshot.hasData) {
//注意:數(shù)據(jù)處理這里,一定要設(shè)置encoding不然讀出來就是一堆火星文
final String htmlUrl = Uri.dataFromString(snapshot.data, mimeType: 'text/html', encoding: Encoding.getByName('utf-8'), base64: true).toString();
return _buildWeb(htmlUrl);
}
return EmptyView('讀取失敗');
})
const String kNavigationExamplePage = '''
<!DOCTYPE html><html>
<head><title>Navigation Delegate Example</title></head>
<body>
<p>
The navigation delegate is set to block navigation to the youtube website.
</p>
<ul>
<ul><a >https://www.youtube.com/</a></ul>
<ul><a >https://www.google.cn/</a></ul>
</ul>
</body>
</html>
''';
void _onNavigationDelegateExample(
WebViewController controller, BuildContext context) async {
//不能用這種方式處理snapshot.data,報錯:contains Invalid Characters,沒弄明白
final String contentBase64 =
base64Encode(const Utf8Encoder().convert(kNavigationExamplePage));
await controller.loadUrl('data:text/html;base64,$contentBase64');
}