Recently, I published a story on Recreating the YouTube app in Flutter and I realised it’d be nice to have the same Flutter PIP widget up on Dart Pub. So I decided to create a Widget for the same.

I decided to write this article as a walk-through for creating and publishing a package and to encourage more people to share things they’ve created in Flutter.

What is a Package?

Packages allow code-sharing and reusability across Flutter/Dart apps. If you think something you’ve made like a new kind of Widget or a tool might be helpful to other developers, consider making a package out of it and uploading it to Dart Pub.

Dart Packages v/s Plugin Packages

In Flutter, there are two types of packages.

The first is a pure Dart package, like a new Widget or a set of Dart classes and is usually simply referred to as a ‘package’.

The second is kind of package makes it easier to access platform-specific functionality. For example, the “battery” package helps you check about the battery details. This requires you to access the platform, but the package does that for you. These kinds of packages are referred to as ‘plugins’.

Creating a Package

Creating a package with Android Studio is remarkably easy to do and kudos to the tooling team for Flutter.

When creating a Flutter project in Android Studio, this is the first window that pops up:

Instead of Flutter Application, choosing Flutter Package creates a basic package for you.

Taking a look at the project files

A package is structured exactly like a Flutter application is:

The lib directory still holds our main Dart code. The pubspec.yaml still holds our dependencies.

The default package has a Calculator class and the ‘test’ directory has a simple test related to it. I’ll omit adding the code for brevity.

Here are a few of the key differences:

All code we want to share should have a line declaring the library it belongs to.

library demo_package;

2. The pubspec.yaml should contain the details about our package version number, author, email and home page where the package is hosted

name: demo_package

description: A new Flutter package.

version: 0.0.1

author:

homepage:

3. The ‘LICENSE’ file should contain a license for the use of the package, usually given a BSD-style license.

4. The ‘README’ should neatly document the use of the package and any important things to note.

5. Adding an “example” directory is helpful for developers using the package and also adds an “Example” tab when the package is published.

6. The ‘CHANGELOG’ logs the changes in every version of the package.

Publishing the package

Publishing the package is as simple as running “pub publish” in the terminal.

Before publishing the package, make sure to run “pub publish — dry-run” which essentially simulates uploading the package without actually publishing it. Any missing files or attributes will be pointed out by the dry run.

For example if I don’t fill a few fields and do a dry run:

Some final thoughts

If you’re already proficient with normal Flutter application development, it never feels like we go out of our way to develop a package. Writing a package doesn’t feel very different from creating a normal app and that is really commendable. So if at any time you feel like something you make might be useful to the Flutter community, be sure to give packages a try.

This article was inspired by my experience in making a simple Picture-In-Picture package for Flutter.

Package Link

GitHub Link