Flutter Packages

A package is a namespace that is a group of similar types of classes, interfaces, and sub-packages. Packages are similar to different folders in our computers where we might keep movies in one, images in another, software in another, etc. In Flutter, Dart organizes and shares a set of functionality using packages.The packages help us to build the app without developing everything from scratch.

The check the standard structure of a package(Assuming a demo package as mycustom_package):

lib/src/*: It contains private Dart code files.

lib/mydemo_package.dart: It is a main Dart code file. We import the package into our code as below:

 import 'package:mycustom_package/mycustom_package.dart'  

We can also export any other code into the main code using syntax below:

 Export src/my_code.dart

lib/*: It is the directory, which contains all the public code in the package. We can access the code using below syntax:

 import 'package:mydemo_package/sub_folder/custom_file.dart'

pubspec.yaml: It is the project’s configuration file that wel use a lot while working on Flutter projects. This file contains:

  • General settings such as name, description, and the version of the project.
  • Dependencies.
  • Assets (e.g., images,fonts etc).
Types of Packages

We can categorize these packages into two types:

  1. Dart Package
  2. Plugin Package

Dart Package: This is a general package, which is written in dart, such as a path package. This package can be used in a web or mobile platform. It contains some of the Flutter specific functionality and thus has a dependency on Flutter framework, such as the fluro package.

Plugin Package: It is a Dart package which is specialized to include an API written in Dart code and also depends on the Flutter framework. We can combine it with a platform-specific implementation for an underlying platform such as Android (using Java or Kotlin), and iOS (using Objective C or Swift). Examples of this package are battery and image picker plugin packages.

Develop a Flutter Package or Plugin

Developing a Flutter package is very similar to a Dart application or Dart package. But, it has some exceptions means packages use system API specific to the platform to get the required functionality. Now, let us see step by step how to develop your own packages in Flutter.

Step:1 First, In Android Studio click on File menu -> Select a New Flutter Project.

flutter-package-plugin

Step 2: A Dialog box will appear in this. We need to select New Flutter Project option, as shown.

flutter-new-project-package

Step 3: In a new Dialog Box , set all the details of the package, example project name, location, and description. After filling, click Finish.

flutter-new-package

Step 4: : Finally, your project will be created. Now, open the flutter_custom_package.dart file and remove the default code created at the time of creation. Then add the following code. This code snippet creates an alert box package.

 library flutter_custom_package;  
  
import 'package:flutter/material.dart';  
  
class CustomPackageAlertBox {  
  static Future showCustomAlertBox({  
    @required BuildContext context,  
    @required Widget willDisplayWidget,  
  }) {  
    assert(context != null, "If context is null!!");  
    assert(willDisplayWidget != null, "If willDisplayWidget is null!!");  
    return showDialog(  
        context: context,  
        builder: (context) {  
          return AlertDialog(  
            shape: RoundedRectangleBorder(  
              borderRadius: BorderRadius.all(Radius.circular(20)),  
            ),  
            content: Column(  
              mainAxisSize: MainAxisSize.min,  
              children: [  
                willDisplayWidget,  
                MaterialButton(  
                  color: Colors.white70,  
                  child: Text('Close Alert'),  
                  onPressed: () {  
                    Navigator.of(context).pop();  
                  },  
                )  
              ],  
            ),  
            elevation: 12,  
          );  
        });  
  }  
}  

Now, we need to test the newly created package. To test the package, create a project. In this project add the package to the pubspec.yaml file by adding the following code in the dependency section.

 dependencies:   
   flutter:   
      sdk: flutter   
   flutter_custom_package:   
      path: ../  

After adding the custom package in pubspec.yaml file, You have to update the pub by clicking on the Get dependencies. The Android Studio will automatically get the package. Now, we can use this package. You can import the package in the code file as below line:

import 'package: flutter_custom_package/flutter_custom_package.dart';  

How to Publish a Package

When you have successfully implemented and tested the package, you can publish it on the pub.dev.

Before publishing the package, make sure to double check the content of pubspec.yaml, README.md, and CHANGELOG.md file is complete and correct.

Next, run the following command to analyze every phase of the package.

 $ flutter pub publish --dry-run

Finally, run the following command to publish the package.

 $ flutter pub publish 
Subscribe Now