1. 创建packages
flutter create --template=package flutter_easy_use_tools
2. 实现功能
lib
下创建src
文件夹,src
文件下的文件是不对外开放的,开放则通过flutter_easy_use_tools.dart
来对外开放。
library flutter_easy_use_tools;
export 'src/extension/string_extension.dart';
export 'src/utils/num_util.dart';
export 'src/utils/string_util.dart';
export 'src/widgets/custom_inner_shadow.dart';
3. 检查pubspec.yaml
name: flutter_easy_use_tools
description: A new Flutter project.
version: 0.0.1
homepage: https://github.com/yuanzhiying/flutter_easy_use_tools
4. 验证packages
$ flutter packages pub publish --dry-run
$ flutter packages pub publish --dry-run
Publishing flutter_easy_use_tools 0.0.1 to https://pub.flutter-io.cn:
|-- .gitignore
|-- .metadata
|-- CHANGELOG.md
|-- LICENSE
|-- README.md
|-- lib
| |-- extension
| | '-- string_extension.dart
| |-- flutter_easy_use_tools.dart
| '-- widgets
| '-- custom_inner_shadow.dart
|-- pubspec.yaml
'-- test
'-- flutter_easy_use_tools_test.dart
Package has 0 warnings.
5. 发布packages
执行flutter packages pub publish
,点击其中链接浏览器打开授权,需要登录谷歌账号。
先用命令测试能否访问谷歌:$ curl www.google.com
$ flutter packages pub publish
Publishing flutter_easy_use_tools 0.0.1 to https://pub.flutter-io.cn:
|-- .gitignore
|-- .metadata
|-- CHANGELOG.md
|-- LICENSE
|-- README.md
|-- lib
| |-- extension
| | '-- string_extension.dart
| |-- flutter_easy_use_tools.dart
| '-- widgets
| '-- custom_inner_shadow.dart
|-- pubspec.yaml
'-- test
'-- flutter_easy_use_tools_test.dart
Publishing is forever; packages cannot be unpublished.
Policy details are available at https://pub.dev/policy
Do you want to publish flutter_easy_use_tools 0.0.1 (y/N)? y
Pub needs your authorization to upload packages on your behalf.
In a web browser, go to https://accounts.google.com/o/oauth2/auth?access_type=offline&approval_prompt=force&response_type=code&client_id=818368855108-8grd2eg9tj9f38os6f1urbcvsq399u8n.apps.googleusercontent.com&redirect_uri=http%3A%2F%2Flocalhost%3A58974&code_challenge=Nc8EH9vceyITk7SeK9H9iEXvcPTlLxzBLhFWE9zwnm0&code_challenge_method=S256&scope=openid+https%3A%2F%2Fwww.googleapis.com%2Fauth%2Fuserinfo.email
Then click "Allow access".
Waiting for your authorization...
发布成功的情况:
$ flutter packages pub publish
Publishing flutter_easy_use_tools 0.0.1 to https://pub.dartlang.org:
|-- .gitignore
|-- .metadata
|-- CHANGELOG.md
|-- LICENSE
|-- README.md
|-- lib
| |-- extension
| | '-- string_extension.dart
| |-- flutter_easy_use_tools.dart
| '-- widgets
| '-- custom_inner_shadow.dart
|-- pubspec.yaml
'-- test
'-- flutter_easy_use_tools_test.dart
Downloading vector_math 2.1.0...
Publishing is forever; packages cannot be unpublished.
Policy details are available at https://pub.dev/policy
Do you want to publish flutter_easy_use_tools 0.0.1 (y/N)? y
Uploading...
Successfully uploaded package.
如果使用了代理,则需要在客户端上执行终端代理命令:
我的代理命令如下:
export HTTP_PROXY=http://127.0.0.1:1081; export HTTPS_PROXY=http://127.0.0.1:1081; export ALL_PROXY=socks5://127.0.0.1:1080
执行完之后,再执行发布命令。
6. 查看自己发布的packages
使用谷歌账号,登录网站:https://pub.dev/
查看自己发布的插件:
搜索自己发布的插件:
7. 更新插件
编写完新的插件代码后:
① 修改pubspec.yaml
文件中的version: 0.0.1
为version: 0.0.2
。
② CHANGELOG.md
增加更新日志,最新版本在最上面。
③ 修改README.md
文件。
④ 提交git
代码,添加tag
。
$ flutter packages pub publish
重新发布。
8. 为插件添加example
在packages
目录下,创建example
项目。
添加插件到example
,
dependencies:
flutter:
sdk: flutter
flutter_easy_use_tools:
path: ../
example
中使用:
import 'package:flutter_easy_use_tools/flutter_easy_use_tools.dart';
在packages
页面查看:
9. 插件问题完善
在我的插件后台可以看到插件不完善和不规范的地方,可以完善以提高评分。
代码格式化执行命令:
flutter format .
$ flutter format .
Formatting directory .:
Unchanged example/lib/main.dart
Unchanged example/test/widget_test.dart
Unchanged lib/flutter_easy_use_tools.dart
Formatted lib/src/extension/string_extension.dart
Formatted lib/src/utils/num_util.dart
Formatted lib/src/utils/string_util.dart
Formatted lib/src/widgets/custom_inner_shadow.dart
Unchanged test/flutter_easy_use_tools_test.dart
10. 避免通过package:flutter_easy_use_tools/src/...
方式来使用插件。
应该通过插件提供的对外文件来使用:
import 'package:flutter_easy_use_tools/flutter_easy_use_tools.dart';
11. 为packages
创建文档
使用 dart doc 可以为 Library 生成 API 文档。
dartdoc 解析源文件去查找使用 ///
语法标注的文档注释。
进入packages
根目录,执行dart doc .
命令。
$ dart doc .
Documenting flutter_easy_use_tools...
dartdoc failed: Top level package requires Flutter but FLUTTER_ROOT environment variable not set.
执行失败。
查看FLUTTER_ROOT
,
$ echo $FLUTTER_ROOT
添加FLUTTER_ROOT
添加FLUTTER_ROOT
,执行,查看。
$ source ~/..bash_profile
$ echo $FLUTTER_ROOT
/Users/yuanzhiying/fvm/versions/stable
$ dartdoc
Documenting flutter_easy_use_tools...
Initialized dartdoc with 393 libraries in 48.6 seconds
Generating docs for library flutter_easy_use_tools from package:flutter_easy_use_tools/flutter_easy_use_tools.dart...
warning: flutter_easy_use_tools has no library level documentation comments, from flutter_easy_use_tools: (file:///Users/yuanzhiying/github/flutter_easy_use_tools/lib/flutter_easy_use_tools.dart:1:9)
Validating docs...
Found 1 warning and 0 errors.
Documented 1 public library in 2.6 seconds
Success! Docs generated into /Users/yuanzhiying/github/flutter_easy_use_tools/doc/api