Check Mobile App
Read now
Start a Project

How To Create Package In Flutter

Save

In this tutorial How To Create Package In Flutter, you’ll learn how to create your own Dart packages to be used in Flutter apps.

How To Create Package In Flutter? Dart packages help us solve issues and create workarounds for problems without having to write the code ourselves from scratch.

Some advantages of being a package publisher:

Creating a Flutter/Dart package

On your Android Studio’s menu bar, click File ▸ New ▸ New Flutter Project. Then, select Flutter Package and click Next.

Now, follow the instructions to fill in the fields:

Click Finish and wait for Android Studio to create and load your new project.

Or you can you terminal

Writing a Flutter widget

Next, we’ll create a custom button styled to our liking.

Clear the original code generated by Flutter in the lib/<project name>.dart. Then, add the CustomButton widget

The library <project name> code sets the name of our package to <project name>.

First, we imported the Flutter Material package — this is the root of all Flutter apps. Next, we created a CustomButton class that extends the StatelessWidget class. This causes our CustomButton widget to hold or manage no local state.

We have three properties that the CustomButton widget constructor will receive:

The build method renders a TextButton and styles the button as follows:

Our custom button is something like a smaller version of TextButton. This button renders a customized TextButton. Inside our CustomButton, we increased the padding, elevation, background color, and text style of the TextButton.

Adjusting the Pubspec

Open your new project’s pubspec.yaml file. Notice that the content of the file is grouped into five sections: metadata, environmentdependenciesdev_dependencies, and flutter. For this tutorial, you’ll need only the first three:

The Metadata

Everything users need to find your package comes out of here.

You already provided parts of this information when creating the project. Now you need only a few adjustments:

The Environment

Use this section to restrict the Dart or Flutter SDK versions of your users. This is useful if, for example, your package relies on a feature introduced in an earlier Flutter release or you still haven’t complied with a breaking change in the API.

The Dependencies

Packages can also depend on other packages. This section is no different than the one you find in your apps. You can also use other packages in your package, Specify the dependency, then make sure your section looks like this:

Creating an Example Project

Every good package has an example app. The example app is the first thing your users will turn to if they can’t make your package work right off the bat. It should be as concise as possible while still showcasing every feature.

Create your example project by again clicking File ▸ New ▸ New Flutter Project. This time, select Flutter Application and click Next.

Like you did before, follow the instructions to fill in the fields:

Click Next and, in the following window, type com.YOUR_APP_NAME.example in the Package name field. Finally, click Finish and wait until Android Studio opens your example project in a new window.

Specifying the Example’s Dependencies

Open your example project’s pubspec.yaml and replace the entire dependencies section with:

Open lib/main.dart and add the following code to the _MyHomePageState widget:

We imported the your_flutter package, then we set the CustomButton widget in the Center widget.

Run the example project by running flutter run in the command line. If you’re using VS Code, right-click on the lib/main.dart file and click Run Without Debugging.

Conclusion

How To Create Package In Flutter in this blog we covered a lot in this tutorial. We started by introducing packages in Dart, what they are, and how they are designed to share code with other devs.

Later on, we learned how to scaffold a Flutter package project and how to write the package code. Next, we learned how to test our Flutter package locally.

If you want to learn about how to publish a package on https://pub.dev/ please visit this link:

https://www.raywenderlich.com/19421827-creating-and-publishing-a-flutter-package#toc-anchor-001

You can read more blogs from Mobikul on this website: https://mobikul.com/blog/

. . .

Leave a Comment

Your email address will not be published. Required fields are marked*


Be the first to comment.

css.php

Very good company, we did get app extension for Magento 2 website. The support was very good they help us through the process. Webkul team is very supportive, I would recommend them.

Yasser Mohamed

Yasser Mohamed

Talk to Sales

Global

Live Chat
Start a Project

    Message Sent!

    If you have more details or questions, you can reply to the received confirmation email.

    Back to Home