VSCode定义Dart代码片段

xiaohai 2019-05-12 18:41:49 2584人围观 标签: 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文件中编写我们的自定义片段,格式如下:
  1. {
  2. "片段名": {
  3. "prefix": "快捷方式",
  4. "body": [
  5. "模板内容第一行",
  6. "模板内容第二行",
  7. ...
  8. ],
  9. "description": "描述"
  10. },
  11. ...
  12. }

如:

  1. {
  2. "StatelessWidget": {
  3. "prefix": "sl",
  4. "body": [
  5. "class ${1:WidgetName} extends StatelessWidget {",
  6. "\t@override",
  7. "\tWidget build(BuildContext context) {",
  8. "\t\treturn ${2:Container}();",
  9. "\t}",
  10. "}"
  11. ],
  12. "description": "StatelessWidget"
  13. }
  14. }
4、最后在这里给出在开发Flutter中常用的代码片段:
  1. {
  2. "StatelessWidget": {
  3. "prefix": "sl",
  4. "body": [
  5. "class ${1:WidgetName} extends StatelessWidget {",
  6. "\t@override",
  7. "\tWidget build(BuildContext context) {",
  8. "\t\treturn ${2:Container}();",
  9. "\t}",
  10. "}"
  11. ],
  12. "description": "StatelessWidget"
  13. },
  14. "StatefulWidget": {
  15. "prefix": "sf",
  16. "body": [
  17. "class ${1:WidgetName} extends StatefulWidget {",
  18. "\t@override",
  19. "\t_${1:WidgetName}State createState() => _${1:WidgetName}State();",
  20. "}\n",
  21. "class _${1:WidgetName}State extends State<${1:WidgetName}> {",
  22. "\t@override",
  23. "\tWidget build(BuildContext context) {",
  24. "\t\treturn ${2:Container}();",
  25. "\t}",
  26. "}"
  27. ],
  28. "description": "StatefulWidget"
  29. },
  30. "StatelessWidget with Scaffold": {
  31. "prefix": "sls",
  32. "body": [
  33. "class ${1:WidgetName} extends StatelessWidget {",
  34. "\t@override",
  35. "\tWidget build(BuildContext context) {",
  36. "\t\treturn ${Scaffold}(",
  37. "\t\t\tappBar: AppBar(",
  38. "\t\t\t\ttitle: Text('${1:WidgetName}'),",
  39. "\t\t\t\televation: 0.0,",
  40. "\t\t\t),${2}",
  41. "\t\t);",
  42. "\t}",
  43. "}"
  44. ],
  45. "description": "StatelessWidget with Scaffold"
  46. },
  47. "StatefulWidget with Scaffold": {
  48. "prefix": "sfs",
  49. "body": [
  50. "class ${1:WidgetName} extends StatefulWidget {",
  51. "\t@override",
  52. "\t_${1:WidgetName}State createState() => _${1:WidgetName}State();",
  53. "}\n",
  54. "class _${1:WidgetName}State extends State<${1:WidgetName}> {",
  55. "\t@override",
  56. "\tWidget build(BuildContext context) {",
  57. "\t\treturn ${Scaffold}(",
  58. "\t\t\tappBar: AppBar(",
  59. "\t\t\t\ttitle: Text('${1:WidgetName}'),",
  60. "\t\t\t\televation: 0.0,",
  61. "\t\t\t),${2}",
  62. "\t\t);",
  63. "\t}",
  64. "}"
  65. ],
  66. "description": "StatefulWidget with Scaffold"
  67. },
  68. "InheritedWidget": {
  69. "prefix": "ih",
  70. "body": [
  71. "class ${1:WidgetName} extends InheritedWidget {",
  72. "\tfinal Widget child;",
  73. "\t${2}",
  74. "\t${1:WidgetName}({",
  75. "\t\tthis.child,",
  76. "\t\t${2}",
  77. "\t}) : super(child: child);\n",
  78. "\tstatic ${1:WidgetName} of(BuildContext context) =>",
  79. "\t\t\tcontext.inheritFromWidgetOfExactType(${1:WidgetName});\n",
  80. "\t@override",
  81. "\tbool updateShouldNotify(${1:WidgetName} oldWidget) {",
  82. "\t\treturn true;",
  83. "\t}",
  84. "}"
  85. ],
  86. "description": "InheritedWidget"
  87. },
  88. "setState": {
  89. "prefix": "ss",
  90. "body": [
  91. "setState(() {${1}});",
  92. ],
  93. "description": "setState"
  94. },
  95. "build": {
  96. "prefix": "build",
  97. "body": [
  98. "@override",
  99. "Widget build(BuildContext context) {",
  100. "\treturn ${1:Container}(${2});",
  101. "}",
  102. ],
  103. "description": "Build Method"
  104. },
  105. }