简体   繁体   English

如何在 flutter Package/Plugin 开发中添加资产?

[英]How to add assets in flutter Package/Plugin development?

I am developing a flutter package containing some assets files.我正在开发一个包含一些资产文件的颤振包。 I mentioned required assets in pubsepc.yaml as usual like this我像往常一样在 pubsepc.yaml 中提到了所需的资产

  assets:
    - assets/abc.xyz

and uploaded the package to https://pub.dartlang.org/ .并将包上传到https://pub.dartlang.org/

After that I created a flutter Application and imported my developed package in pubspec.yaml like...之后,我创建了一个 flutter 应用程序并将我开发的包导入到pubspec.yaml例如...

dependencies:
  flutter:
    sdk: flutter
  my_developed_package: ^0.0.1

Now everything is working fine except my assets are absent.现在一切正常,除了我的资产不存在。 I put some assets in my Application without mentioning in pubsepc.yaml and its working.我在我的应用程序中放入了一些资产,而没有在pubsepc.yaml提及它及其工作。 I am unable to understand, how do I add these assets to my package so that they load automatically?我无法理解,如何将这些资产添加到我的包中以便它们自动加载?

Quote from报价

If the desired asset is specified in the pubspec.yaml file of the package, it is bundled automatically with the application.如果在包的 pubspec.yaml 文件中指定了所需的资产,它会自动与应用程序捆绑在一起。 In particular, assets used by the package itself must be specified in its pubspec.yaml.特别是,包本身使用的资产必须在其 pubspec.yaml 中指定。

In Flutter you can use assets from packages, it should not be a problem.在 Flutter 中,您可以使用包中的资产,这应该不是问题。 Only thing is, you need to specify your package and import it.唯一的问题是,您需要指定您的包并导入它。 Eg If it's an image, you can use AssetImage class and it's package attribute.例如,如果它是一个图像,你可以使用AssetImage类和它的package属性。

AssetImage('assets/abc.xyz', package: 'my_developed_package');

For more information about how you can call texts and other stuff, please check here .有关如何拨打短信和其他内容的更多信息,请查看此处

Sorry for the late answer but the following approach helped me for including assets (not only images but any type of file) in plugin development.抱歉回答晚了,但以下方法帮助我在插件开发中包含资产(不仅是图像,还包括任何类型的文件)。

I put my assets under the lib folder like, my_plugin/lib/assets and in pubspec.yaml like this.我把我的资产放在lib文件夹下,比如my_plugin/lib/assetspubspec.yaml像这样。

  assets:
    - packages/my_plugin/assets/asset_name
       
  # Be careful about indentation

It has been added with the plugin and then I accessed them with path like this packages/my_plugin/assets/asset_name , eg它已与插件一起添加,然后我使用这样的路径来访问它们packages/my_plugin/assets/asset_name ,例如

File myAsset = File("packages/my_plugin/assets/asset_name");

By this approach, I was able to get any asset from Plugin not only Images.通过这种方法,我能够从插件中获取任何资产,而不仅仅是图像。

For a complete Example, you can check my plugin here .对于完整的示例,您可以在此处查看我的插件。

To load an image from a package dependency, the package argument must be provided to AssetImage .要从package依赖项加载图像,必须向AssetImage提供包参数。

For instance, suppose your application depends on a package called my_icons , which has the following directory structure:例如,假设您的应用程序依赖于名为my_icons的包,该包具有以下目录结构:

.../pubspec.yaml
.../icons/heart.png
.../icons/1.5x/heart.png
.../icons/2.0x/heart.png
...etc.

To load the image, use:要加载图像,请使用:

AssetImage('icons/heart.png', package: 'my_icons')

Assets used by the package itself should also be fetched using the package argument as above.包本身使用的资产也应该使用上面的package参数来获取。

Pubspec yaml is indentation sensitive Pubspec yaml 对缩进敏感

there is a difference between有区别

 assets:
  - packages/my_plugin/assets/asset_name

VS VS

 assets:
   - packages/my_plugin/assets/asset_name

If you closely notice at above two then you will find that pubspec.yaml is very sensitive如果你仔细观察上面两个,那么你会发现 pubspec.yaml 非常敏感

always write总是写

assets:
  -asset/yourasset/example1

there must be two spaces from the beginning of line.行首必须有两个空格。

After adding asset link to pubspec you have to run packages get It will show exits with 0 error if you place your assets properly otherwise it will show you reason behind the error.将资产链接添加到 pubspec 后,您必须运行包 get如果您正确放置资产,它将显示 0 错误退出,否则它将显示错误背后的原因。

Generally, Beginners face these type of problems.一般来说,初学者面临这些类型的问题。 With time you will crack the way to solve this error随着时间的推移,您将破解解决此错误的方法

Adding Assets to a Flutter Package can get Difficult!将资产添加到 Flutter 包会变得很困难!
Just follow these steps and you're good to go...只需按照以下步骤操作,您就可以开始了...

Step 1: Make an assets folder in the root directory of the Package第一步:在Package根目录下创建assets文件夹

Step 2: Add it to your pubspec.yaml file第 2 步:将其添加到您的pubspec.yaml文件中

flutter:
  assets:
    - assets/asset_name

# Mind the indentation

Step 3: Using the asset , for an image file第 3 步:使用资产,用于图像文件

AssetImage('assets/asset_name.png', package: 'your_package_name')

Run Pub.dev and do a Cold Start for the Flutter Application using the Package and运行Pub.dev并使用 Package 对 Flutter 应用程序进行冷启动

Voila You're Done.瞧,你完成了。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM