addItem method

void addItem({
  1. required String type,
  2. required String label,
  3. required String section,
  4. required String value,
  5. required String prefix,
  6. required String urlString,
  7. Map<String, dynamic>? properties,
})

Implementation

void addItem({required String type,
  required String label,
  required String section,
  required String value,
  required String prefix,
  required String urlString,
  Map<String, dynamic>? properties}) {
  clearSelection();

  List<String> itemLabels = [
    'Header Text',
    'SubHeader Text',
    'SubBody Text',
    'Footer Text',
  ];

  ///check the devPrintBlue and change this with showSnackbar

  // Check if item already exists
  if (items
      .any((item) => item.label == label && !itemLabels.contains(label))) {
    devPrintBlue('This component already exists in the canvas');
    return;
  }

  final sectionY = sectionYPositions[section]!;
  const size = Size(120, 30);
  // final size = componentSizes[label] ?? const Size(120, 30);

  Offset initialPos = Offset(20, sectionY + 10);

  // Create properties with custom values if provided
  PrintDesignPackageProperties itemProperties = PrintDesignPackageProperties(
    fontSize: properties?['font_size']?.toDouble() ?? 10.0,
    fontWeight:
    properties?['font_weight']?.toInt() ?? FontWeight.normal.value,
    textAlign: properties?['text_align'] ?? TextAlign.left.toString(),
    textColor: properties?['text_color']?.toInt() ?? Colors.black.value,
    isPrefixNeeded: properties?['is_prefix_needed'] ?? false,
  );

  PrinterDesignerPackageListModel newItem = PrinterDesignerPackageListModel(
    id: UniqueKey().toString(),
    type: type,
    label: label,
    value: value,
    prefix: prefix,
    imageUrl: urlString,
    positionX: initialPos.dx,
    positionY: initialPos.dy,
    height: size.height,
    width: size.width,
    section: section,
    isSelected: true,
    properties: itemProperties,
  );

  Offset? placementPosition =
  _calculatePlacementPosition(newItem, initialPos, null);

  if (placementPosition != null) {
    items.add(PrinterDesignerPackageListModel(
      id: UniqueKey().toString(),
      type: type,
      label: label,
      value: value,
      prefix: prefix,
      imageUrl: urlString,
      positionX: placementPosition.dx,
      positionY: placementPosition.dy,
      height: size.height,
      width: size.width,
      section: section,
      isSelected: true,
      properties: itemProperties,
    ));
  } else {
    devPrintBlue("Could not find space in the $section section for: $label");
  }
}