基础库
Non puoi selezionare più di 25 argomenti Gli argomenti devono iniziare con una lettera o un numero, possono includere trattini ('-') e possono essere lunghi fino a 35 caratteri.
 
 
 
 
 

83 righe
1.9 KiB

  1. import 'package:flutter/material.dart';
  2. import 'package:zhiying_comm/zhiying_comm.dart';
  3. enum WidgetType {
  4. normal, // 正常状态
  5. skeleton, // 显示骨架
  6. }
  7. /* Widget生成工厂, 用于生成flutter端组件 */
  8. class WidgetFactory {
  9. static Map<String, WidgetCreater> widgetCreaters = Map();
  10. /**
  11. * @description: 注册组件
  12. * @param {name} 组件名,唯一
  13. * @return:
  14. */
  15. static void regist(String name, WidgetCreater creater) {
  16. if (!widgetCreaters.containsKey(name)) {
  17. widgetCreaters[name] = creater;
  18. }
  19. }
  20. /**
  21. * @description: 创建组件
  22. * @param {String} 组件注册的名字
  23. * @return:
  24. */
  25. static List<Widget> create(
  26. String name, {
  27. // 组件名
  28. Map<String, dynamic> model,
  29. bool isSliver, // 是否加载sliver组件
  30. }) {
  31. WidgetCreater creater;
  32. if (widgetCreaters.containsKey(name)) {
  33. creater = widgetCreaters[name];
  34. List<Widget> result = [];
  35. // if (type == WidgetType.normal)
  36. result = creater.createWidgets(model);
  37. // else
  38. // result = creater.createSkeleton(model);
  39. if (isSliver) {
  40. if (creater.isSliverChild()) {
  41. return result;
  42. } else {
  43. return result.map((widget) {
  44. return SliverToBoxAdapter(child: widget);
  45. }).toList();
  46. }
  47. } else {
  48. if (creater.isSliverChild()) {
  49. return _createEmpty(false);
  50. } else {
  51. return result;
  52. }
  53. }
  54. } else {
  55. print('${name} 未注册');
  56. }
  57. return _createEmpty(isSliver);
  58. }
  59. static List<Widget> _createEmpty(bool isSliver) {
  60. return isSliver
  61. ? [
  62. SliverToBoxAdapter(
  63. child: Container(height: 0),
  64. )
  65. ]
  66. : [];
  67. }
  68. /**
  69. * @description: 是否注册组件
  70. * @param {bool}
  71. * @return:
  72. */
  73. static bool hasRegisted(String name) {
  74. return widgetCreaters.containsKey(name);
  75. }
  76. }