flutter_graphite icon indicating copy to clipboard operation
flutter_graphite copied to clipboard

Add a new field to NodeInput class for displayed text

Open mikeryder opened this issue 2 years ago • 2 comments

For my use case, I would like to have a third field in NodeInput for the displayed text, possibly 'displayText'. The unique identifier, 'id' would be used to identify the Node. The 'displayText' would be displayed in the graph. For backwards compatibility, it could fall back to displaying the 'id' as the displayed text if 'displayText' is not populated.

Would you be open to a pull request with this change?

mikeryder avatar Aug 15 '21 14:08 mikeryder

@mikeryder

I think it would be better(for wide usage) to implement generic field payload inside NodeInput, and passthrough this data to NodeOutput. In this case, you will be able to hold any kind of data inside node (Strings, classes, HashMaps, Lists).

class NodeInput <T> {
  NodeInput({
    required this.id,
    required this.next,
    this.payload
  });

  final String id;
  final List<String> next;
  final T payload;

  factory NodeInput.fromJson(Map<String, dynamic> json) => NodeInput(
        id: json["id"] == null ? null : json["id"],
        next: List<String>.from(json["next"].map((x) => x)),
      );

  Map<String, dynamic> toJson() => {
        "id": id,
        "next": List<dynamic>.from(next.map((x) => x)),
      };
}

class NodeOutput<T> extends NodeInput<T> {
  NodeOutput({
    required String id,
    required List<String> next,
    this.anchorType,
    this.from,
    this.to,
    this.orientation,
    this.isAnchor = false,
    this.passedIncomes = const [],
    this.renderIncomes = const [],
    this.childrenOnMatrix,
    this.anchorMargin,
    this.payload,
  }) : super(id: id, next: next, payload: payload);

  AnchorType? anchorType;
  String? from;
  String? to;
  AnchorOrientation? orientation;
  AnchorMargin? anchorMargin;
  
  final T payload;

  bool isAnchor;
  List<String> passedIncomes = [];
  List<String> renderIncomes = [];
  int? childrenOnMatrix;
}

lempiy avatar Aug 15 '21 14:08 lempiy

Hi @lempiy,

Your solution is nice and solves an issue that I also ran into. I worked around the lack of Payload by creating a Map from the id to my "Payload". I had to created a small class to pass my "Payload" with the List<NodeInput>. Your solution is much more elegant than my workaround.

Your solution does not solve the reason for my post. I would like two nodes to show the same text. Right now, the text and the id are the same, so if I set two nodes to have the same text, I assume assertions would fail and certainly the graph would not be able to be created correctly.

mikeryder avatar Aug 15 '21 20:08 mikeryder