文档语言
pt_BR en_US – 此文件 zh_CN

GetX™ 框架的官方 CLI。

// To install:
pub global activate get_cli 
// (to use this add the following to system PATH: [FlutterSDKInstallDir]\bin\cache\dart-sdk\bin

flutter pub global activate get_cli

// To create a flutter project in the current directory:
// Note: By default it will take the folder's name as project name
// You can name the project with `get create project:my_project`
// If the name has spaces use `get create project:"my cool project"`
get create project

// To generate the chosen structure on an existing project:
get init

// To create a page:
// (Pages have controller, view, and binding)
// Note: you can use any name, ex: `get create page:login`
// Nota: use this option if the chosen structure was Getx_pattern
get create page:home

// To create a screen
// (Screens have controller, view, and binding)
// Note: you can use any name, ex: `get screen page:login`
// Nota: use this option if the chosen structure was CLEAN (by Arktekko)
get create screen:home 

// To create a new controller in a specific folder:
// Note: you don't need to reference the folder,
// Getx will search automatically for the home folder
// and add your controller there.
get create controller:dialogcontroller on home

// To create a new view in a specific folder:
// Note: you don't need to reference the folder,
// Getx will automatically search for the home folder
// and insert your view there.
get create view:dialogview on home

// To create a new provider in a specific folder:
get create provider:user on home

// To generate a localization file:
// Note: 'assets/locales' directory with your translation files in json format
get generate locales assets/locales

// To generate a class model:
// Note: 'assets/models/user.json' path of your template file in json format
// Note: on  == folder output file
// Getx will automatically search for the home folder
// and insert your class model there.
get generate model on home with assets/models/user.json

//to generate the model without the provider
get generate model on home with assets/models/user.json --skipProvider

//Note: the URL must return a json format
get generate model on home from "https://api.github.com/users/CpdnCristiano"

// To install a package in your project (dependencies):
get install camera

// To install several packages from your project:
get install http path camera

// To install a package with specific version:
get install path:1.6.4

// You can also specify several packages with version numbers

// To install a dev package in your project (dependencies_dev):
get install flutter_launcher_icons --dev

// To remove a package from your project:
get remove http

// To remove several packages from your project:
get remove http path

// To update CLI:
get update
// or `get upgrade`

// Shows the current CLI version:
get -v
// or `get -version`

// For help
get help

探索 CLI

让我们探索 CLI 中现有的命令

创建项目

  get create project

用于生成新项目,您可以选择 Flutterget_server,在创建默认目录后,它将运行下一个 get init 命令

初始化

  get init

请谨慎使用此命令,它将覆盖 lib 文件夹中的所有文件。
它允许您选择两种结构:getx_patternclean

创建页面

  get create page:name

此命令允许您创建模块,推荐给选择使用 getx_pattern 的用户。

它会创建 view、controller 和 binding 文件,并自动添加路由。

您可以在另一个模块中创建模块。

  get create page:name on other_module

现在创建新项目并使用 on 创建页面时,CLI 将使用 子页面

创建屏幕

  get create screen:name

create page 类似,但适合使用 Clean 的用户

创建控制器

  get create controller:dialog on your_folder

在特定文件夹中创建控制器。

使用选项
您现在可以创建模板文件,按照您喜欢的方式。

运行

  get create controller:auth with examples/authcontroller.dart on your_folder

或使用 URL
运行

  get create controller:auth with 'https://raw.githubusercontent.com/jonataslaw/get_cli/master/samples_file/controller.dart.example' on your_folder

输入

@import

class @controller extends GetxController {
  final  email = ''.obs;
  final  password = ''.obs;
  void login() {
  }

}

输出

import 'package:get/get.dart';

class AuthController extends GetxController {
  final email = ''.obs;
  final password = ''.obs;
  void login() {}
}

创建视图

  get create view:dialog on your_folder

在特定文件夹中创建视图

生成本地化文件

在 assets/locales 文件夹中创建 json 语言文件。

输入

pt_BR.json

{
  "buttons": {
    "login": "Entrar",
    "sign_in": "Cadastrar-se",
    "logout": "Sair",
    "sign_in_fb": "Entrar com o Facebook",
    "sign_in_google": "Entrar com o Google",
    "sign_in_apple": "Entrar com a  Apple"
  }
}

en_US.json

{
  "buttons": {
    "login": "Login",
    "sign_in": "Sign-in",
    "logout": "Logout",
    "sign_in_fb": "Sign-in with Facebook",
    "sign_in_google": "Sign-in with Google",
    "sign_in_apple": "Sign-in with Apple"
  }
}

运行

get generate locales assets/locales

输出

abstract class AppTranslation {

  static Map<String, Map<String, String>> translations = {
    'en_US' : Locales.en_US,
    'pt_BR' : Locales.pt_BR,
  };

}
abstract class LocaleKeys {
  static const buttons_login = 'buttons_login';
  static const buttons_sign_in = 'buttons_sign_in';
  static const buttons_logout = 'buttons_logout';
  static const buttons_sign_in_fb = 'buttons_sign_in_fb';
  static const buttons_sign_in_google = 'buttons_sign_in_google';
  static const buttons_sign_in_apple = 'buttons_sign_in_apple';
}

abstract class Locales {

  static const en_US = {
   'buttons_login': 'Login',
   'buttons_sign_in': 'Sign-in',
   'buttons_logout': 'Logout',
   'buttons_sign_in_fb': 'Sign-in with Facebook',
   'buttons_sign_in_google': 'Sign-in with Google',
   'buttons_sign_in_apple': 'Sign-in with Apple',
  };
  static const pt_BR = {
   'buttons_login': 'Entrar',
   'buttons_sign_in': 'Cadastrar-se',
   'buttons_logout': 'Sair',
   'buttons_sign_in_fb': 'Entrar com o Facebook',
   'buttons_sign_in_google': 'Entrar com o Google',
   'buttons_sign_in_apple': 'Entrar com a  Apple',
  };

}

现在只需将此行添加到 GetMaterialApp 中

    GetMaterialApp(
      ...
      translationsKeys: AppTranslation.translations,
      ...
    )

生成模型示例

在 assets/models/user.json 中创建 json 模型文件

输入

{
  "name": "",
  "age": 0,
  "friends": ["", ""]
}

运行

get generate model on home with assets/models/user.json

输出

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 Map<String, dynamic> data = new Map<String, dynamic>();
    data['name'] = this.name;
    data['age'] = this.age;
    data['friends'] = this.friends;
    return data;
  }
}

文件类型分隔符

有一天,一位用户问我,是否可以更改文件的最终名称,他认为使用:my_controller_name.controller.dart 比 CLI 生成的默认名称:my_controller_name_controller. dart 更具可读性。考虑到像他一样的用户,我们添加了一个选项供您选择自己的分隔符,只需在您的 pubsepc.yaml 中添加此信息即可。

示例

get_cli:
  separator: "."

您的导入是否混乱?

为了帮助您整理导入,创建了一个新命令:get sort,除了整理导入外,该命令还将格式化您的 dart 文件。感谢 dart_style
使用 get sort 时,所有文件都会重命名,并带有 分隔符
要不重命名,请使用 --skipRename 标志。

您是否是那些宁愿使用相对导入而不是项目导入的人,请使用 --relative 选项。get_cli 将进行转换。

CLI 的国际化

CLI 现在有一个国际化系统。

将 CLI 翻译成您的语言

  1. translations 文件夹中创建一个包含您语言的新 json 文件
  2. 文件 复制键,并翻译值
  3. 提交您的 PR。

待办事项

  • 支持 customModels
  • 包含单元测试
  • 改进生成的结构
  • 添加备份系统

GitHub

查看 Github