Read more about the Slide Container package in my previous blog post.
Publishing a Flutter package to the world
Sharing is caring, and good developers share the useful things they create right? Well, Dart Pub is here to help you share your work with the world!
First create a Dart package, either from your IDE as shown here or with this single command from a terminal:
flutter create --template=package package_name
Now open the file in the
lib folder and get coding! Don’t forget to add documentation with Dart Doc to your public facing API (check out this document for best practices).
In the case of the Slide Container, the public facing API is composed of the
SlideContainer class final parameters. The documentation will be automatically generated from the
/// The strength of the dampening effect when the container is moved. /// /// The bigger this value the slower the container will move toward the finger position. /// A Value of 1.0 means no damping is added. /// /// Needs to be superior or equal to 1.0. final double dampeningStrength;
After writing your package it is good practice to create an example app demoing what it does. Start by creating a new Flutter project in a folder called
example in your package directory (see the folder structure on our Github repo for reference).
pubspec.yaml file of your example project, add the reference to your package like this:
dependencies: flutter: sdk: flutter your_package_name: path: ../
If your folder structure is correct the path to your package from the example folder should just be
Now you can start writing the example app in the
lib/main.dart file. You can check here for a quick reminder on how to get started.
You are encouraged to add tests but if you don’t have anything worth testing just delete the test folder.
Before uploading, update the
pubspec.yaml of the package (not the example) to add extra information such as a link to the source code on Github/Bitbucket and the authors name (just so you get the recognition you deserve!).
Run the following terminal command from you package folder to make sure everything is nicely formatted:
flutter format .
This command will check that everything is correct before you publish your package:
flutter packages pub publish --dry-run
Finally run this to share your package with the community:
flutter packages pub publish
If this is your first package you will need a Gmail email address which will be associated to you on Dart Pub. Only uploaders can update the package, but you can give access to more people by adding them to the list of authorized uploaders.
It will take a few hours for your package to appear on Dart Pub, so take a (very) long and well deserved coffee break. Or a tea break. Or whatever kind of break you want, we aren’t judging! Once the package is live, people will be able to use it by simply referencing it in their
Good job, you have now successful uploaded your first package for the whole world to enjoy! Now, go and treat yourself to an alcoholic beverage, or a healthy fruit juice, or some water (you know the drill…), its on us!*
*PS: It isn’t actually on us, we aren’t that rich…