Flutter 代码片段

vscode

摘自宁皓网

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
{
"StatelessWidget": {
"prefix": "slw",
"body": [
"import 'package:flutter/material.dart';",
"",
"class ${1:WidgetName} extends StatelessWidget {",
"\t@override",
"\tWidget build(BuildContext context) {",
"\t\treturn ${2:Container}();",
"\t}",
"}"
],
"description": "StatelessWidget"
},
"StatefulWidget": {
"prefix": "sfw",
"body": [
"import 'package:flutter/material.dart';",
"",
"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": "slws",
"body": [
"import 'package:flutter/material.dart';",
"",
"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),${2}",
"\t\t);",
"\t}",
"}"
],
"description": "StatelessWidget with Scaffold"
},
"StatefulWidget with Scaffold": {
"prefix": "sfws",
"body": [
"import 'package:flutter/material.dart';",
"",
"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),${2}",
"\t\t);",
"\t}",
"}"
],
"description": "StatefulWidget with Scaffold"
},
"InheritedWidget": {
"prefix": "ihw",
"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"
},
"JsonSerializable Import": {
"prefix": "jsli",
"body": [
"import 'package:json_annotation/json_annotation.dart';",
"part '${1:name}.g.dart';",
"",
"@JsonSerializable(explicitToJson: true)",
"class ${2:Name} {",
" ${3}",
"",
" ${2:Name}({",
" ${4}",
" });",
"",
" factory ${2:Name}.fromJson(Map<String, dynamic> json) {",
" return _$${2:Name}FromJson(json);",
" }",
"",
" Map<String, dynamic> toJson() => _$${2:Name}ToJson(this);",
"}"
],
"description": "JsonSerializable Import"
},
"JsonSerializable": {
"prefix": "jsl",
"body": [
"@JsonSerializable(explicitToJson: true)",
"class ${1:Name} {",
" ${2}",
"",
" ${1:Name}({",
" ${3}",
" });",
"",
" factory ${1:Name}.fromJson(Map<String, dynamic> json) {",
" return _$${1:Name}FromJson(json);",
" }",
"",
" Map<String, dynamic> toJson() => _$${1:Name}ToJson(this);",
"}"
],
"description": "JsonSerializable Class"
}
}

android studio

Optimizing your time as a Flutter Developer with Live Templates

生成dart实体类

快捷键 jsl

Template Text
1
2
3
4
5
6
7
8
9
10
11
12
13
14
@JsonSerializable(explicitToJson: true)
class $Name$ {
$END$

$Name$({

});

factory $Name$.fromJson(Map<String, dynamic> json) {
return _$$$Name$FromJson(json);
}

Map<String, dynamic> toJson() => _$$$Name$ToJson(this);
}
1
2
3
4
5
6
 <template name="jsl" value="@JsonSerializable(explicitToJson: true)&#10;class $Name$ {&#10;  $END$&#10;&#10;  $Name$({&#10;    &#10;  });&#10;  &#10;  factory $Name$.fromJson(Map&lt;String, dynamic&gt; json) {&#10;    return _$$$Name$FromJson(json);&#10;  }&#10;&#10;  Map&lt;String, dynamic&gt; toJson() =&gt; _$$$Name$ToJson(this);&#10;}" description="生成dart实体类" toReformat="false" toShortenFQNames="true">
<variable name="Name" expression="" defaultValue="" alwaysStopAt="true" />
<context>
<option name="DART" value="true" />
</context>
</template>

生成dart实体类(带导入)

快捷键 jsli

Template Text
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
import 'package:json_annotation/json_annotation.dart';
part '$className$.g.dart';

@JsonSerializable(explicitToJson: true)
class $ClassName$ {
$END$

$ClassName$({

});

factory $ClassName$.fromJson(Map<String, dynamic> json) {
return _$$$ClassName$FromJson(json);
}

Map<String, dynamic> toJson() => _$$$ClassName$ToJson(this);
}
1
2
3
4
5
6
7
<template name="jsli" value="import 'package:json_annotation/json_annotation.dart';&#10;part '$className$.g.dart';&#10;&#10;@JsonSerializable(explicitToJson: true)&#10;class $ClassName$ {&#10;  $END$&#10;&#10;  $ClassName$({&#10;    &#10;  });&#10;  &#10;  factory $ClassName$.fromJson(Map&lt;String, dynamic&gt; json) {&#10;    return _$$$ClassName$FromJson(json);&#10;  }&#10;&#10;  Map&lt;String, dynamic&gt; toJson() =&gt; _$$$ClassName$ToJson(this);&#10;}" description="生成dart实体类(带导入)" toReformat="false" toShortenFQNames="true" deactivated="true">
<variable name="className" expression="fileNameWithoutExtension()" defaultValue="" alwaysStopAt="true" />
<variable name="ClassName" expression="capitalize(fileNameWithoutExtension())" defaultValue="" alwaysStopAt="true" />
<context>
<option name="DART" value="true" />
</context>
</template>