基础库
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 

81 lines
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. WidgetType type, // 组件显示类型,普通/骨架
  30. bool isSliver, // 是否加载sliver组件
  31. }) {
  32. WidgetCreater creater;
  33. if (widgetCreaters.containsKey(name)) {
  34. creater = widgetCreaters[name];
  35. List<Widget> result = [];
  36. if (type == WidgetType.normal)
  37. result = creater.createWidgets(model);
  38. else
  39. result = creater.createSkeleton(model);
  40. if (isSliver) {
  41. if (creater.isSliverChild()) {
  42. return result;
  43. } else {
  44. return result.map((widget) {
  45. return SliverToBoxAdapter(child: widget);
  46. }).toList();
  47. }
  48. } else {
  49. if (creater.isSliverChild()) {
  50. return _createEmpty();
  51. } else {
  52. return result;
  53. }
  54. }
  55. }
  56. return _createEmpty();
  57. }
  58. static List<Widget> _createEmpty() {
  59. const bool inProduction = const bool.fromEnvironment("dart.vm.product");
  60. if (!inProduction) {
  61. return [];
  62. } else {
  63. return [EmptyPage()];
  64. }
  65. }
  66. /**
  67. * @description: 是否注册组件
  68. * @param {bool}
  69. * @return:
  70. */
  71. static bool hasRegisted(String name) {
  72. return widgetCreaters.containsKey(name);
  73. }
  74. }