VSCode定义Dart代码片段

xiaohai 2019-05-12 18:41:49 1769人围观 标签: Dart  VSCode 
简介很多时候我们编写代码的过程中会发现,有些代码片段我们会重复的使用,如果我们重头到位的敲一遍会感觉有点浪费时间,所以VSCode给我们提供了一个很好的功能,就是自定义代码片段,本文主要介绍VSCode中如何定义Dart代码片段。

  本文我们主要介绍如何在VSCode中给Dart定义代码片段,其实操作还是比较简单。操作步骤如下:

1、Ctrl+Shift+P快速打开命令工具,并搜索 snippets,并选择Configure User Snippets

image.png

2、选择dart.json

image.png

3、在dart.json文件中编写我们的自定义片段,格式如下:
{ "片段名": { "prefix": "快捷方式", "body": [ "模板内容第一行", "模板内容第二行", ... ], "description": "描述" }, ... }

如:

{ "StatelessWidget": { "prefix": "sl", "body": [ "class ${1:WidgetName} extends StatelessWidget {", "\t@override", "\tWidget build(BuildContext context) {", "\t\treturn ${2:Container}();", "\t}", "}" ], "description": "StatelessWidget" } }
4、最后在这里给出在开发Flutter中常用的代码片段:
{ "StatelessWidget": { "prefix": "sl", "body": [ "class ${1:WidgetName} extends StatelessWidget {", "\t@override", "\tWidget build(BuildContext context) {", "\t\treturn ${2:Container}();", "\t}", "}" ], "description": "StatelessWidget" }, "StatefulWidget": { "prefix": "sf", "body": [ "class ${1:WidgetName} extends StatefulWidget {", "\t@override", "\t_${1:WidgetName}State createState() => _${1:WidgetName}State();", "}\n", "class _${1:WidgetName}State extends State<${1:WidgetName}> {", "\t@override", "\tWidget build(BuildContext context) {", "\t\treturn ${2:Container}();", "\t}", "}" ], "description": "StatefulWidget" }, "StatelessWidget with Scaffold": { "prefix": "sls", "body": [ "class ${1:WidgetName} extends StatelessWidget {", "\t@override", "\tWidget build(BuildContext context) {", "\t\treturn ${Scaffold}(", "\t\t\tappBar: AppBar(", "\t\t\t\ttitle: Text('${1:WidgetName}'),", "\t\t\t\televation: 0.0,", "\t\t\t),${2}", "\t\t);", "\t}", "}" ], "description": "StatelessWidget with Scaffold" }, "StatefulWidget with Scaffold": { "prefix": "sfs", "body": [ "class ${1:WidgetName} extends StatefulWidget {", "\t@override", "\t_${1:WidgetName}State createState() => _${1:WidgetName}State();", "}\n", "class _${1:WidgetName}State extends State<${1:WidgetName}> {", "\t@override", "\tWidget build(BuildContext context) {", "\t\treturn ${Scaffold}(", "\t\t\tappBar: AppBar(", "\t\t\t\ttitle: Text('${1:WidgetName}'),", "\t\t\t\televation: 0.0,", "\t\t\t),${2}", "\t\t);", "\t}", "}" ], "description": "StatefulWidget with Scaffold" }, "InheritedWidget": { "prefix": "ih", "body": [ "class ${1:WidgetName} extends InheritedWidget {", "\tfinal Widget child;", "\t${2}", "\t${1:WidgetName}({", "\t\tthis.child,", "\t\t${2}", "\t}) : super(child: child);\n", "\tstatic ${1:WidgetName} of(BuildContext context) =>", "\t\t\tcontext.inheritFromWidgetOfExactType(${1:WidgetName});\n", "\t@override", "\tbool updateShouldNotify(${1:WidgetName} oldWidget) {", "\t\treturn true;", "\t}", "}" ], "description": "InheritedWidget" }, "setState": { "prefix": "ss", "body": [ "setState(() {${1}});", ], "description": "setState" }, "build": { "prefix": "build", "body": [ "@override", "Widget build(BuildContext context) {", "\treturn ${1:Container}(${2});", "}", ], "description": "Build Method" }, }