显示文档中资产的图像

问题描述 投票:0回答:4

我正在编写有关变量的文档,并且我想包含项目内的图像。例如:

assets/
 |- icons/
 |   |- my-image.svg
lib/
 |- my_file.dart

我知道可以显示来自 URL 的图像,但是来自文件的图像怎么样?

那些是我不成功的尝试:

// lib/my_file.dart

/// The image attempt:
/// ![](assets/icons/my-image.svg)
/// ![](../assets/icons/my-image.svg)
const myVariable = 0;

但它不起作用:


有办法吗?

flutter dart visual-studio-code comments dartdoc
4个回答
1
投票

GitHub 上已经有一个问题:#2390 还有一个相关线程:在 dart 文档注释中使用本地图像资源

实际上,Web url 有效,但相对路径无效,因为 VSCode 不支持这一点。它尝试打开与其应用程序目录相关的文件。但我也无法在 Windows 上确认这一点,因为对我来说,甚至绝对路径都不起作用......

因此,如果您的项目位于公共存储库中,您可以只使用 Web url。


0
投票

1:打开pubspec.yaml并点击“Pub get”

2:

Image.asset("assets/icons/my-image.svg",width: 100,height: 100,)

0
投票

所以我认为你想让变量保存资产图像路径,所以这样做

  1. 转到 pubspecs.yaml 文件并添加此

    颤动: 资产: - 资产/图标/my-image.svg 然后运行

    flutter pub get

然后你可以这样使用图像

//let the variable hold the image path
const myVariable = "assets/icons/my-image.svg";


//to display on widget
Image.asset(myVariable);

0
投票

这可以通过使用外部url来实现

/// Asset derived from `assets/images/icon_inactive_account.svg`.
/// <img src="https://https://user-images.githubusercontent.com/Flutter/assets/images/icon_inactive_account.svg" alt="icon_inactive_account" width="30" height="30" />
///
String get icon_inactive_account => 'assets/images/icon_inactive_account.svg';

或像这样的内部绝对路径

/// Asset derived from `assets/images/icon_inactive_account.svg`.
/// <img src="/Users/Computer/Documents/Project/Flutter/assets/images/icon_inactive_account.svg" alt="icon_inactive_account" width="30" height="30" />
///
String get icon_inactive_account => 'assets/images/icon_inactive_account.svg';
© www.soinside.com 2019 - 2024. All rights reserved.