generateJsonString method

String generateJsonString()

Implementation

String generateJsonString() {
  final sortedVisibleHeaders = tableVisibleHeaders.toList()
    ..sort((a, b) => (a['index'] as int).compareTo(b['index'] as int));

  final List<Map<String, dynamic>> headersWithProperties =
  sortedVisibleHeaders.map((table) {
    return {
      'index': table['index'],
      'label': table['label'],
      'name': table['name'],
      'width': table['width'] ?? 70,
      'type': table['type'],
      'alignment': table['alignment'],
      'value': table['value'],
    };
  }).toList();

  Map<String, dynamic> section = {
    'header': sectionYPositions['header'],
    'subHeader': sectionYPositions['subHeader'],
    'body': sectionYPositions['body'],
    'subBody': sectionYPositions['subBody'],
    'footer': sectionYPositions['footer'],
  };

  final Map<String, dynamic> jsonData = {
    'section': section,
    'headers': {
      'tableRows': int.parse(noOfTableRows.value),
      'data': headersWithProperties,
    },
    'signUrl': signUrlUploaded.value,
    'items': items.map((item) {
      double positionY = item.positionY ?? 0;

      switch (item.section) {
        case 'subHeader':
          positionY -= headerHeight.value; //100
          break;
        case 'subBody':
          positionY -= (headerHeight.value +
              subHeaderHeight.value +
              bodyHeight.value); //510
          break;
        case 'footer':
          positionY -= (headerHeight.value +
              subHeaderHeight.value +
              bodyHeight.value +
              subBodyHeight.value); //685
          break;
      }

      return {
        'id': item.id,
        'label': item.label,
        'type': item.type,
        'value': item.value,
        'prefix': item.prefix,
        'imageUrl': item.imageUrl,
        'positionX': (item.positionX ?? 0) + 3,
        'positionY': positionY,
        'height': (item.height ?? 0),
        'width': (item.width ?? 0),
        'section': item.section,
        'customText': item.customText,
        'properties': {
          'font_size': (item.properties?.fontSize ?? 10),
          'font_weight': item.properties?.fontWeight ?? 400,
          'text_color': item.properties?.textColor ?? 4278190080,
          'text_align': item.properties?.textAlign ?? 'TextAlign.left',
          'direction': item.properties?.direction ?? 'horizontal',
          'is_prefix_needed': item.properties?.isPrefixNeeded ?? false,
          'prefix_font_size': (item.properties?.prefixFontSize ?? 10),
          'prefix_font_weight': item.properties?.prefixFontWeight ?? 400,
          'prefix_text_color': item.properties?.prefixTextColor ?? 4278190080,
        },
      };
    }).toList(),
  };

  // Update the current tab's data in tabsJson
  tabsJson[screen] = jsonData;

  // Create inventoryJson with all tabs
  final Map<String, dynamic> inventoryJson = {};
  for (String tabName in tabs) {
    inventoryJson[tabName] = tabsJson[tabName] ?? {};
  }

  return jsonEncode(inventoryJson);
}