-
Notifications
You must be signed in to change notification settings - Fork 116
/
ArcChooser.dart
345 lines (279 loc) · 11 KB
/
ArcChooser.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
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
import 'dart:math';
import 'dart:ui';
import 'package:flutter/material.dart';
import 'package:flutter/painting.dart';
class ArcChooser extends StatefulWidget {
ArcSelectedCallback arcSelectedCallback;
@override
State<StatefulWidget> createState() {
return ChooserState(arcSelectedCallback);
}
}
class ChooserState extends State<ArcChooser> with SingleTickerProviderStateMixin {
var slideValue = 200;
Offset centerPoint;
double userAngle = 0.0;
double startAngle;
static double center = 270.0;
static double centerInRadians = degreeToRadians(center);
static double angle = 45.0;
static double angleInRadians = degreeToRadians(angle);
static double angleInRadiansByTwo = angleInRadians/2;
static double centerItemAngle = degreeToRadians(center - (angle / 2));
List<ArcItem> arcItems;
AnimationController animation;
double animationStart;
double animationEnd = 0.0;
int currentPosition = 0;
Offset startingPoint;
Offset endingPoint;
ArcSelectedCallback arcSelectedCallback;
ChooserState(ArcSelectedCallback arcSelectedCallback){
this.arcSelectedCallback = arcSelectedCallback;
}
static double degreeToRadians(double degree) {
return degree * (PI / 180);
}
static double radianToDegrees(double radian) {
return radian * (180 / PI );
}
@override
void initState() {
arcItems = List<ArcItem>();
arcItems.add(ArcItem("UGH", [Color(0xFFF9D976), Color(0xfff39f86)],
angleInRadiansByTwo + userAngle));
arcItems.add(ArcItem("OK", [Color(0xFF21e1fa), Color(0xff3bb8fd)],
angleInRadiansByTwo + userAngle + (angleInRadians)));
arcItems.add(ArcItem("GOOD", [Color(0xFF3ee98a), Color(0xFF41f7c7)],
angleInRadiansByTwo + userAngle + (2 * angleInRadians)));
arcItems.add(ArcItem("BAD", [Color(0xFFfe0944), Color(0xFFfeae96)],
angleInRadiansByTwo + userAngle + (3 * angleInRadians)));
arcItems.add(ArcItem("UGH", [Color(0xFFF9D976), Color(0xfff39f86)],
angleInRadiansByTwo + userAngle + (4 * angleInRadians)));
arcItems.add(ArcItem("OK", [Color(0xFF21e1fa), Color(0xff3bb8fd)],
angleInRadiansByTwo + userAngle + (5 * angleInRadians)));
arcItems.add(ArcItem("GOOD", [Color(0xFF3ee98a), Color(0xFF41f7c7)],
angleInRadiansByTwo + userAngle + (6 * angleInRadians)));
arcItems.add(ArcItem("BAD", [Color(0xFFfe0944), Color(0xFFfeae96)],
angleInRadiansByTwo + userAngle + (7 * angleInRadians)));
animation = new AnimationController(
duration: const Duration(milliseconds: 200), vsync: this);
animation.addListener(() {
userAngle = lerpDouble(animationStart, animationEnd, animation.value);
setState(() {
for (int i = 0; i < arcItems.length; i++) {
arcItems[i].startAngle = angleInRadiansByTwo + userAngle +
(i * angleInRadians);
}
});
});
super.initState();
}
@override
Widget build(BuildContext context) {
double centerX = MediaQuery.of(context).size.width / 2;
double centerY = MediaQuery.of(context).size.height * 1.5;
centerPoint = Offset(centerX, centerY);
return new GestureDetector(
// onTap: () {
// print('ChooserState.build ONTAP');
// animationStart = touchAngle;
// animationEnd = touchAngle + angleInRadians;
// animation.forward(from: 0.0);
// },
onPanStart: (DragStartDetails details) {
startingPoint = details.globalPosition;
var deltaX = centerPoint.dx - details.globalPosition.dx;
var deltaY = centerPoint.dy - details.globalPosition.dy;
startAngle = atan2(deltaY, deltaX);
},
onPanUpdate: (DragUpdateDetails details) {
endingPoint = details.globalPosition;
var deltaX = centerPoint.dx - details.globalPosition.dx;
var deltaY = centerPoint.dy - details.globalPosition.dy;
var freshAngle = atan2(deltaY, deltaX);
userAngle += freshAngle - startAngle;
setState(() {
for (int i = 0; i < arcItems.length; i++) {
arcItems[i].startAngle =
angleInRadiansByTwo + userAngle + (i * angleInRadians);
}
});
startAngle = freshAngle;
},
onPanEnd: (DragEndDetails details){
//find top arc item with Magic!!
bool rightToLeft = startingPoint.dx<endingPoint.dx;
// Animate it from this values
animationStart = userAngle;
if(rightToLeft) {
animationEnd +=angleInRadians;
currentPosition--;
if(currentPosition<0){
currentPosition = arcItems.length-1;
}
}else{
animationEnd -=angleInRadians;
currentPosition++;
if(currentPosition>=arcItems.length){
currentPosition = 0;
}
}
if(arcSelectedCallback!=null){
arcSelectedCallback(currentPosition, arcItems[(currentPosition>=(arcItems.length-1))?0:currentPosition+1]);
}
animation.forward(from: 0.0);
},
child: CustomPaint(
size: Size(MediaQuery.of(context).size.width,
MediaQuery.of(context).size.width * 1 / 1.5),
painter: ChooserPainter(arcItems, angleInRadians),
),
);
}
}
// draw the arc and other stuff
class ChooserPainter extends CustomPainter {
//debugging Paint
final debugPaint = new Paint()
..color = Colors.red.withAlpha(100) //0xFFF9D976
..strokeWidth = 1.0
..style = PaintingStyle.stroke;
final linePaint = new Paint()
..color = Colors.black.withAlpha(65) //0xFFF9D976
..strokeWidth = 2.0
..style = PaintingStyle.stroke
..strokeCap = StrokeCap.square;
final whitePaint = new Paint()
..color = Colors.white //0xFFF9D976
..strokeWidth = 1.0
..style = PaintingStyle.fill;
List<ArcItem> arcItems;
double angleInRadians;
double angleInRadiansByTwo;
double angleInRadians1;
double angleInRadians2;
double angleInRadians3;
double angleInRadians4;
ChooserPainter(List<ArcItem> arcItems, double angleInRadians) {
this.arcItems = arcItems;
this.angleInRadians = angleInRadians;
this.angleInRadiansByTwo = angleInRadians/2;
angleInRadians1 = angleInRadians/6;
angleInRadians2 = angleInRadians/3;
angleInRadians3 = angleInRadians*4/6;
angleInRadians4 = angleInRadians*5/6;
}
@override
void paint(Canvas canvas, Size size) {
//common calc
double centerX = size.width / 2;
double centerY = size.height * 1.6;
Offset center = Offset(centerX, centerY);
double radius = sqrt((size.width * size.width) / 2);
// var mainRect = Rect.fromLTRB(0.0, 0.0, size.width, size.height);
// canvas.drawRect(mainRect, debugPaint);
//for white arc at bottom
double leftX = centerX - radius;
double topY = centerY - radius;
double rightX = centerX + radius;
double bottomY = centerY + radius;
//for items
double radiusItems = radius * 1.5;
double leftX2 = centerX - radiusItems;
double topY2 = centerY - radiusItems;
double rightX2 = centerX + radiusItems;
double bottomY2 = centerY + radiusItems;
//for shadow
double radiusShadow = radius*1.13;
double leftX3 = centerX - radiusShadow;
double topY3 = centerY - radiusShadow;
double rightX3 = centerX + radiusShadow;
double bottomY3 = centerY + radiusShadow;
double radiusText = radius * 1.30;
double radius4 = radius * 1.12;
double radius5 = radius * 1.06;
var arcRect = Rect.fromLTRB(leftX2, topY2, rightX2, bottomY2);
var dummyRect = Rect.fromLTRB(0.0, 0.0, size.width, size.height);
canvas.clipRect(dummyRect, clipOp: ClipOp.intersect);
for (int i = 0; i < arcItems.length; i++) {
canvas.drawArc(
arcRect,
arcItems[i].startAngle,
angleInRadians,
true,
new Paint()
..style = PaintingStyle.fill
..shader = new LinearGradient(
colors: arcItems[i].colors,
).createShader(dummyRect));
//Draw text
TextSpan span = new TextSpan(style: new TextStyle(fontWeight: FontWeight.normal, fontSize: 32.0, color: Colors.white), text: arcItems[i].text);
TextPainter tp = new TextPainter(text: span, textAlign: TextAlign.center, textDirection: TextDirection.ltr,);
tp.layout();
//find additional angle to make text in center
double f = tp.width/2;
double t = sqrt((radiusText*radiusText) + (f*f));
double additionalAngle = acos(((t*t) + (radiusText*radiusText)-(f*f))/(2*t*radiusText));
double tX = center.dx + radiusText*cos(arcItems[i].startAngle+angleInRadiansByTwo - additionalAngle);// - (tp.width/2);
double tY = center.dy + radiusText*sin(arcItems[i].startAngle+angleInRadiansByTwo - additionalAngle);// - (tp.height/2);
canvas.save();
canvas.translate(tX,tY);
// canvas.rotate(arcItems[i].startAngle + angleInRadiansByTwo);
canvas.rotate(arcItems[i].startAngle+angleInRadians+angleInRadians+angleInRadiansByTwo);
tp.paint(canvas, new Offset(0.0,0.0));
canvas.restore();
//big lines
canvas.drawLine(
new Offset(center.dx + radius4*cos(arcItems[i].startAngle), center.dy + radius4*sin(arcItems[i].startAngle)),
center,
linePaint);
canvas.drawLine(
new Offset(center.dx + radius4*cos(arcItems[i].startAngle+angleInRadiansByTwo), center.dy + radius4*sin(arcItems[i].startAngle+angleInRadiansByTwo)),
center,
linePaint);
//small lines
canvas.drawLine(
new Offset(center.dx + radius5*cos(arcItems[i].startAngle+angleInRadians1), center.dy + radius5*sin(arcItems[i].startAngle+angleInRadians1)),
center,
linePaint);
canvas.drawLine(
new Offset(center.dx + radius5*cos(arcItems[i].startAngle+angleInRadians2), center.dy + radius5*sin(arcItems[i].startAngle+angleInRadians2)),
center,
linePaint);
canvas.drawLine(
new Offset(center.dx + radius5*cos(arcItems[i].startAngle+angleInRadians3), center.dy + radius5*sin(arcItems[i].startAngle+angleInRadians3)),
center,
linePaint);
canvas.drawLine(
new Offset(center.dx + radius5*cos(arcItems[i].startAngle+angleInRadians4), center.dy + radius5*sin(arcItems[i].startAngle+angleInRadians4)),
center,
linePaint);
}
//shadow
Path shadowPath = new Path();
shadowPath.addArc(
Rect.fromLTRB(leftX3, topY3, rightX3, bottomY3),
ChooserState.degreeToRadians(180.0),
ChooserState.degreeToRadians(180.0));
canvas.drawShadow(shadowPath, Colors.black, 18.0, true);
//bottom white arc
canvas.drawArc(
Rect.fromLTRB(leftX, topY, rightX, bottomY),
ChooserState.degreeToRadians(180.0),
ChooserState.degreeToRadians(180.0),
true,
whitePaint);
}
@override
bool shouldRepaint(CustomPainter oldDelegate) {
return true;
}
}
typedef void ArcSelectedCallback(int position, ArcItem arcitem);
class ArcItem {
String text;
List<Color> colors;
double startAngle;
ArcItem(this.text, this.colors, this.startAngle);
}