/
main.dart
209 lines (183 loc) · 4.48 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
import 'dart:async';
import 'dart:typed_data';
import 'package:flutter/material.dart';
import 'package:vector_math/vector_math.dart' as vmath;
import 'dart:math' as math;
import 'dart:ui' as UI;
import 'package:flutter/services.dart';
void main() {
runApp(const MyApp());
}
class MyApp extends StatelessWidget {
const MyApp({Key? key}) : super(key: key);
// This widget is the root of your application.
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Demo',
theme: ThemeData(
primarySwatch: Colors.blue,
),
home: const MyHomePage(title: 'Multi Circle Indicator'),
);
}
}
class MyHomePage extends StatefulWidget {
const MyHomePage({Key? key, required this.title}) : super(key: key);
final String title;
@override
State<MyHomePage> createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
UI.Image? img1;
UI.Image? img2;
@override
void initState() {
super.initState();
loadUiImage("lib/assets/yoga1.png").then(
(value) => {
setState(() {
img1 = value;
}),
},
);
loadUiImage("lib/assets/yoga.png").then(
(value) => {
setState(() {
img2 = value;
}),
},
);
}
Future<UI.Image> loadUiImage(String imageAssetPath) async {
final ByteData data = await rootBundle.load(imageAssetPath);
final Completer<UI.Image> completer = Completer();
UI.decodeImageFromList(Uint8List.view(data.buffer), (UI.Image img) {
return completer.complete(img);
});
return completer.future;
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text(widget.title),
),
body: Center(
child: Stack(
children: <Widget>[
outer(),
middle(),
inner(),
],
),
),
);
}
outer() {
return CustomPaint(
painter: MyPainter(
img1,
total: 300,
current: 230,
radius: 90,
strokeWidth: 15,
strokeColor: Colors.red.shade300,
),
child: Container(),
);
}
middle() {
return CustomPaint(
painter: MyPainter(
img2,
total: 200,
current: 140,
radius: 60,
strokeColor: Colors.pink.shade300,
strokeWidth: 15,
),
child: Container(),
);
}
inner() {
return CustomPaint(
painter: MyPainter(
img2,
total: 100,
current: 50,
radius: 30,
strokeWidth: 15,
),
child: Container(),
);
}
}
class MyPainter extends CustomPainter {
UI.Image? img;
double radius;
double total;
double current;
double strokeWidth;
Color color;
Color strokeColor;
MyPainter(this.img,
{required this.total,
required this.current,
this.radius = 85,
this.color = Colors.black12,
this.strokeColor = Colors.green,
this.strokeWidth = 30});
@override
Future<void> paint(Canvas canvas, Size size) async {
final center = Offset(size.width / 2, size.height / 2);
double startAngle = 0;
startAngle = startAngle - 90;
double angle = 0;
// calculate angle
var percentage = current / total;
angle = percentage * 360;
// BG layer
canvas.drawCircle(
center,
radius,
Paint()
..style = PaintingStyle.stroke
..color = color
..strokeWidth = strokeWidth,
);
canvas.saveLayer(
Rect.fromCenter(
center: center,
width: radius * 2 + strokeWidth,
height: radius * 2 + strokeWidth),
// Paint()..blendMode = BlendMode.dstIn,
Paint(),
);
//Progress
canvas.drawArc(
Rect.fromCenter(center: center, width: radius * 2, height: radius * 2),
vmath.radians(startAngle),
vmath.radians(angle),
false,
Paint()
..style = PaintingStyle.stroke
..strokeCap = StrokeCap.round
..color = strokeColor
..strokeWidth = strokeWidth,
);
Offset p = Offset(
((size.width / 2) +
radius * math.cos(vmath.radians(angle + startAngle))) -
10,
((size.height / 2) +
radius * math.sin(vmath.radians(angle + startAngle))) -
10);
if (img != null) {
canvas.drawImage(img!, p, Paint()..color = Colors.red);
}
canvas.restore();
}
@override
bool shouldRepaint(covariant CustomPainter oldDelegate) => false;
}