如何将示例项目添加到Flutter软件包中?

时间:2019-06-08 12:32:11

标签: flutter dart flutter-dependencies dart-pub

有没有一种方法可以自动为包中的示例地图结构生成模板代码?

大多数软件包在软件包根目录中都有一个/ example文件夹,用于展示软件包的功能。 我不确定创建示例的“最佳”方法是什么,或者甚至无关紧要。

我是否单独创建所有文件? 我是否要创建一个新项目,然后将其复制到程序包的根目录? 我可以直接在项目中自动生成示例项目吗?

这是我第一次尝试创建软件包,我想在第一时间得到正确的答案。

4 个答案:

答案 0 :(得分:3)

使用项目根目录中的内置 flutter 命令行工具来完成所有工作:

/e/projects/myproject # flutter create example

具有以下输出:

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\Icon-App-1024x1024@1x.png (created)
  example\ios\Runner\Assets.xcassets\AppIcon.appiconset\Icon-App-20x20@1x.png (created)
  example\ios\Runner\Assets.xcassets\AppIcon.appiconset\Icon-App-20x20@2x.png (created)
  example\ios\Runner\Assets.xcassets\AppIcon.appiconset\Icon-App-20x20@3x.png (created)
  example\ios\Runner\Assets.xcassets\AppIcon.appiconset\Icon-App-29x29@1x.png (created)
  example\ios\Runner\Assets.xcassets\AppIcon.appiconset\Icon-App-29x29@2x.png (created)
  example\ios\Runner\Assets.xcassets\AppIcon.appiconset\Icon-App-29x29@3x.png (created)
  example\ios\Runner\Assets.xcassets\AppIcon.appiconset\Icon-App-40x40@1x.png (created)
  example\ios\Runner\Assets.xcassets\AppIcon.appiconset\Icon-App-40x40@2x.png (created)
  example\ios\Runner\Assets.xcassets\AppIcon.appiconset\Icon-App-40x40@3x.png (created)
  example\ios\Runner\Assets.xcassets\AppIcon.appiconset\Icon-App-60x60@2x.png (created)
  example\ios\Runner\Assets.xcassets\AppIcon.appiconset\Icon-App-60x60@3x.png (created)
  example\ios\Runner\Assets.xcassets\AppIcon.appiconset\Icon-App-76x76@1x.png (created)
  example\ios\Runner\Assets.xcassets\AppIcon.appiconset\Icon-App-76x76@2x.png (created)
  example\ios\Runner\Assets.xcassets\AppIcon.appiconset\Icon-App-83.5x83.5@2x.png (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\LaunchImage@2x.png (created)
  example\ios\Runner\Assets.xcassets\LaunchImage.imageset\LaunchImage@3x.png (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.

如果使用的是Android Studio 3.5.2,请运行默认示例进行确认:

  1. 通过从工具栏上的Flutter下拉菜单中选择编辑配置... ,为示例main.dart添加新配置。

  2. 选择运行/调试配置对话框左侧的Flutter目录,然后按 + 按钮。

  3. 选择 Flutter

  4. 通过单击 Dart入口点:对话框中的文件夹,在新的example / lib目录中浏览main.dart。

  5. 单击工具栏上的三角形运行按钮。

我只是为自己的项目执行了这些步骤,所以效果很好。

然后,要为您的项目进行自定义,请编辑示例的 pubspec.yaml 文件并向您的项目添加依赖项:

dev_dependencies:
  my_project:
    path: ../

答案 1 :(得分:3)

使用Android Studio中的示例创建Flutter包

  1. 创建一个新的Flutter项目 enter image description here
  2. 选择Flutter软件包 enter image description here
  3. 在Android Studio中打开新项目后,选择“终端”标签,然后运行:

动态创建示例

enter image description here

  1. 选择“编辑配置”->添加配置->选择位于example / lib文件夹中的main.dart文件 enter image description here enter image description here enter image description here enter image description here

  2. 打开示例pubspec.yaml并通过添加以下内容链接到库:

    您的包装名称: 路径:.. \ enter image description here

注意:当打开Example文件夹时,它看起来像是整个Flutter应用程序嵌套在您的项目中。那是因为。只需打开io和android文件夹即可看到它们共享该模式: enter image description here

答案 2 :(得分:2)

在项目的根目录中创建一个名为example的新项目,然后删除不必要的文件,例如CHANGELOGLICENSEREADME,因为它们将位于包文件夹中。 / p>

这是我创建的程序包的一个示例(无双关语):

enter image description here enter image description here

现在在pubspec.yaml中,您应该像这样将软件包包含为开发依赖项:

dev_dependencies:
  your_package:
    path: ../

我相信您也可以使用常规依赖项,但这对我有用。

现在在/lib内,您可以添加main.dart文件,导入包,然后创建示例项目。

答案 3 :(得分:2)

转到您的项目根文件夹并运行flutter create example
就是这样。