TextLayerData.fromMap constructor

TextLayerData.fromMap(
  1. Layer layer,
  2. Map<String, dynamic> map
)

Factory constructor for creating a TextLayerData instance from a Layer instance and a map.

Implementation

factory TextLayerData.fromMap(Layer layer, Map<String, dynamic> map) {
  /// Helper function to determine the text decoration style from a string.
  TextDecoration getDecoration(String decoration) {
    if (decoration.contains('combine')) {
      /// List to hold multiple text decoration styles if combined.
      List<TextDecoration> decorations = [];

      /// Adds line-through decoration if specified.
      if (decoration.contains('lineThrough')) {
        decorations.add(TextDecoration.lineThrough);
      }

      /// Adds overline decoration if specified.
      if (decoration.contains('overline')) {
        decorations.add(TextDecoration.overline);
      }

      /// Adds underline decoration if specified.
      if (decoration.contains('underline')) {
        decorations.add(TextDecoration.underline);
      }

      /// Combines multiple decorations into a single TextDecoration.
      return TextDecoration.combine(decorations);
    } else {
      /// Checks and returns line-through decoration.
      if (decoration.contains('lineThrough')) {
        return TextDecoration.lineThrough;
      }

      /// Checks and returns overline decoration.
      else if (decoration.contains('overline')) {
        return TextDecoration.overline;
      }

      /// Checks and returns underline decoration.
      else if (decoration.contains('underline')) {
        return TextDecoration.underline;
      }
    }

    /// Returns no decoration if none is specified.
    return TextDecoration.none;
  }

  /// Optional properties for text styling from the map.
  String? fontFamily = map['fontFamily'] as String?;
  double? wordSpacing = tryParseDouble(map['wordSpacing']);
  double? height = tryParseDouble(map['height']);
  double? letterSpacing = tryParseDouble(map['letterSpacing']);
  int? fontWeight = tryParseInt(map['fontWeight']);
  String? fontStyle = map['fontStyle'] as String?;
  String? decoration = map['decoration'] as String?;

  /// Constructs and returns a TextLayerData instance with properties derived
  /// from the map.
  return TextLayerData(
    flipX: layer.flipX,
    flipY: layer.flipY,
    enableInteraction: layer.enableInteraction,
    offset: layer.offset,
    rotation: layer.rotation,
    scale: layer.scale,
    text: map['text'] ?? '-',
    fontScale: map['fontScale'] ?? 1.0,
    textStyle: map['fontFamily'] != null
        ? TextStyle(
            fontFamily: fontFamily,
            height: height,
            wordSpacing: wordSpacing,
            letterSpacing: letterSpacing,
            decoration: decoration != null ? getDecoration(decoration) : null,
            fontStyle: fontStyle != null
                ? FontStyle.values
                    .firstWhere((element) => element.name == fontStyle)
                : null,
            fontWeight: fontWeight != null
                ? FontWeight.values
                    .firstWhere((element) => element.value == fontWeight)
                : null,
          )
        : null,
    colorMode: LayerBackgroundMode.values
        .firstWhere((element) => element.name == map['colorMode']),
    color: Color(map['color']),
    background: Color(map['background']),
    colorPickerPosition: map['colorPickerPosition'] ?? 0,
    align: TextAlign.values
        .firstWhere((element) => element.name == map['align']),
    customSecondaryColor: map['customSecondaryColor'] ?? false,
  );
}