Joel Broström
Joel Broström

Reputation: 4060

How do I add an Example Project to a Flutter Package?

Is there a way to autogenerate template code for the example map structure in a package?

Most packages have a /example folder within the package root that showcases the package functionality. I'm not sure what's the "best" way to create the example is, or if it even matter.

Do I create all files individually? Do I create a new project and then copy it to the package root? Can I autogenerate an example project directly in the project?

It's my first attempt on creating a package and I want to get it right the first time.

Upvotes: 36

Views: 12856

Answers (6)

Dilip
Dilip

Reputation: 2734

If you have fvm setup then

fvm flutter create --template=package upr_ride_reservation_bloc

If you don't have fvm then

flutter create --template=package package name

Upvotes: 0

Rahul Kushwaha
Rahul Kushwaha

Reputation: 6722

In my case , I had forgot to add the example file . I added this in this way. You can check at my pub.dev location_provider package.

1)Create a directory/package inside you package . Name of package/directory must be "example". For more info check this.

2)Open command Terminal and move inside the example package using

cd example

3)Create Flutter project inside it using command

Flutter create

4)You will see the lib/main.dart file inside it. Add your example in this file.

5)After adding , push latest code to the github.

6)Increase the version of your package inside pubspec.yaml file and CHELLAGELOG.md file . After that, run these command to publish the package to pub.dev.

Flutter publish --dry-run
Flutter publish

After that you will see ,example Tab in pub.dev

Upvotes: 0

Pete Alvin
Pete Alvin

Reputation: 4790

Use the built-in flutter command-line tool in the root directory of the project to do all the work:

/e/projects/myproject # flutter create example

which has this output:

Creating project example... androidx: true
  example\.gitignore (created)
  example\.idea\libraries\Dart_SDK.xml (created)
  example\.idea\libraries\Flutter_for_Android.xml (created)
  example\.idea\libraries\KotlinJavaRuntime.xml (created)
  example\.idea\modules.xml (created)
  example\.idea\runConfigurations\main_dart.xml (created)
  example\.idea\workspace.xml (created)
  example\.metadata (created)
  example\android\app\build.gradle (created)
  example\android\app\src\main\kotlin\com\example\example\MainActivity.kt (created)
  example\android\build.gradle (created)
  example\android\example_android.iml (created)
  example\android\.gitignore (created)
  example\android\app\src\debug\AndroidManifest.xml (created)
  example\android\app\src\main\AndroidManifest.xml (created)
  example\android\app\src\main\res\drawable\launch_background.xml (created)
  example\android\app\src\main\res\mipmap-hdpi\ic_launcher.png (created)
  example\android\app\src\main\res\mipmap-mdpi\ic_launcher.png (created)
  example\android\app\src\main\res\mipmap-xhdpi\ic_launcher.png (created)
  example\android\app\src\main\res\mipmap-xxhdpi\ic_launcher.png (created)
  example\android\app\src\main\res\mipmap-xxxhdpi\ic_launcher.png (created)
  example\android\app\src\main\res\values\styles.xml (created)
  example\android\app\src\profile\AndroidManifest.xml (created)
  example\android\gradle\wrapper\gradle-wrapper.properties (created)
  example\android\gradle.properties (created)
  example\android\settings.gradle (created)
  example\ios\Runner\AppDelegate.swift (created)
  example\ios\Runner\Runner-Bridging-Header.h (created)
  example\ios\Runner.xcodeproj\project.pbxproj (created)
  example\ios\Runner.xcodeproj\xcshareddata\xcschemes\Runner.xcscheme (created)
  example\ios\.gitignore (created)
  example\ios\Flutter\AppFrameworkInfo.plist (created)
  example\ios\Flutter\Debug.xcconfig (created)
  example\ios\Flutter\Release.xcconfig (created)
  example\ios\Runner\Assets.xcassets\AppIcon.appiconset\Contents.json (created)
  example\ios\Runner\Assets.xcassets\AppIcon.appiconset\[email protected] (created)
  example\ios\Runner\Assets.xcassets\AppIcon.appiconset\[email protected] (created)
  example\ios\Runner\Assets.xcassets\AppIcon.appiconset\[email protected] (created)
  example\ios\Runner\Assets.xcassets\AppIcon.appiconset\[email protected] (created)
  example\ios\Runner\Assets.xcassets\AppIcon.appiconset\[email protected] (created)
  example\ios\Runner\Assets.xcassets\AppIcon.appiconset\[email protected] (created)
  example\ios\Runner\Assets.xcassets\AppIcon.appiconset\[email protected] (created)
  example\ios\Runner\Assets.xcassets\AppIcon.appiconset\[email protected] (created)
  example\ios\Runner\Assets.xcassets\AppIcon.appiconset\[email protected] (created)
  example\ios\Runner\Assets.xcassets\AppIcon.appiconset\[email protected] (created)
  example\ios\Runner\Assets.xcassets\AppIcon.appiconset\[email protected] (created)
  example\ios\Runner\Assets.xcassets\AppIcon.appiconset\[email protected] (created)
  example\ios\Runner\Assets.xcassets\AppIcon.appiconset\[email protected] (created)
  example\ios\Runner\Assets.xcassets\AppIcon.appiconset\[email protected] (created)
  example\ios\Runner\Assets.xcassets\AppIcon.appiconset\[email protected] (created)
  example\ios\Runner\Assets.xcassets\LaunchImage.imageset\Contents.json (created)
  example\ios\Runner\Assets.xcassets\LaunchImage.imageset\LaunchImage.png (created)
  example\ios\Runner\Assets.xcassets\LaunchImage.imageset\[email protected] (created)
  example\ios\Runner\Assets.xcassets\LaunchImage.imageset\[email protected] (created)
  example\ios\Runner\Assets.xcassets\LaunchImage.imageset\README.md (created)
  example\ios\Runner\Base.lproj\LaunchScreen.storyboard (created)
  example\ios\Runner\Base.lproj\Main.storyboard (created)
  example\ios\Runner\Info.plist (created)
  example\ios\Runner.xcodeproj\project.xcworkspace\contents.xcworkspacedata (created)
  example\ios\Runner.xcworkspace\contents.xcworkspacedata (created)
  example\lib\main.dart (created)
  example\example.iml (created)
  example\pubspec.yaml (created)
  example\README.md (created)
  example\test\widget_test.dart (created)
Running "flutter pub get" in example...                             3.0s
Wrote 68 files.

All done!
[√] Flutter: is fully installed. (Channel stable, v1.12.13+hotfix.5, on Microsoft Windows [Version 6.1.7601], locale en-US)
[√] Android toolchain - develop for Android devices: is fully installed. (Android SDK version 29.0.2)
[√] Android Studio: is fully installed. (version 3.5)
[√] Connected device: is fully installed. (2 available)

In order to run your application, type:

  $ cd example
  $ flutter run

Your application code is in example\lib\main.dart.

If using Android Studio 3.5.2, run the default example for assurance:

  1. Add a new configuration for the example main.dart by selecting Edit Configurations... from the Flutter drop-down on the toolbar.

  2. Select the Flutter directory on the left of the Run/Debug Configurations dialog and press the + button.

  3. Select Flutter

  4. Browse for main.dart in the new example/lib directory by clicking the file folder in the Dart entrypoint: dialog.

  5. Click the triangle run button on the toolbar.

I just performed these steps for my own project and it worked perfectly.

Then, to customize for your project, edit the example's pubspec.yaml file and add a dependency to your project:

dependencies:
  my_project:
    path: ../

Upvotes: 10

Chris Sprague
Chris Sprague

Reputation: 3584

To create a Flutter package with an Example in Android Studio

  1. Create a new Flutter Project enter image description here
  2. Select Flutter Package enter image description here
  3. After the new project opens in Android Studio, select the "Terminal" tab and then run:

flutter create example

enter image description here

  1. Select "Edit Configuration"-> add configuration -> Select the main.dart file that is located in the example/lib folder enter image description here enter image description here enter image description here enter image description here

  2. Open your example pubspec.yaml and link to the library by adding:

    your_package_name: path: ..\ enter image description here

NOTE: When you open the Example folder it will look like an entire Flutter app nested into your project; that's because it is. Just open the io and android folders to see that they share that pattern: enter image description here

Upvotes: 51

Joel Broström
Joel Broström

Reputation: 4060

Go to your project root folder and run flutter create example.
Thats it.

Upvotes: 22

Noodles
Noodles

Reputation: 4080

Create a new project inside the root directory of your project called example and then remove unnecessary files such as CHANGELOG, LICENSE and README as they will be in your package folder.

Here's an example (no pun intended) with a package that I created:

enter image description here enter image description here

Now in pubspec.yaml you should include your package as dev dependency like this:

dev_dependencies:
  your_package:
    path: ../

I believe you can also use regular dependencies, but this worked for me.

Now inside /lib you can add a main.dart file, import your package and then create an example project.

Upvotes: 20

Related Questions