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

h9vpoimq  于 2023-02-01  发布在  Flutter
关注(0)|答案(5)|浏览(159)

有没有办法为包中的示例Map结构自动生成模板代码?
大多数软件包在根目录下都有一个/example文件夹,它展示了软件包的功能,我不确定创建示例的“最佳”方式是什么,或者这是否重要。
我是否可以单独创建所有文件?我是否可以创建一个新项目,然后将其复制到包根目录?我是否可以直接在项目中自动生成一个示例项目?
这是我第一次尝试创建一个包,我想第一次就把它做好。

gkn4icbw

gkn4icbw1#

在Android Studio中创建包含示例的Flutter包
1.创建新的Flutter项目x1c 0d1x
1.选择Flutter包

1.在Android Studio中打开新项目后,选择“终端”选项卡,然后运行:

** Flutter 创建示例**

1.选择“编辑配置”-〉添加配置-〉选择位于示例/lib文件夹

x 1c 4d 1xx 1c 5d 1xx 1c 6d 1x中的主.dart文件
1.打开示例pubspec.yaml,并通过添加以下内容链接到库:
您的软件包名称:路径:..

注意:当你打开Example文件夹时,它看起来就像一个嵌套在你项目中的完整Flutter应用程序;那是因为它就是这样,只要打开io和android文件夹,就能看到它们都有这样的模式:

slwdgvem

slwdgvem2#

在项目的根目录中创建一个名为example的新项目,然后删除不必要的文件,如CHANGELOGLICENSEREADME,因为它们将位于包文件夹中。
下面是我创建的一个包的示例(没有双关语):
第一节第一节第一节第一节第一次
现在在pubspec.yaml中,你应该像这样把你的软件包作为dev依赖项包括进来:

dev_dependencies:
  your_package:
    path: ../

我相信您也可以使用常规依赖项,但这对我很有效。
现在,在/lib中,您可以添加一个main.dart文件,导入您的包,然后创建一个示例项目。

yeotifhr

yeotifhr3#

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

yhxst69z

yhxst69z4#

使用项目根目录中的内置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下拉菜单中选择 *Edit Configurations... *,为示例main.dart添加新配置。
1.选择 Run/Debug Configurations(运行/调试配置) 对话框左侧的Flutter目录并按下 + 按钮。
1.选择 * Flutter *
1.单击 Dart entrypoint: 对话框中的文件夹,在新的example/lib目录中浏览main.dart。
1.单击工具栏上的三角形运行按钮。
我只是为我自己的项目执行了这些步骤,它工作得很完美。
然后,要为您的项目进行定制,请编辑示例的pubspec.yaml文件,并向您的项目添加依赖项:

dependencies:
  my_project:
    path: ../
1zmg4dgp

1zmg4dgp5#

  • 在我的例子中,我忘记添加示例文件。我是这样添加的。* 你可以在我的pub.devlocation_provider包中检查。

1)在包内创建目录/包。包/目录的名称必须为“example"。有关详细信息,请查看此选项。***
2)打开命令终端并使用以下命令移动到***示例***包内

cd example

3)使用命令在其中创建Flutter项目

Flutter create

4)您将在其中看到***lib/main.dart***文件。请在此文件中添加您的示例。
5)添加后,将最新代码推送到github。
6)在***pubspec.yaml***文件和***CHELLAGELOG.md***文件中增加软件包的版本。然后,运行这些命令将软件包发布到*pub.dev

Flutter publish --dry-run
Flutter publish
  • 之后,您将看到**pub.dev*中的示例选项卡

相关问题