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开发

相关文章
相关标签/搜索