Flutter自制工具之fluct建立文件神器

fluct

一個幫助開發Flutter應用程序的工具
.----------------------------------------------
| github地址:
| github.com/rhymelph/fl…
| pub地址:
| pub.dev/packages/fl…
`----------------------------------------------git

安裝

該工具無需添加到依賴項中,咱們只須要激活便可,使用以下命令:github

$ pub global activate fluct
# 或者
$ flutter pub global activate fluct
複製代碼

使用

fluct 目前只有一個create命令,用於建立文件及widget,文件名按Dart文件命名規則指定的單詞與單詞之間添加下劃線,並沒有需指定.dart後綴,例如:index_pagebash

fluct create

Flutter開發過程當中,咱們建立文件是必須的,而AS自帶的建立文件,並無自動的生成相關的內容,這會讓開發者很是的苦惱,類名還須要本身手動敲的話,而該命令,直接能夠一步到位。app

當運行此命令後,命令行會輸出如下內容less

Help Flutter application create a new file

Usage: fluct create [arguments] <path>
-h, --help            Print this usage information.
-t, --type            
          [custom]    Create a new file about custom widget in 'fluct.yaml'
                      建立自定義widget的文件,-a 爲 指定你在‘fluct.yaml’文件聲明的指令
          [stful]     Create a new file about StatefulWidget
                      建立StatefulWidget文件
          [stless]    Create a new file about StatelessWidget
                      建立StatelessWidget文件

-a, --arg             create a new file about your custom widget use arg in 'fluct.yaml'
                      使用你在'fluct.yaml'聲明的指令

Run "fluct help" to see global options.
複製代碼

能夠看到,該命令輸出的內容是簡單易懂的,咱們來簡單使用一下吧。ide

簡單使用

建立IndexPage頁面,繼承自StatefulWidget,可使用以下命令:svg

$ fluct create -t stful ./index_page
Create a new file about StatefulWidget
create class IndexPage
create success
exit 0
複製代碼

運行成功以後,咱們會在項目下找到index_page.dart文件,內容爲:工具

import 'package:flutter/material.dart';

class IndexPage extends StatefulWidget {
  @override
  _IndexPageState createState() => _IndexPageState();
}

class _IndexPageState extends State<IndexPage> {
  @override
  Widget build(BuildContext context) {
    return Container();
  }
}
複製代碼

固然,你也能夠指定哪一個文件夾,例如,我要在./lib/src/page 文件夾下建立IndexPage,使用以下命令ui

$ fluct create -t stful ./lib/src/page/index_page
複製代碼

自定義內容的文件

在開始以前,咱們須要在項目根目錄下新建一個fluct.yaml文件,由於fluct create -t custom命令會找到它,內容以下:this

inh: | import 'package:flutter/material.dart'; class $NAME$ extends InheritedWidget { const $NAME$({ Key key, @required Widget child, }) : assert(child != null), super(key: key, child: child); static $NAME$ of(BuildContext context) { return context.dependOnInheritedWidgetOfExactType(aspect: $NAME$) as $NAME$; } @override bool updateShouldNotify($NAME$ old) { return false; } } 複製代碼

這裏,我聲明瞭inh命令,而後運行這個命令以後會在生成文件的時候添加inh對應的值,內容中咱們值得注意的是$NAME$佔位符,該字符串會被替換成根據文件名生成的內容,例如:index_page 會插入IndexPage$NAME$佔位符中,最後,咱們運行如下命令:

$ fluct create -t custom -a inh ./index_inherited
複製代碼

運行成功以後,咱們可以在根目錄下找到index_inherited.dart文件,內容也是對應的自定義內容

import 'package:flutter/material.dart';

class IndexInherited extends InheritedWidget {
  const IndexInherited({
    Key key,
    @required Widget child,
  })  : assert(child != null),
        super(key: key, child: child);

  static IndexInherited of(BuildContext context) {
    return context.dependOnInheritedWidgetOfExactType(aspect: IndexInherited) as IndexInherited;
  }

  @override
  bool updateShouldNotify(IndexInherited old) {
    return false;
  }
}
複製代碼

Flutter中運行命令

有小夥伴可能會疑惑,fluct create運行以後會發現未找到命令,可能你使用了flutter pub global activate fluct命令激活,這個時候,咱們可使用flutter pub run fluct create運行

最後,但願你們喜歡這個工具,並關注我,瞭解更多關於Flutter/Dart開發

相關文章
相關標籤/搜索