-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathmain.dart
263 lines (230 loc) · 9.1 KB
/
main.dart
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
import 'dart:math' as math;
import 'package:arrow_path/arrow_path.dart';
import 'package:flutter/material.dart';
void main() => runApp(const MyApp());
class MyApp extends StatelessWidget {
const MyApp({Key? key}) : super(key: key);
@override
Widget build(BuildContext context) => MaterialApp(
title: 'Arrow Path Example',
theme: ThemeData(primarySwatch: Colors.blue),
home: const ExampleApp(),
);
}
class ExampleApp extends StatelessWidget {
const ExampleApp({Key? key}) : super(key: key);
@override
Widget build(BuildContext context) => Scaffold(
appBar: AppBar(
title: const Text('Arrow Path Example'),
),
body: SingleChildScrollView(
child: ClipRect(
child: CustomPaint(
size: Size(MediaQuery.of(context).size.width, 700),
painter: ArrowPainter(),
),
),
),
);
}
class ArrowPainter extends CustomPainter {
@override
void paint(Canvas canvas, Size size) {
/// The arrows usually looks better with rounded caps.
final Paint paint = Paint()
..color = Colors.black
..style = PaintingStyle.stroke
..strokeCap = StrokeCap.round
..strokeJoin = StrokeJoin.round
..strokeWidth = 3.0;
/// Draw a single arrow.
{
Path path = Path();
path.moveTo(size.width * 0.25, 60);
path.relativeCubicTo(0, 0, size.width * 0.25, 50, size.width * 0.5, 0);
path = ArrowPath.addTip(path);
canvas.drawPath(path, paint..color = Colors.blue);
const TextSpan textSpan = TextSpan(
text: 'Single arrow',
style: TextStyle(color: Colors.blue),
);
final TextPainter textPainter = TextPainter(
text: textSpan,
textAlign: TextAlign.center,
textDirection: TextDirection.ltr,
);
textPainter.layout(minWidth: size.width);
textPainter.paint(canvas, const Offset(0, 36));
}
/// Draw a double sided arrow.
{
Path path = Path();
path.moveTo(size.width * 0.25, 120);
path.relativeCubicTo(0, 0, size.width * 0.25, 50, size.width * 0.5, 0);
path = ArrowPath.addTip(path);
path = ArrowPath.addTip(path, isBackward: true);
canvas.drawPath(path, paint..color = Colors.cyan);
const TextSpan textSpan = TextSpan(
text: 'Double sided arrow',
style: TextStyle(color: Colors.cyan),
);
final TextPainter textPainter = TextPainter(
text: textSpan,
textAlign: TextAlign.center,
textDirection: TextDirection.ltr,
);
textPainter.layout(minWidth: size.width);
textPainter.paint(canvas, const Offset(0, 96));
}
/// Use complex path.
{
Path path = Path();
path.moveTo(size.width * 0.25, 180);
path.relativeCubicTo(0, 0, size.width * 0.25, 50, size.width * 0.5, 50);
path.relativeCubicTo(0, 0, -size.width * 0.25, 0, -size.width * 0.5, 50);
path.relativeCubicTo(0, 0, size.width * 0.125, 10, size.width * 0.25, -10);
path = ArrowPath.addTip(path);
canvas.drawPath(path, paint..color = Colors.blue);
const TextSpan textSpan = TextSpan(
text: 'Complex path',
style: TextStyle(color: Colors.blue),
);
final TextPainter textPainter = TextPainter(
text: textSpan,
textAlign: TextAlign.center,
textDirection: TextDirection.ltr,
);
textPainter.layout(minWidth: size.width);
textPainter.paint(canvas, const Offset(0, 168));
}
/// Path with several forward arrow and one backward arrow.
/// IMPORTANT: You should never use more than a single backward arrow on the same path, or the results
/// may be unpredictable due to limitations of the Path API.
///
/// If you need to build a complex path with several backward arrows then use sub-paths and
/// merge them with Path.addPath (see after).
{
Path path = Path();
path.moveTo(size.width * 0.25, 358);
path.relativeLineTo(size.width * 0.13, 50);
path = ArrowPath.addTip(path, tipAngle: math.pi * 0.1);
path.relativeLineTo(size.width * 0.13, -50);
path = ArrowPath.addTip(path, tipAngle: math.pi * 0.1);
path.relativeLineTo(size.width * 0.08, 50);
path = ArrowPath.addTip(path, tipAngle: math.pi * 0.1);
path.relativeLineTo(size.width * 0.08, -50);
path = ArrowPath.addTip(path, tipAngle: math.pi * 0.1);
path.relativeLineTo(size.width * 0.04, 50);
path = ArrowPath.addTip(path, tipAngle: math.pi * 0.1);
path.relativeLineTo(size.width * 0.04, -50);
path = ArrowPath.addTip(path, tipAngle: math.pi * 0.1);
path = ArrowPath.addTip(path, tipAngle: math.pi * 0.1, isBackward: true);
canvas.drawPath(path, paint..color = Colors.cyan);
const TextSpan textSpan = TextSpan(
text: 'Single Path with multiple forward and one backward arrow tips.',
style: TextStyle(color: Colors.cyan),
);
final TextPainter textPainter = TextPainter(
text: textSpan,
textAlign: TextAlign.center,
textDirection: TextDirection.ltr,
);
textPainter.layout(minWidth: size.width);
textPainter.paint(canvas, const Offset(0, 326));
}
/// Path with several backward arrow and one forward arrow.
/// IMPORTANT: Due to limitations of the Path API this can only be achieved reliably by using sub-paths.
{
Path path = Path();
Offset penPosition = Offset(size.width * 0.25, 470);
path.moveTo(penPosition.dx, penPosition.dy);
penPosition += Offset(size.width * 0.13, 50);
path.lineTo(penPosition.dx, penPosition.dy);
path = ArrowPath.addTip(path, tipAngle: math.pi * 0.1, isBackward: true);
Path subPath = Path();
subPath.moveTo(penPosition.dx, penPosition.dy);
penPosition += Offset(size.width * 0.13, -50);
subPath.lineTo(penPosition.dx, penPosition.dy);
subPath = ArrowPath.addTip(subPath, tipAngle: math.pi * 0.1, isBackward: true);
path.addPath(subPath, Offset.zero);
subPath = Path();
subPath.moveTo(penPosition.dx, penPosition.dy);
penPosition += Offset(size.width * 0.08, 50);
subPath.lineTo(penPosition.dx, penPosition.dy);
subPath = ArrowPath.addTip(subPath, tipAngle: math.pi * 0.1, isBackward: true);
path.addPath(subPath, Offset.zero);
subPath = Path();
subPath.moveTo(penPosition.dx, penPosition.dy);
penPosition += Offset(size.width * 0.08, -50);
subPath.lineTo(penPosition.dx, penPosition.dy);
subPath = ArrowPath.addTip(subPath, tipAngle: math.pi * 0.1, isBackward: true);
path.addPath(subPath, Offset.zero);
subPath = Path();
subPath.moveTo(penPosition.dx, penPosition.dy);
penPosition += Offset(size.width * 0.04, 50);
subPath.lineTo(penPosition.dx, penPosition.dy);
subPath = ArrowPath.addTip(subPath, tipAngle: math.pi * 0.1, isBackward: true);
path.addPath(subPath, Offset.zero);
subPath = Path();
subPath.moveTo(penPosition.dx, penPosition.dy);
penPosition += Offset(size.width * 0.04, -50);
subPath.lineTo(penPosition.dx, penPosition.dy);
subPath = ArrowPath.addTip(subPath, tipAngle: math.pi * 0.1, isBackward: true);
path.addPath(subPath, Offset.zero);
path = ArrowPath.addTip(path, tipAngle: math.pi * 0.1);
canvas.drawPath(path, paint..color = Colors.blue);
const TextSpan textSpan = TextSpan(
text: 'Single Path with multiple backward arrow tips.',
style: TextStyle(color: Colors.blue),
);
final TextPainter textPainter = TextPainter(
text: textSpan,
textAlign: TextAlign.center,
textDirection: TextDirection.ltr,
);
textPainter.layout(minWidth: size.width);
textPainter.paint(canvas, const Offset(0, 436));
}
/// Adjusted
{
Path path = Path();
path.moveTo(size.width * 0.1, 590);
path.relativeCubicTo(0, 0, size.width * 0.3, 50, size.width * 0.25, 75);
path = ArrowPath.addTip(path, isAdjusted: true);
canvas.drawPath(path, paint..color = Colors.cyan);
const TextSpan textSpan = TextSpan(
text: 'Adjusted',
style: TextStyle(color: Colors.cyan),
);
final TextPainter textPainter = TextPainter(
text: textSpan,
textAlign: TextAlign.left,
textDirection: TextDirection.ltr,
);
textPainter.layout();
textPainter.paint(canvas, Offset(size.width * 0.2, 572));
}
/// Non adjusted.
{
Path path = Path();
path.moveTo(size.width * 0.6, 590);
path.relativeCubicTo(0, 0, size.width * 0.3, 50, size.width * 0.25, 75);
path = ArrowPath.addTip(path, isAdjusted: false);
canvas.drawPath(path, paint..color = Colors.cyan);
const TextSpan textSpan = TextSpan(
text: 'Non adjusted',
style: TextStyle(color: Colors.cyan),
);
final TextPainter textPainter = TextPainter(
text: textSpan,
textAlign: TextAlign.left,
textDirection: TextDirection.ltr,
);
textPainter.layout();
textPainter.paint(canvas, Offset(size.width * 0.65, 572));
}
}
@override
bool shouldRepaint(ArrowPainter oldDelegate) => false;
}