Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

  assets:
    - assets/abc.xyz

and uploaded the package to https://pub.dartlang.org/.

After that I created a flutter Application and imported my developed package in pubspec.yaml like...

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. I am unable to understand, how do I add these assets to my package so that they load automatically?

like image 578
Shahzad Akram Avatar asked Feb 18 '19 05:02

Shahzad Akram


People also ask

How do you add assets to flutter?

Step 1: At the root of your project, create a new folder called assets . Step 2: Inside the root folder, create another folder called images . You can give any name to this folder such as pictures, graphics, etc. Step 3: Add your images inside the assets/images folder.

How do I add modules in flutter?

Using the File > New > New Module… menu in Android Studio in your existing Android project, you can either create a new Flutter module to integrate, or select an existing Flutter module that was created previously. If you create a new module, you can use a wizard to select the module name, location, and so on.

How to use assets from packages in flutter?

In Flutter you can use assets from packages, it should not be a problem. Only thing is, you need to specify your package and import it. E.g. If it's an image, you can use AssetImage class and it's package attribute. For more information about how you can call texts and other stuff, please check here.

How do I create a plugin package in flutter?

To create a plugin package, use the --template=plugin flag with flutter create. Use the --platforms= option followed by a comma-separated list to specify the platforms that the plugin supports. Available platforms are: android, ios, web, linux, macos, and windows . If no platforms are specified, the resulting project doesn’t support any platforms.

Why are my assets not getting added to the package’s assets bundle?

Any assets that are not in the lib/ folder, though, will not get added to the package’s assets bundle unless you specifically include them in pubspec.yaml: It is easiest to understand how this works by looking at some examples. I’ll show you four different Flutter packages on Pub.dev that each do it a little bit differently.

How to create a custom switch in flutter?

In this dialogue, select Flutter package option as shown in the below images Now, enter all the details of your package such as package name, project location, and package description. In this blog, we’re gonna create a custom switch, so give the package name as “custom_switch”


Video Answer


1 Answers

To load an image from a package dependency, the package argument must be provided to AssetImage.

For instance, suppose your application depends on a package called my_icons, which has the following directory structure:

.../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.

like image 72
Paresh Mangukiya Avatar answered Sep 21 '22 08:09

Paresh Mangukiya