The article origin
In my last post, I introduced a weather prediction app written with Flutter. One of the features that needs to be improved is to display weather information based on the current location of the city. Because there was no way to use GMS (Google Mobile Service), the Positioning package based on Google Map provided by Flutter official could not be directly introduced into the project. Therefore, I came up with the idea of making a similar plug-in based on Gaudamap SDK. It can be used for the weather app, or it can be used directly for other projects in the future.
Another is to satisfy the small desire to create a flutter package.
The plugin is introduced
In Flutter, a plug-in is called a package. The purpose of using Packages is to achieve modularity, creating code that can be reused and shared. This is the same concept as modules and packages in most programming languages. The created package can be directly relied upon in pubspec.yaml.
A minimal package contains two parts:
-
A pubspec.yaml file: a metadata file that declares the name, version, author, and so on of the declared package.
-
A lib folder: contains the public code for the package. The folder must contain at least the
.dart file.
Note: the
.dart file must exist because it is convenient for people to quickly import the package to use it, which can be understood as a rule that must be followed.
The kinds of package
Packages can be divided into two types: those with pure DART code and those with platform-specific code.
-
Dart Packages: This is a Dart code only package that contains the specific functionality of a flutter, so it depends on the flutter framework and can only be used with a flutter.
-
Plugin Packages: This is an API that contains both dart code authoring and platform (Android/IOS) implementation-specific packages that can be called by Android and IOS.
The above should be easy to understand and can be understood as the difference between the Java JAR package and the Android SDK. The positioning package to be developed is the second.
Develop plugin Pakcage practices
The official recommendation is to create a package using the command line.
The first step is to create a Dart package, which we won’t cover here.
$ flutter create –template=package hello
Create the plugin package
We create a plugin package using the following command. Template selects plugin:
$ flutter create –org com.kinsomy –template=plugin amap_location_plugin
By default, plugin projects are created in Objective-C (ios) and Java (Android), and if you need to add swift and Kotlin support, you can add -i and -a to the command.
$ flutter create –org com.kinsomy –template=plugin amap_location_plugin -i swift -a kotlin hello
Since I am developing with Android Studio, I can also create FLutter project directly with Android Studio: select Create FLutter Project and then Plugin Project. Select swift and Kotlin support on the creation screen.
The project structure
Once created, you see the structure of the entire Plugin project:
-
Lib /amap_location_plugin.dart This is the code part of the DART API in plugin package, which is the interface code for users to invoke in their own FLUTTER projects.
-
Amaplocationplugin.java this is the implementation of the Android part of the Plugin package and is developed in conjunction with the DART API above.
-
Ios/Here is the specific implementation of plugin Package for ios, similar to the implementation of Android above, to write specific code for ios platform.
-
Example/This is a code example to illustrate the use of the plugin package, in which the source code relies on the written plugin.
Code implementation
Details will be covered in the next article.
First paste the code store address github.com/KinsomyJS/l…
Currently, my plugin is only developed for Android platform. For the plugin project I need to write Java code and dart code at the same time, the Java code is written in the android/SRC/main/Java/com/green/kinsomy/application directory, main work is connected to the gold location of the SDK, Dart code is written in the lib directory. Again, you need to have at least the
Release the packages
Once you’ve written your plugin and tested it well, you can publish it for others to use, preferably in Example.
Yaml, readme. md, and Changelog. md files are mandatory. The author, version number, and name of pubspec.yaml are mandatory. Changelog. md Version iteration information recorded.
When all the work is done, run the command check to see if everything is complete.
$ flutter packages pub publish –dry-run
If everything works correctly, you can execute the publish command.
$ flutter packages pub publish
You can then search for the package name you published in the Pub to find it.
You can see a score sign on the Package home page, where you can learn how to improve your score by providing more detailed instructions, complete usage documentation, and so on.
Github project address: github.com/KinsomyJS/l…
Reference documentation
-
Developing Packages & Plugins
-
Flutter/plugins git repository
-
Dart packages