|
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980 |
- import 'package:flutter/material.dart';
- import 'package:zhiying_comm/zhiying_comm.dart';
-
- enum WidgetType {
- normal, // 正常状态
- skeleton, // 显示骨架
- }
-
- /* Widget生成工厂, 用于生成flutter端组件 */
- class WidgetFactory {
- static Map<String, WidgetCreater> widgetCreaters = Map();
-
- /**
- * @description: 注册组件
- * @param {name} 组件名,唯一
- * @return:
- */
- static void regist(String name, WidgetCreater creater) {
- if (!widgetCreaters.containsKey(name)) {
- widgetCreaters[name] = creater;
- }
- }
-
- /**
- * @description: 创建组件
- * @param {String} 组件注册的名字
- * @return:
- */
- static List<Widget> create(
- String name, {
- // 组件名
- Map<String, dynamic> model,
- WidgetType type, // 组件显示类型,普通/骨架
- bool isSliver, // 是否加载sliver组件
- }) {
- WidgetCreater creater;
- if (widgetCreaters.containsKey(name)) {
- creater = widgetCreaters[name];
- List<Widget> result = [];
- if (type == WidgetType.normal)
- result = creater.createWidgets(model);
- else
- result = creater.createSkeleton(model);
- if (isSliver) {
- if (creater.isSliverChild()) {
- return result;
- } else {
- return result.map((widget) {
- return SliverToBoxAdapter(child: widget);
- }).toList();
- }
- } else {
- if (creater.isSliverChild()) {
- return _createEmpty();
- } else {
- return result;
- }
- }
- }
- return _createEmpty();
- }
-
- static List<Widget> _createEmpty() {
- const bool inProduction = const bool.fromEnvironment("dart.vm.product");
- if (!inProduction) {
- return [];
- } else {
- return [EmptyPage()];
- }
- }
-
- /**
- * @description: 是否注册组件
- * @param {bool}
- * @return:
- */
- static bool hasRegisted(String name) {
- return widgetCreaters.containsKey(name);
- }
- }
|