dart.code-snippets 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280
  1. {
  2. "index代码片段": {
  3. "prefix": "tplindex",
  4. "body": [
  5. "import 'package:flutter/material.dart';",
  6. "import 'package:json_annotation/json_annotation.dart';",
  7. "import 'package:wisdom_cli/wisdom_cli.dart';",
  8. "import 'store.dart';",
  9. "import 'view.dart';",
  10. "part 'index.g.dart';",
  11. "///路由传递的参数",
  12. "@JsonSerializable(nullable: false)",
  13. "class ${TM_DIRECTORY/.*pages\\/(.)/${1:/upcase}/}PageArguments {",
  14. "final String id;",
  15. "${TM_DIRECTORY/.*pages\\/(.)/${1:/upcase}/}PageArguments({this.id});",
  16. "",
  17. "///",
  18. "///",
  19. "///",
  20. "///",
  21. "///",
  22. "///",
  23. "///",
  24. "//***以下为模板代码谨慎修改***",
  25. "factory ${TM_DIRECTORY/.*pages\\/(.)/${1:/upcase}/}PageArguments.fromJson(Map<String, dynamic> json) {",
  26. "return _\\$${TM_DIRECTORY/.*pages\\/(.)/${1:/upcase}/}PageArgumentsFromJson(json??{});",
  27. "}",
  28. "factory ${TM_DIRECTORY/.*pages\\/(.)/${1:/upcase}/}PageArguments.fromRouteArguments(BuildContext context) {",
  29. "return _\\$${TM_DIRECTORY/.*pages\\/(.)/${1:/upcase}/}PageArgumentsFromJson(",
  30. "ModalRoute.of(context).settings?.arguments",
  31. ");",
  32. "}",
  33. "Map<String, dynamic> toJson() => _\\$${TM_DIRECTORY/.*pages\\/(.)/${1:/upcase}/}PageArgumentsToJson(this);",
  34. "}",
  35. "///",
  36. "///",
  37. "///",
  38. "///",
  39. "///",
  40. "//***以下为模板代码谨慎修改***",
  41. "class ${TM_DIRECTORY/.*pages\\/(.)/${1:/upcase}/}Page extends StatelessWidget {",
  42. "const ${TM_DIRECTORY/.*pages\\/(.)/${1:/upcase}/}Page({Key key}) : super(key: key);",
  43. "@override",
  44. "Widget build(BuildContext context) {",
  45. "return MultiProvider(",
  46. "providers: [",
  47. "ChangeNotifierProvider(create: (_) => ${TM_DIRECTORY/.*pages\\/(.)/${1:/upcase}/}PageStore(context)),",
  48. "],",
  49. "child: ${TM_DIRECTORY/.*pages\\/(.)/${1:/upcase}/}PageView(),",
  50. ");",
  51. "}",
  52. "}",
  53. ],
  54. "description": "index.dart代码片段"
  55. },
  56. "store.dart代码片段": {
  57. "prefix": "tplstore",
  58. "body": [
  59. "import 'package:flutter/material.dart';",
  60. "import 'package:mobx/mobx.dart';",
  61. "import 'index.dart';",
  62. "part 'store.g.dart';",
  63. "class ${TM_DIRECTORY/.*pages\\/(.)/${1:/upcase}/}PageStore = _${TM_DIRECTORY/.*pages\\/(.)/${1:/upcase}/}PageStore with _$${TM_DIRECTORY/.*pages\\/(.)/${1:/upcase}/}PageStore, ChangeNotifier;",
  64. "abstract class _${TM_DIRECTORY/.*pages\\/(.)/${1:/upcase}/}PageStore with Store {",
  65. "///上下文(请勿修改)",
  66. "final BuildContext context;",
  67. "///路由参数(请勿修改)",
  68. "${TM_DIRECTORY/.*pages\\/(.)/${1:/upcase}/}PageArguments get arguments =>",
  69. "${TM_DIRECTORY/.*pages\\/(.)/${1:/upcase}/}PageArguments.fromRouteArguments(context);",
  70. "",
  71. "_${TM_DIRECTORY/.*pages\\/(.)/${1:/upcase}/}PageStore(this.context){",
  72. "///可在此处理进入页面前的操作,比如响应路由的传参进行赋值。",
  73. "}",
  74. "///",
  75. "//@observable",
  76. "//int value;",
  77. "}",
  78. ],
  79. "description": "store.dart代码片段"
  80. },
  81. "view.dart代码片段": {
  82. "prefix": "tplview",
  83. "body": [
  84. "import 'package:flutter/material.dart';",
  85. "import 'package:wisdom_cli/wisdom_cli.dart';",
  86. "import 'store.dart';",
  87. "//part 'components/list.dart';",
  88. "",
  89. "class ${TM_DIRECTORY/.*pages\\/(.)/${1:/upcase}/}PageView extends StatelessWidget {",
  90. "const ${TM_DIRECTORY/.*pages\\/(.)/${1:/upcase}/}PageView({Key key}) : super(key: key);",
  91. "@override",
  92. "Widget build(BuildContext context) {",
  93. "// final store = Provider.of<${TM_DIRECTORY/.*pages\\/([a-z])(.*)/${1:/upcase}${2}/}PageStore>(context, listen: false) ;",
  94. "// final style = WTheme.of(context);",
  95. "// final colorScheme = style.colorScheme;",
  96. "return Scaffold(",
  97. "///",
  98. ");",
  99. "}",
  100. "}",
  101. ],
  102. "description": "view.dart代码片段"
  103. },
  104. "component代码片段": {
  105. "prefix": "tplcomponent",
  106. "body": [
  107. "part of '../view.dart';",
  108. "",
  109. "class _${TM_FILENAME_BASE/(^.)|_(.{1})/${1:/upcase}${2:/upcase}/g}Component extends StatelessWidget {",
  110. "const _${TM_FILENAME_BASE/(^.)|_(.{1})/${1:/upcase}${2:/upcase}/g}Component({Key key}) : super(key: key);",
  111. "@override",
  112. "Widget build(BuildContext context) {",
  113. "// final store = Provider.of<${TM_DIRECTORY/.*pages\\/([a-z])(.*)\\/.*/${1:/upcase}${2}/}PageStore>(context, listen: false) ;",
  114. "// final style = WTheme.of(context);",
  115. "// final colorScheme = style.colorScheme;",
  116. "return Container();",
  117. "}",
  118. "}",
  119. ],
  120. "description": "component代码片段"
  121. },
  122. "globalcomponent代码片段": {
  123. "prefix": "tplglobalcomponent",
  124. "body": [
  125. "import 'package:flutter/material.dart';",
  126. "class ${TM_FILENAME_BASE/(^.)|_(.{1})/${1:/upcase}${2:/upcase}/g}Component extends StatelessWidget {",
  127. "const ${TM_FILENAME_BASE/(^.)|_(.{1})/${1:/upcase}${2:/upcase}/g}Component({Key key}) : super(key: key);",
  128. "@override",
  129. "Widget build(BuildContext context) {",
  130. "// final style = WTheme.of(context);",
  131. "// final colorScheme = theme.colorScheme;",
  132. "return Container();",
  133. "}",
  134. "}",
  135. ],
  136. "description": "全局component代码片段"
  137. },
  138. "globalstore代码片段": {
  139. "prefix": "tplglobalstore",
  140. "body": [
  141. "import 'package:mobx/mobx.dart';",
  142. "part '${TM_FILENAME_BASE}.g.dart';",
  143. "class ${TM_FILENAME_BASE/(^.)|_(.{1})/${1:/upcase}${2:/upcase}/g}GlobalStore = _${TM_FILENAME_BASE/(^.)|_(.{1})/${1:/upcase}${2:/upcase}/g}GlobalStore with _$${TM_FILENAME_BASE/(^.)|_(.{1})/${1:/upcase}${2:/upcase}/g}GlobalStore;",
  144. "abstract class _${TM_FILENAME_BASE/(^.)|_(.{1})/${1:/upcase}${2:/upcase}/g}GlobalStore with Store {",
  145. "@observable",
  146. "int value;",
  147. "}",
  148. ],
  149. "description": "全局store代码片段"
  150. },
  151. "model代码片段": {
  152. "prefix": "tplmodel",
  153. "body": [
  154. "import 'package:json_annotation/json_annotation.dart';",
  155. "part '${TM_FILENAME_BASE}.g.dart';",
  156. "@JsonSerializable(nullable: false)",
  157. "class ${TM_FILENAME_BASE/(^.)|_(.{1})/${1:/upcase}${2:/upcase}/g} {",
  158. "final String value;",
  159. "${TM_FILENAME_BASE/(^.)|_(.{1})/${1:/upcase}${2:/upcase}/g}({this.value});",
  160. "///",
  161. "///",
  162. "///",
  163. "///",
  164. "///",
  165. "///",
  166. "///",
  167. "//***以下为模板代码谨慎修改***",
  168. "factory ${TM_FILENAME_BASE/(^.)|_(.{1})/${1:/upcase}${2:/upcase}/g}.fromJson(Map<String, dynamic> json) =>",
  169. "_$${TM_FILENAME_BASE/(^.)|_(.{1})/${1:/upcase}${2:/upcase}/g}FromJson(json);",
  170. "Map<String, dynamic> toJson() => _$${TM_FILENAME_BASE/(^.)|_(.{1})/${1:/upcase}${2:/upcase}/g}ToJson(this);",
  171. "}",
  172. ],
  173. "description": "全局store代码片段"
  174. },
  175. "api single代码片段": {
  176. "prefix": "tplapisingle",
  177. "body": [
  178. "static Future<ResModel<${3:Type}>> ${1:apiName}(data) async {",
  179. "var res = await HttpUtil.instance.get(",
  180. "'$prefix/${2:path}',",
  181. "queryParameters: data,",
  182. ");",
  183. "",
  184. "return ResModel<${3:Type}>(",
  185. "json: res.data,",
  186. "to: (v) => v,",
  187. "from: (v) => v,",
  188. ");",
  189. "}",
  190. ],
  191. "description": "data只返回了一个单字段"
  192. },
  193. "api map代码片段": {
  194. "prefix": "tplapimap",
  195. "body": [
  196. "static Future<ResModel<${3:DataModel}>> ${1:apiName}(data) async {",
  197. "var res = await HttpUtil.instance.get(",
  198. "'$prefix/${2:path}',",
  199. "queryParameters: data,",
  200. ");",
  201. "",
  202. "return ResModel<${3:DataModel}>(",
  203. "json: res.data,",
  204. "to: (v) => v.toJson(),",
  205. "from: (v) => ${3:DataModel}.fromJson(v),",
  206. ");",
  207. "}",
  208. ],
  209. "description": "data返回了一个map"
  210. },
  211. "api list代码片段": {
  212. "prefix": "tplapilist",
  213. "body": [
  214. "static Future<ResModel<List<${3:ItemModel}>>> ${1:apiName}(data) async {",
  215. "var res = await HttpUtil.instance.get(",
  216. "'$prefix/${2:path}',",
  217. "queryParameters: data,",
  218. ");",
  219. "",
  220. "return ResModel<List<${3:ItemModel}>>(",
  221. "json: res.data,",
  222. "to: (v) => (v ?? []).map((f) => f.toJson()).toList(),",
  223. "from: (v) => (v ?? []).map<${3:ItemModel}>((f) => ${3:ItemModel}.fromJson(f)).toList(),",
  224. ");",
  225. "}",
  226. ],
  227. "description": "data返回了一个list"
  228. },
  229. "api paging代码片段": {
  230. "prefix": "tplapipaging",
  231. "body": [
  232. "static Future<ResModel<PagingModel<${3:ItemModel}>>> ${1:apiName}(data) async {",
  233. "var res = await HttpUtil.instance.get(",
  234. "'$prefix/${2:path}',",
  235. "queryParameters: data,",
  236. ");",
  237. "",
  238. "return ResModel<PagingModel<${3:ItemModel}>>(",
  239. "json: res.data,",
  240. "to: (v) => v.toJson(),",
  241. "from: (v) => PagingModel(",
  242. "json: v,",
  243. "to: (v) => v.toJson(),",
  244. "from: (v) => ${3:ItemModel}.fromJson(v),",
  245. "),",
  246. ");",
  247. "}",
  248. ],
  249. "description": "data返回了分页数据"
  250. },
  251. "getList代码片段": {
  252. "prefix": "tplGetList",
  253. "body": [
  254. "final scrollViewKey = GlobalKey<WisScrollViewState>();",
  255. "late Map params = {",
  256. "'pageNum': 1,",
  257. " 'pageSize': 20,",
  258. "};",
  259. "List<ItemModel> list = [];",
  260. "Future getList({bool isLoad = true}) async {",
  261. "if (!isLoad) params['pageNum'] = 1;",
  262. "var res = await NameApi.getList({",
  263. "...params,",
  264. "});",
  265. "final _data = res?.data;",
  266. "final _list = _data?.list ?? [];",
  267. "if ((_data?.total ?? 0) == 0) {",
  268. "scrollViewKey.currentState.isEmpty();",
  269. "} else if ((_data?.pages ?? 0) <= (_data?.pageNum ?? 0)) {",
  270. "scrollViewKey.currentState.isLast();",
  271. "} else {",
  272. "params['pageNum']++;",
  273. "}",
  274. "isLoad ? list.addAll(_list) : list = _list;",
  275. "notifyListeners();",
  276. "}",
  277. ],
  278. "description": "getList代码片段"
  279. },
  280. }