conversionProps function

Map conversionProps(
  1. Map props,
  2. String className
)

Implementation

Map conversionProps(Map props, String className) {
  List propList = props['propsList'];
  String yzType = props['yzType'];
  Map tempMap = {};
  List dataList = [];
  propList.forEach((element) {
    // element 有 name expression 时有子节点 继续解析
    // 仅有 type 和 value 时为叶子节点
    Map expressionMap = element['expression'];
    Map nameMap = element['name'];
    String type = element['type'];
    var value = element['value'];

    if (yzType != null) {
      tempMap['type'] = yzType;
    }

    if (value != null) {
      /// [Text]
      if (className == 'Text') {
        tempMap['data'] = value;

        /// [BorderRadius]
      } else if (className == 'BorderRadius') {
        if (yzType == 'circular') {
          tempMap['radius'] = '$value';
        }

        /// [Radius]
      } else if (className == 'Radius') {
        if (yzType == 'circular') {
          tempMap['radius'] = '$value';
        }

        /// [Image]
      } else if (className == 'Image') {
        if (yzType != null && yzType == 'asset') {
          tempMap['name'] = '$value';
        } else if (yzType != null && yzType == 'network') {
          tempMap['src'] = '$value';
        }
      } else {
        tempMap['dataList'] = dataList;
        dataList.add(value);
      }
    }

    if (expressionMap == null) return {};

    // 被调用者名称
    Map callee = expressionMap['callee'];
    String calleeName = '';
    if (callee != null) {
      Map calleeObject = callee['object'];
      if (calleeObject != null) {
        calleeName = calleeObject['name'];
      }
    }

    if (expressionMap['widgetName'] != null) {
      // 这里解析具体方法,
      /// 具体方法具体解析,如[padding]
      Map propsMap = expressionMap['props'];
      // if (expressionMap['widgetName'] == 'fromLTRB') {
      //   tempMap[nameMap['name']] = conversionPadding(propsMap);
      // }

      if (calleeName != null && calleeName == 'EdgeInsets') {
        tempMap[nameMap['name']] = conversionPadding(propsMap);
      }

      /// [color] 特殊处理
      else if (nameMap['name'] == 'color' &&
          expressionMap['widgetName'] == 'Color') {
        tempMap['color'] = conversionColor(propsMap);
      }

      /// [AssetImage NetworkImage] 特殊处理
      else if (expressionMap['widgetName'] == 'NetworkImage' ||
          expressionMap['widgetName'] == 'AssetImage') {
        tempMap[nameMap['name']] =
            conversionImageProvider(propsMap, expressionMap['widgetName']);
      }

      /// [style]
      else if (nameMap['name'] == 'style' &&
          expressionMap['widgetName'] == 'TextStyle') {
        tempMap['style'] = conversionStyle(propsMap);
      } else {
        /// 一般情况
        tempMap[nameMap['name']] = {
          'widgetName': expressionMap['widgetName'],
          'props': conversionProps(propsMap, expressionMap['widgetName'])
        };
      }
    }
    // 参数是列表
    else if (expressionMap['list'] != null) {
      List funcList = expressionMap['list'];
      if (nameMap['name'] == 'children') {}

      List resultList = [];

      /// 如果参数是方法,则继续解析
      /// 如果参数是基本数据类型,那么直接赋值
      /// 约定 如果是数据,需要转成json字符串

      if (funcList.length > 0) {
        if (funcList.first['type'] == 'StringLiteral') {
          /// 如果是 基本数据类型的 list,必须将list转为字符串
          funcList.forEach((element) {
            resultList.add('${element['value']}');
          });
          tempMap[nameMap['name']] = jsonEncode(resultList);
        } else {
          funcList.forEach((element) {
            resultList.add(conversionFunction(element));
          });
          tempMap[nameMap['name']] = resultList;
        }
      }
    } else if (expressionMap['type'] != null) {
      if (expressionMap['type'] == 'PrefixedIdentifier') {
        // tempMap['value'] = expressionMap['identifier']['name'];
        // tempMap['prefix'] = expressionMap['prefix']['name'];
        tempMap[nameMap['name']] = expressionMap['identifier']['name'];
      }

      /// 当expressionMap为value时
      // {
      //   "name": {
      //     "type": "Identifier",
      //     "name": "height"
      //   },
      //   "expression": {
      //     "type": "IntegerLiteral",
      //     "value": 200
      //   }
      // }
      else if (expressionMap['type'] == 'StringLiteral' ||
          expressionMap['type'] == 'IntegerLiteral' ||
          expressionMap['type'] == 'StringLiteral') {
        tempMap[nameMap['name']] = '${expressionMap['value']}';
      }
    }
  });
  return tempMap;
}