11import 'dart:ui' ;
2+ import 'package:cookethflow/core/utils/enums.dart' ;
23import 'package:cookethflow/features/models/canvas_models/canvas_object.dart' ;
34import 'package:flutter/material.dart' ;
45import 'package:uuid/uuid.dart' ;
@@ -10,20 +11,26 @@ class ConnectorObject extends CanvasObject {
1011 final String targetId;
1112 final Alignment sourceAlignment;
1213 final Alignment targetAlignment;
14+ final double thickness;
15+ final ConnectionType connectionType;
1316
1417 ConnectorObject ({
1518 required super .id,
19+ required super .color,
1620 required this .sourceId,
1721 required this .targetId,
1822 required this .sourceAlignment,
1923 required this .targetAlignment,
20- }) : super (color: Colors .black87, textDelta: null ); // Connectors have a fixed color and no text
24+ this .thickness = 2.0 ,
25+ this .connectionType = ConnectionType .solid,
26+ }) : super (textDelta: null );
2127
2228 @override
2329 Map <String , dynamic > toJson () {
2430 return {
2531 'object_type' : type,
2632 'id' : id,
33+ 'color' : color.value.toRadixString (16 ),
2734 'source_id' : sourceId,
2835 'target_id' : targetId,
2936 'source_alignment' : {
@@ -34,12 +41,15 @@ class ConnectorObject extends CanvasObject {
3441 'x' : targetAlignment.x,
3542 'y' : targetAlignment.y
3643 },
44+ 'thickness' : thickness,
45+ 'connection_type' : connectionType.name,
3746 };
3847 }
3948
4049 factory ConnectorObject .fromJson (Map <String , dynamic > json) {
4150 return ConnectorObject (
4251 id: json['id' ],
52+ color: Color (int .parse (json['color' ] as String , radix: 16 )),
4353 sourceId: json['source_id' ],
4454 targetId: json['target_id' ],
4555 sourceAlignment: Alignment (
@@ -50,6 +60,11 @@ class ConnectorObject extends CanvasObject {
5060 json['target_alignment' ]['x' ],
5161 json['target_alignment' ]['y' ],
5262 ),
63+ thickness: json['thickness' ] as double ? ?? 2.0 ,
64+ connectionType: ConnectionType .values.firstWhere (
65+ (e) => e.name == json['connection_type' ],
66+ orElse: () => ConnectionType .solid,
67+ ),
5368 );
5469 }
5570
@@ -58,8 +73,10 @@ class ConnectorObject extends CanvasObject {
5873 required String targetId,
5974 required Alignment sourceAlignment,
6075 required Alignment targetAlignment,
76+ Color color = Colors .black87,
77+ double thickness = 2.0 ,
78+ ConnectionType connectionType = ConnectionType .solid,
6179 }) {
62- // A connector cannot connect to its own points.
6380 if (sourceId == targetId) {
6481 throw StateError ('A connector cannot connect to itself.' );
6582 }
@@ -70,10 +87,12 @@ class ConnectorObject extends CanvasObject {
7087 targetId: targetId,
7188 sourceAlignment: sourceAlignment,
7289 targetAlignment: targetAlignment,
90+ color: color,
91+ thickness: thickness,
92+ connectionType: connectionType,
7393 );
7494 }
7595
76- // Connectors don't have a direct copyWith like shapes, as they are defined by their connections.
7796 @override
7897 ConnectorObject copyWith ({
7998 String ? textDelta,
@@ -82,34 +101,35 @@ class ConnectorObject extends CanvasObject {
82101 String ? targetId,
83102 Alignment ? sourceAlignment,
84103 Alignment ? targetAlignment,
104+ double ? thickness,
105+ ConnectionType ? connectionType,
85106 }) {
86107 return ConnectorObject (
87108 id: id,
109+ color: color ?? this .color,
88110 sourceId: sourceId ?? this .sourceId,
89111 targetId: targetId ?? this .targetId,
90112 sourceAlignment: sourceAlignment ?? this .sourceAlignment,
91113 targetAlignment: targetAlignment ?? this .targetAlignment,
114+ thickness: thickness ?? this .thickness,
115+ connectionType: connectionType ?? this .connectionType,
92116 );
93117 }
94118
95- // A connector's bounds are the line between its two points. For hit detection, this is handled differently.
96119 @override
97120 Rect getBounds () => Rect .zero;
98121
99- // Connectors are not moved directly; they follow their connected objects.
100122 @override
101123 ConnectorObject move (Offset delta) => this ;
102124
103- // Connectors cannot be resized.
104125 @override
105126 ConnectorObject resize (Offset newTopLeft, Offset newBottomRight) => this ;
106127
107- // Hit detection for a line is more complex than for a rect.
108128 @override
109129 bool intersectsWith (Offset point) {
110- // This requires calculating the distance from the point to the line segment .
111- // For simplicity, we'll skip complex hit detection for now .
112- // Interactions will be primarily through connection points on shapes .
130+ // This method is for hit detection on the connector line itself .
131+ // It requires access to the source and target objects to get the start and end points .
132+ // This logic is best handled in the WorkspaceProvider's onPanDown, where all objects are available .
113133 return false ;
114134 }
115- }
135+ }
0 commit comments