html - 如何将资产文件夹中的图像添加到本地 HTML 文件并在 Flutter WebView 中显示 HTML 文件
问题描述
我已经在资产文件夹中添加了一个图像,但是当在img
带有“image.png”的html文件中的标签中使用它时,因为src
flutter中的Webview没有显示它。我也尝试将其转换为 base64,这解决了问题,但我想要资产文件夹中的图像。
解决方案
您可以复制粘贴运行完整代码和index.html
下面
您可以使用包https://pub.dev/packages/flutter_inappwebview
在工作演示中,您可以看到颤振图标<img src="images/flutter-logo.svg" alt="flutter logo">
正确显示
第 1 步:添加android:usesCleartextTraffic="true"
第AndroidManifest.xml
2 步:pubspec.yaml
设置
assets:
- assets/images/
- assets/
第 3 步:将文件和图像添加到assets
文件夹
工作演示
完整代码
import 'package:flutter/material.dart';
import 'package:flutter_inappwebview/flutter_inappwebview.dart';
InAppLocalhostServer localhostServer = new InAppLocalhostServer();
Future main() async {
WidgetsFlutterBinding.ensureInitialized();
await localhostServer.start();
runApp(new MyApp());
}
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
appBar: AppBar(
title: const Text('InAppWebView Example'),
),
body: Container(
child: Column(children: <Widget>[
Expanded(
child: Container(
child: InAppWebView(
initialUrl: "http://localhost:8080/assets/index.html",
initialHeaders: {},
initialOptions: InAppWebViewGroupOptions(),
onWebViewCreated: (InAppWebViewController controller) {},
onLoadStart: (InAppWebViewController controller, String url) {},
onLoadStop: (InAppWebViewController controller, String url) {},
),
),
)
])),
),
);
}
}
索引.html
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Flutter InAppWebView</title>
<link rel="stylesheet" href="https://getbootstrap.com/docs/4.3/dist/css/bootstrap.min.css">
<link rel="stylesheet" href="css/style.css">
<script src="https://code.jquery.com/jquery-3.3.1.min.js"></script>
<link rel="shortcut icon" href="favicon.ico">
</head>
<body class="text-center">
<div class="cover-container d-flex w-100 h-100 p-3 mx-auto flex-column">
<header class="masthead mb-auto">
<div class="inner">
<h3 class="masthead-brand">Flutter InAppWebView</h3>
<nav class="nav nav-masthead justify-content-center">
<a class="nav-link active" href="index.html">Home</a>
<a class="nav-link" href="page-1.html">Page 1</a>
<a class="nav-link" href="page-2.html">Page 2</a>
</nav>
</div>
</header>
<main role="main" class="inner cover">
<h1 class="cover-heading">Inline WebView</h1>
<img src="images/flutter-logo.svg" alt="flutter logo">
<a href="index.html"><img src="images/flutter-logo.svg" alt="flutter logo"></a>
<p class="lead">Cover is a one-page template for building simple and beautiful home pages. Download, edit the text, and add your own fullscreen background photo to make it your own.</p>
</main>
<footer class="mastfoot mt-auto">
<div class="inner">
<p>Cover template for <a target="_blank" href="https://getbootstrap.com/">Bootstrap</a>, by <a href="https://twitter.com/mdo">@mdo</a>.</p>
<p>Phone link example <a href="tel:1-408-555-5555">1-408-555-5555</a></p>
<p>Email link example <a href="mailto:example@gmail.com">example@gmail.com</a></p>
</div>
</footer>
</div>
<script src="js/main.js"></script>
</body>
</html>
推荐阅读
- php - 在 PHP 中重定向 - 显示 txt 文件内容
- angular - 如何从 addUserInfoFormGroup 获取“name”表单控件的“valueChanges”事件
- python - 如果值是整数,如何将浮点数格式化为整数。也就是说,将 13.0 显示为 13 而将 13.5 显示为 13.5
- azure-pipelines - 构建 id 的测试结果
- c# - 在 Web api 控制器中读取的 SQL Server 存储过程数据
- python - Pandas 过滤多个条件
- java - 仅匹配正则表达式中第一次出现的字符
- java - 减轻多态性中的参数,因为孩子需要不同的参数
- java - 连接到 Azurite 时验证失败
- mysql - 当 IFNULL() 与 IS NULL 的性能不同时如何调整我的 MYSQL 数据库