@@ -5,6 +5,7 @@ import 'package:flutter/material.dart';
5
5
import 'package:simple_edge_detection/edge_detection.dart' ;
6
6
7
7
import 'edge_painter.dart' ;
8
+ import 'magnifier.dart' ;
8
9
import 'touch_bubble.dart' ;
9
10
10
11
class EdgeDetectionShape extends StatefulWidget {
@@ -33,10 +34,12 @@ class _EdgeDetectionShapeState extends State<EdgeDetectionShape> {
33
34
double top;
34
35
double left;
35
36
37
+ Offset currentDragPosition;
38
+
36
39
@override
37
40
void didChangeDependencies () {
38
41
double shortestSide = min (MediaQuery .of (context).size.width, MediaQuery .of (context).size.height);
39
- edgeDraggerSize = shortestSide / 8 ;
42
+ edgeDraggerSize = shortestSide / 12 ;
40
43
super .didChangeDependencies ();
41
44
}
42
45
@@ -50,19 +53,23 @@ class _EdgeDetectionShapeState extends State<EdgeDetectionShape> {
50
53
51
54
@override
52
55
Widget build (BuildContext context) {
53
- return Stack (
54
- children: [
55
- _getTouchBubbles (),
56
- CustomPaint (
57
- painter: EdgePainter (
58
- points: points,
59
- color: Theme .of (context).accentColor.withOpacity (0.5 )
60
- ),
61
- )
62
- ],
56
+ return Magnifier (
57
+ visible: currentDragPosition != null ,
58
+ position: currentDragPosition,
59
+ child: Stack (
60
+ children: [
61
+ _getTouchBubbles (),
62
+ CustomPaint (
63
+ painter: EdgePainter (
64
+ points: points,
65
+ color: Theme .of (context).accentColor.withOpacity (0.5 )
66
+ )
67
+ )
68
+ ],
69
+ )
63
70
);
64
71
}
65
-
72
+
66
73
void _calculateDimensionValues () {
67
74
top = 0.0 ;
68
75
left = 0.0 ;
@@ -85,17 +92,43 @@ class _EdgeDetectionShapeState extends State<EdgeDetectionShape> {
85
92
);
86
93
}
87
94
95
+ Offset _clampOffset (Offset givenOffset) {
96
+ double absoluteX = givenOffset.dx * renderedImageWidth;
97
+ double absoluteY = givenOffset.dy * renderedImageHeight;
98
+
99
+ return Offset (
100
+ absoluteX.clamp (0.0 , renderedImageWidth) / renderedImageWidth,
101
+ absoluteY.clamp (0.0 , renderedImageHeight) / renderedImageHeight
102
+ );
103
+ }
104
+
88
105
Widget _getTouchBubbles () {
89
106
points = [
90
- Offset (left + edgeDetectionResult.topLeft.dx * renderedImageWidth, top + edgeDetectionResult.topLeft.dy * renderedImageHeight),
91
- Offset (left + edgeDetectionResult.topRight.dx * renderedImageWidth, top + edgeDetectionResult.topRight.dy * renderedImageHeight),
92
- Offset (left + edgeDetectionResult.bottomRight.dx * renderedImageWidth, top + (edgeDetectionResult.bottomRight.dy * renderedImageHeight)),
93
- Offset (left + edgeDetectionResult.bottomLeft.dx * renderedImageWidth, top + edgeDetectionResult.bottomLeft.dy * renderedImageHeight),
94
- Offset (left + edgeDetectionResult.topLeft.dx * renderedImageWidth, top + edgeDetectionResult.topLeft.dy * renderedImageHeight),
107
+ Offset (
108
+ left + edgeDetectionResult.topLeft.dx * renderedImageWidth,
109
+ top + edgeDetectionResult.topLeft.dy * renderedImageHeight
110
+ ),
111
+ Offset (
112
+ left + edgeDetectionResult.topRight.dx * renderedImageWidth,
113
+ top + edgeDetectionResult.topRight.dy * renderedImageHeight
114
+ ),
115
+ Offset (
116
+ left + edgeDetectionResult.bottomRight.dx * renderedImageWidth,
117
+ top + (edgeDetectionResult.bottomRight.dy * renderedImageHeight)
118
+ ),
119
+ Offset (
120
+ left + edgeDetectionResult.bottomLeft.dx * renderedImageWidth,
121
+ top + edgeDetectionResult.bottomLeft.dy * renderedImageHeight
122
+ ),
123
+ Offset (
124
+ left + edgeDetectionResult.topLeft.dx * renderedImageWidth,
125
+ top + edgeDetectionResult.topLeft.dy * renderedImageHeight
126
+ ),
95
127
];
96
128
97
129
final Function onDragFinished = () {
98
- setState (() {});
130
+ currentDragPosition = null ;
131
+ setState (() {});
99
132
};
100
133
101
134
return Container (
@@ -108,8 +141,12 @@ class _EdgeDetectionShapeState extends State<EdgeDetectionShape> {
108
141
size: edgeDraggerSize,
109
142
onDrag: (position) {
110
143
setState (() {
111
- edgeDetectionResult.topLeft += _getNewPositionAfterDrag (
112
- position, renderedImageWidth, renderedImageHeight
144
+ currentDragPosition = Offset (points[0 ].dx, points[0 ].dy);
145
+ Offset newTopLeft = _getNewPositionAfterDrag (
146
+ position, renderedImageWidth, renderedImageHeight
147
+ );
148
+ edgeDetectionResult.topLeft = _clampOffset (
149
+ edgeDetectionResult.topLeft + newTopLeft
113
150
);
114
151
});
115
152
},
@@ -123,9 +160,13 @@ class _EdgeDetectionShapeState extends State<EdgeDetectionShape> {
123
160
size: edgeDraggerSize,
124
161
onDrag: (position) {
125
162
setState (() {
126
- edgeDetectionResult.topRight += _getNewPositionAfterDrag (
127
- position, renderedImageWidth, renderedImageHeight
163
+ Offset newTopRight = _getNewPositionAfterDrag (
164
+ position, renderedImageWidth, renderedImageHeight
165
+ );
166
+ edgeDetectionResult.topRight = _clampOffset (
167
+ edgeDetectionResult.topRight + newTopRight
128
168
);
169
+ currentDragPosition = Offset (points[1 ].dx, points[1 ].dy);
129
170
});
130
171
},
131
172
onDragFinished: onDragFinished
@@ -135,30 +176,38 @@ class _EdgeDetectionShapeState extends State<EdgeDetectionShape> {
135
176
),
136
177
Positioned (
137
178
child: TouchBubble (
138
- size: edgeDraggerSize,
139
- onDrag: (position) {
140
- setState (() {
141
- edgeDetectionResult.bottomRight + = _getNewPositionAfterDrag (
179
+ size: edgeDraggerSize,
180
+ onDrag: (position) {
181
+ setState (() {
182
+ Offset newBottomRight = _getNewPositionAfterDrag (
142
183
position, renderedImageWidth, renderedImageHeight
143
- );
144
- });
145
- },
146
- onDragFinished: onDragFinished
184
+ );
185
+ edgeDetectionResult.bottomRight = _clampOffset (
186
+ edgeDetectionResult.bottomRight + newBottomRight
187
+ );
188
+ currentDragPosition = Offset (points[2 ].dx, points[2 ].dy);
189
+ });
190
+ },
191
+ onDragFinished: onDragFinished
147
192
),
148
193
left: points[2 ].dx - (edgeDraggerSize / 2 ),
149
194
top: points[2 ].dy - (edgeDraggerSize / 2 )
150
195
),
151
196
Positioned (
152
197
child: TouchBubble (
153
- size: edgeDraggerSize,
154
- onDrag: (position) {
155
- setState (() {
156
- edgeDetectionResult.bottomLeft += _getNewPositionAfterDrag (
157
- position, renderedImageWidth, renderedImageHeight
158
- );
159
- });
160
- },
161
- onDragFinished: onDragFinished
198
+ size: edgeDraggerSize,
199
+ onDrag: (position) {
200
+ setState (() {
201
+ Offset newBottomLeft = _getNewPositionAfterDrag (
202
+ position, renderedImageWidth, renderedImageHeight
203
+ );
204
+ edgeDetectionResult.bottomLeft = _clampOffset (
205
+ edgeDetectionResult.bottomLeft + newBottomLeft
206
+ );
207
+ currentDragPosition = Offset (points[3 ].dx, points[3 ].dy);
208
+ });
209
+ },
210
+ onDragFinished: onDragFinished
162
211
),
163
212
left: points[3 ].dx - (edgeDraggerSize / 2 ),
164
213
top: points[3 ].dy - (edgeDraggerSize / 2 )
0 commit comments