-
Notifications
You must be signed in to change notification settings - Fork 19
Expand file tree
/
Copy pathwidget.dart
More file actions
174 lines (151 loc) · 5.37 KB
/
widget.dart
File metadata and controls
174 lines (151 loc) · 5.37 KB
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
import 'dart:math';
import 'package:flutter/material.dart';
import 'model.dart';
class CircularText extends StatelessWidget {
///List of text
final List<TextItem> children;
/// Circle radius
final double radius;
/// Text position either outside or inside circle
final CircularTextPosition position;
/// Background paint
final Paint? backgroundPaint;
CircularText({
Key? key,
required this.children,
this.radius = 125,
this.position = CircularTextPosition.inside,
this.backgroundPaint,
}) : assert(radius >= 0),
super(key: key);
@override
Widget build(BuildContext context) {
return FittedBox(
child: SizedBox.fromSize(
size: Size(2 * radius, 2 * radius),
child: CustomPaint(
painter: _CircularTextPainter(
children: children,
position: position,
backgroundPaint: backgroundPaint,
textDirection: Directionality.of(context),
),
),
),
);
}
}
class _CircularTextPainter extends CustomPainter {
final List<TextItem> children;
final CircularTextPosition position;
final Paint backgroundPaint;
final TextDirection textDirection;
double _radius = 0.0;
_CircularTextPainter({
required this.children,
this.position = CircularTextPosition.inside,
Paint? backgroundPaint,
required this.textDirection,
}) : this.backgroundPaint =
backgroundPaint ?? (Paint()..color = Colors.transparent);
@override
void paint(Canvas canvas, Size size) {
_radius = min(size.width / 2, size.height / 2);
canvas.translate(size.width / 2, size.height / 2);
canvas.drawCircle(Offset.zero, _radius, backgroundPaint);
for (final textItem in children) {
canvas.save();
List<TextPainter> _charPainters = [];
Text text = textItem.text;
for (final char in text.data!.split("")) {
_charPainters.add(TextPainter(
text: TextSpan(text: char, style: text.style),
textDirection: textDirection)
..layout());
}
if (textItem.direction == CircularTextDirection.clockwise) {
_paintTextClockwise(canvas, size, textItem, _charPainters);
} else {
_paintTextAntiClockwise(canvas, size, textItem, _charPainters);
}
canvas.restore();
}
}
void _paintTextClockwise(Canvas canvas, Size size, TextItem textItem,
List<TextPainter> _charPainters) {
bool hasStrokeStyle = backgroundPaint.style == PaintingStyle.stroke &&
backgroundPaint.strokeWidth > 0.0;
double angleShift = _calculateAngleShift(textItem, _charPainters.length);
canvas.rotate((textItem.startAngle + 90 - angleShift) * pi / 180);
for (int i = 0; i < _charPainters.length; i++) {
final tp = _charPainters[i];
final x = -tp.width / 2;
final y = position == CircularTextPosition.outside
? (-_radius - tp.height) -
(hasStrokeStyle ? backgroundPaint.strokeWidth / 2 : 0.0)
: -_radius - (hasStrokeStyle ? tp.height / 2 : 0.0);
tp.paint(canvas, Offset(x, y));
canvas.rotate(textItem.space * pi / 180);
}
}
void _paintTextAntiClockwise(Canvas canvas, Size size, TextItem textItem,
List<TextPainter> _charPainters) {
bool hasStrokeStyle = backgroundPaint.style == PaintingStyle.stroke &&
backgroundPaint.strokeWidth > 0.0;
double angleShift = _calculateAngleShift(textItem, _charPainters.length);
canvas.rotate((textItem.startAngle - 90 + angleShift) * pi / 180);
for (int i = 0; i < _charPainters.length; i++) {
final tp = _charPainters[i];
final x = -tp.width / 2;
final y = position == CircularTextPosition.outside
? _radius + (hasStrokeStyle ? backgroundPaint.strokeWidth / 2 : 0.0)
: (_radius - tp.height) + (hasStrokeStyle ? tp.height / 2 : 0.0);
tp.paint(canvas, Offset(x, y));
canvas.rotate(-textItem.space * pi / 180);
}
}
double _calculateAngleShift(TextItem textItem, int textLength) {
double angleShift = -1;
switch (textItem.startAngleAlignment) {
case StartAngleAlignment.start:
angleShift = 0;
break;
case StartAngleAlignment.center:
int halfItemsLength = textLength ~/ 2;
if (textLength % 2 == 0) {
angleShift =
((halfItemsLength - 1) * textItem.space) + (textItem.space / 2);
} else {
angleShift = (halfItemsLength * textItem.space);
}
break;
case StartAngleAlignment.end:
angleShift = (textLength - 1) * textItem.space;
break;
}
return angleShift;
}
@override
bool shouldRepaint(_CircularTextPainter oldDelegate) {
bool isTextItemsChanged() {
bool isChanged = false;
for (int i = 0; i < children.length; i++) {
if (i >= oldDelegate.children.length || children[i].isChanged(oldDelegate.children[i])) {
isChanged = true;
break;
}
}
return isChanged;
}
bool isBackgroundPaintChanged() {
return oldDelegate.backgroundPaint.color != backgroundPaint.color ||
oldDelegate.backgroundPaint.style != backgroundPaint.style ||
oldDelegate.backgroundPaint.strokeWidth !=
backgroundPaint.strokeWidth;
}
return isTextItemsChanged() ||
oldDelegate.position != position ||
oldDelegate.textDirection != textDirection ||
isBackgroundPaintChanged();
}
}