一個幫助開發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_page
bash
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;
}
}
複製代碼
有小夥伴可能會疑惑,fluct create
運行以後會發現未找到命令,可能你使用了flutter pub global activate fluct
命令激活,這個時候,咱們可使用flutter pub run fluct create
運行
最後,但願你們喜歡這個工具,並關注我,瞭解更多關於
Flutter/Dart開發