GetX™ 框架的官方 CLI 使用指南
2022-03-07 本文已影响0人
吾等斩去红尘时
GetX™ 框架的官方 CLI 使用指南
安装命令行工具
flutter pub global activate get_cli
配置环境变量
.zshrc
###
# Flutter GetX Cli
###
export PATH="$PATH":"$HOME/snap/flutter/.pub-cache/bin"
使环境变量生效
source .zshrc
验证安装是否成功
get --version
终端信息
The [--version] is not necessary
░██████╗░███████╗████████╗ ░█████╗░██╗░░░░░░██╗
██╔════╝░██╔════╝╚══██╔══╝ ██╔══██╗██║░░░░░░██║
██║░░██╗░█████╗░░░░░██║░░░ ██║░░╚═╝██║░░░░░░██║
██║░░╚██╗██╔══╝░░░░░██║░░░ ██║░░██╗██║░░░░░░██║
╚██████╔╝███████╗░░░██║░░░ ╚█████╔╝███████╗ ██║
░╚═════╝░╚══════╝░░░╚═╝░░░ ░╚════╝░╚══════╝ ╚═╝
Version: 1.7.1
Time: 200 Milliseconds
创建项目
get create project:flutter_get_01
终端信息
? (Use arrow keys)
❯ Flutter Project
Get Server
? What is your company's domain? Example: com.yourcompany com.dabolau
? what language do you want to use on ios? (Use arrow keys)
❯ Swift
Objective-C
? what language do you want to use on android? (Use arrow keys)
❯ Kotlin
Java
? Do you want to use null safe? (Use arrow keys)
❯ Yes!
No
? do you want to use some linter? (Use arrow keys)
❯ no
Pedantic [Deprecated]
Effective Dart [Deprecated]
Dart Recommended
Running `flutter create /Users/dabolau/temp/flutter_get_01` …
$ flutter create --no-pub -i swift -a kotlin --org com.dabolau /Users/dabolau/temp/flutter_get_01
Creating project ....
Wrote 128 files.
All done!
In order to run your application, type:
$ cd .
$ flutter run
Your application code is in ./lib/main.dart.
Running `flutter pub get` …
$ flutter pub get
Running "flutter pub get" in flutter_get_01... 1,127ms
$ dart migrate --apply-changes --skip-import-check
Migrating /Users/dabolau/temp/flutter_get_01
See https://dart.dev/go/null-safety-migration for a migration guide.
Analyzing project...
All sources appear to be already migrated. Nothing to do.
✓ File: analysis_options.yaml created successfully at path: analysis_options.yaml
? Which architecture do you want to use? (Use arrow keys)
❯ GetX Pattern (by Kauê)
CLEAN (by Arktekko)
? Your lib folder is not empty. Are you sure you want to overwrite your application?
WARNING: This action is irreversible (Use arrow keys)
❯ Yes!
No
✓ 'Package: get installed!
✓ File: main.dart created successfully at path: lib/main.dart
✓ File: home_controller.dart created successfully at path: lib/app/modules/home/controllers/home_controller.dart
✓ File: home_view.dart created successfully at path: lib/app/modules/home/views/home_view.dart
✓ File: home_binding.dart created successfully at path: lib/app/modules/home/bindings/home_binding.dart
✓ File: app_routes.dart created successfully at path: lib/app/routes/app_routes.dart
✓ File: app_pages.dart created successfully at path: lib/app/routes/app_pages.dart
✓ home route created successfully.
✓ Home page created successfully.
✓ GetX Pattern structure successfully generated.
Running `flutter pub get` …
$ flutter pub get
Running "flutter pub get" in flutter_get_01... 1,027ms
Time: 42341 Milliseconds
目录结构
.
├── app
│ ├── data
│ ├── modules
│ │ └── home
│ │ ├── bindings
│ │ │ └── home_binding.dart
│ │ ├── controllers
│ │ │ └── home_controller.dart
│ │ └── views
│ │ └── home_view.dart
│ └── routes
│ ├── app_pages.dart
│ └── app_routes.dart
└── main.dart
创建页面
get create page:login
终端信息
✓ File: login_controller.dart created successfully at path: lib/app/modules/login/controllers/login_controller.dart
✓ File: login_view.dart created successfully at path: lib/app/modules/login/views/login_view.dart
✓ File: login_binding.dart created successfully at path: lib/app/modules/login/bindings/login_binding.dart
✓ login route created successfully.
✓ Login page created successfully.
Time: 742 Milliseconds
目录结构
.
├── app
│ ├── data
│ ├── modules
│ │ ├── home
│ │ │ ├── bindings
│ │ │ │ └── home_binding.dart
│ │ │ ├── controllers
│ │ │ │ └── home_controller.dart
│ │ │ └── views
│ │ │ └── home_view.dart
│ │ └── login
│ │ ├── bindings
│ │ │ └── login_binding.dart
│ │ ├── controllers
│ │ │ └── login_controller.dart
│ │ └── views
│ │ └── login_view.dart
│ └── routes
│ ├── app_pages.dart
│ └── app_routes.dart
└── main.dart
创建控制器 controller
get create controller:register on login
终端信息
✓ File: register_controller.dart created successfully at path: lib/app/modules/login/controllers/register_controller.dart
✓ The Register has been added to binding at path: lib/app/modules/login/bindings/login_binding.dart'
Time: 545 Milliseconds
目录结构
.
├── app
│ ├── data
│ ├── modules
│ │ ├── home
│ │ │ ├── bindings
│ │ │ │ └── home_binding.dart
│ │ │ ├── controllers
│ │ │ │ └── home_controller.dart
│ │ │ └── views
│ │ │ └── home_view.dart
│ │ └── login
│ │ ├── bindings
│ │ │ └── login_binding.dart
│ │ ├── controllers
│ │ │ ├── login_controller.dart
│ │ │ └── register_controller.dart
│ │ └── views
│ │ └── login_view.dart
│ └── routes
│ ├── app_pages.dart
│ └── app_routes.dart
└── main.dart
创建视图 view
get create view:register on login
终端信息
✓ File: register_view.dart created successfully at path: lib/app/modules/login/views/register_view.dart
Time: 500 Milliseconds
目录结构
.
├── app
│ ├── data
│ ├── modules
│ │ ├── home
│ │ │ ├── bindings
│ │ │ │ └── home_binding.dart
│ │ │ ├── controllers
│ │ │ │ └── home_controller.dart
│ │ │ └── views
│ │ │ └── home_view.dart
│ │ └── login
│ │ ├── bindings
│ │ │ └── login_binding.dart
│ │ ├── controllers
│ │ │ ├── login_controller.dart
│ │ │ └── register_controller.dart
│ │ └── views
│ │ ├── login_view.dart
│ │ └── register_view.dart
│ └── routes
│ ├── app_pages.dart
│ └── app_routes.dart
└── main.dart
创建状态控制器 provider
get create provider:register on login
终端信息
✓ File: register_provider.dart created successfully at path: lib/app/modules/login/providers/register_provider.dart
Time: 463 Milliseconds
目录结构
.
├── app
│ ├── data
│ ├── modules
│ │ ├── home
│ │ │ ├── bindings
│ │ │ │ └── home_binding.dart
│ │ │ ├── controllers
│ │ │ │ └── home_controller.dart
│ │ │ └── views
│ │ │ └── home_view.dart
│ │ └── login
│ │ ├── bindings
│ │ │ └── login_binding.dart
│ │ ├── controllers
│ │ │ ├── login_controller.dart
│ │ │ └── register_controller.dart
│ │ ├── providers
│ │ │ └── register_provider.dart
│ │ └── views
│ │ ├── login_view.dart
│ │ └── register_view.dart
│ └── routes
│ ├── app_pages.dart
│ └── app_routes.dart
└── main.dart
生成国际化文件 locales
在 assets/locales 目录创建 json 格式的语言文件
assets/locales/en_US.json
{
"buttons": {
"login": "Login",
"sign_in": "Sign-in",
"logout": "Logout"
}
}
assets/locales/zh_CN.json
{
"buttons": {
"login": "登录",
"sign_in": "注册",
"logout": "注销"
}
}
运行
get generate locales assets/locales
终端信息
✓ File: locales.g.dart created successfully at path: lib/generated/locales.g.dart
✓ locale files generated successfully.
Time: 514 Milliseconds
输出
class AppTranslation {
static Map<String, Map<String, String>> translations = {
'zh_CN': Locales.zh_CN,
'en_US': Locales.en_US,
};
}
class LocaleKeys {
LocaleKeys._();
static const buttons_login = 'buttons_login';
static const buttons_sign_in = 'buttons_sign_in';
static const buttons_logout = 'buttons_logout';
}
class Locales {
static const zh_CN = {
'buttons_login': '登录',
'buttons_sign_in': '注册',
'buttons_logout': '注销',
};
static const en_US = {
'buttons_login': 'Login',
'buttons_sign_in': 'Sign-in',
'buttons_logout': 'Logout',
};
}
目录结构
.
├── app
│ ├── data
│ ├── modules
│ │ ├── home
│ │ │ ├── bindings
│ │ │ │ └── home_binding.dart
│ │ │ ├── controllers
│ │ │ │ └── home_controller.dart
│ │ │ └── views
│ │ │ └── home_view.dart
│ │ └── login
│ │ ├── bindings
│ │ │ └── login_binding.dart
│ │ ├── controllers
│ │ │ ├── login_controller.dart
│ │ │ └── register_controller.dart
│ │ ├── providers
│ │ │ └── register_provider.dart
│ │ └── views
│ │ ├── login_view.dart
│ │ └── register_view.dart
│ └── routes
│ ├── app_pages.dart
│ └── app_routes.dart
├── generated
│ └── locales.g.dart
└── main.dart
生成模型 model
在 assets/models 目录创建 json 格式的模型文件
assets/models/user.json
{
"name": "",
"age": 0,
"friends": ["", ""]
}
运行
# 方式一,生成带有 provider 的 model
get generate model on login with assets/models/user.json
# 方式二,生成不带有 provider 的 model
get generate model on login with assets/models/user.json --skipProvider
# 方式三,通过 json 格式的 URL 生成 model
get generate model on login from "https://api.github.com/users/CpdnCristiano"
终端信息
✓ File: user_model.dart created successfully at path: lib/app/modules/login/user_model.dart
✓ File: user_provider.dart created successfully at path: lib/app/modules/login/providers/user_provider.dart
输出
class User {
String? name;
int? age;
List<String>? friends;
User({this.name, this.age, this.friends});
User.fromJson(Map<String, dynamic> json) {
name = json['name'];
age = json['age'];
friends = json['friends'].cast<String>();
}
Map<String, dynamic> toJson() {
final data = <String, dynamic>{};
data['name'] = name;
data['age'] = age;
data['friends'] = friends;
return data;
}
}
目录结构
.
├── app
│ ├── data
│ ├── modules
│ │ ├── home
│ │ │ ├── bindings
│ │ │ │ └── home_binding.dart
│ │ │ ├── controllers
│ │ │ │ └── home_controller.dart
│ │ │ └── views
│ │ │ └── home_view.dart
│ │ └── login
│ │ ├── bindings
│ │ │ └── login_binding.dart
│ │ ├── controllers
│ │ │ ├── login_controller.dart
│ │ │ └── register_controller.dart
│ │ ├── providers
│ │ │ ├── register_provider.dart
│ │ │ └── user_provider.dart
│ │ ├── user_model.dart
│ │ └── views
│ │ ├── login_view.dart
│ │ └── register_view.dart
│ └── routes
│ ├── app_pages.dart
│ └── app_routes.dart
├── generated
│ └── locales.g.dart
└── main.dart
安装包 dependencies
运行
# 安装依赖包
get install http
# 安装多个依赖包
get install http path camera
终端信息
Installing package "http" …
✓ 'Package: http installed!
Running `flutter pub get` …
$ flutter pub get
Running "flutter pub get" in flutter_get_01... 1,566ms
Time: 4589 Milliseconds
安装包 dev_dependencies
运行
get install flutter_launcher_icons --dev
终端信息
The [--dev] is not necessary
Installing package "flutter_launcher_icons" …
✓ 'Package: flutter_launcher_icons installed!
Running `flutter pub get` …
$ flutter pub get
Running "flutter pub get" in flutter_get_01... 5.6s
Time: 8614 Milliseconds
移除包
# 移除依赖包
get remove http
# 移除多个依赖包
get remove http path camera
终端信息
Removing package: "http"
✓ Package: http removed!
Running `flutter pub get` …
$ flutter pub get
Running "flutter pub get" in flutter_get_01... 1,422ms
Time: 3797 Milliseconds
更新脚手架
get update
终端信息
Latest version of get_cli already installed
Time: 814 Milliseconds
查看版本号
get -v
终端信息
░██████╗░███████╗████████╗ ░█████╗░██╗░░░░░░██╗
██╔════╝░██╔════╝╚══██╔══╝ ██╔══██╗██║░░░░░░██║
██║░░██╗░█████╗░░░░░██║░░░ ██║░░╚═╝██║░░░░░░██║
██║░░╚██╗██╔══╝░░░░░██║░░░ ██║░░██╗██║░░░░░░██║
╚██████╔╝███████╗░░░██║░░░ ╚█████╔╝███████╗ ██║
░╚═════╝░╚══════╝░░░╚═╝░░░ ░╚════╝░╚══════╝ ╚═╝
Version: 1.7.1
Time: 188 Milliseconds
查看帮助
get help
终端信息
List available commands:
create:
controller: Generate controller
page: Use to generate pages
project: Use to generate new project
provider: Create a new Provider
screen: Generate new screen
view: Generate view
generate:
locales: Generate translation file from json files
model: generate Class model from json
help: Show this help
init: generate the chosen structure on an existing project:
install: Use to install a package in your project (dependencies):
remove: Use to remove a package in your project (dependencies):
sort: Sort imports and format dart files
update: To update GET_CLI
--version: Shows the current CLI version'
Time: 119 Milliseconds
运行程序
flutter run