-
Notifications
You must be signed in to change notification settings - Fork 23
/
Copy pathrotating_gradient_arc.dart
97 lines (83 loc) · 2.76 KB
/
rotating_gradient_arc.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
import 'package:flutter/material.dart';
import 'dart:math' as math;
import 'package:custom_widgets/style.dart';
class RotatingGradientArc extends StatefulWidget {
const RotatingGradientArc({super.key});
@override
State<RotatingGradientArc> createState() => _RotatingGradientArcState();
}
class _RotatingGradientArcState extends State<RotatingGradientArc>
with TickerProviderStateMixin {
late final AnimationController _animationController = AnimationController(
duration: const Duration(seconds: 5),
vsync: this,
)..repeat();
late final Animation<double> _turnAnimation =
Tween<double>(begin: 0, end: math.pi * 2).animate(_animationController);
Gradient get borderGradient => const LinearGradient(colors: BaseColors.grad1);
@override
void dispose() {
_animationController.dispose();
super.dispose();
}
@override
Widget build(BuildContext context) {
return Scaffold(
backgroundColor: Colors.black,
appBar: AppBar(
automaticallyImplyLeading: true,
title: const Text(
"Rotating gradient arc",
style: TextStyle(color: Colors.white),
),
backgroundColor: Colors.black,
),
body: Center(
child: AnimatedBuilder(
animation: _animationController,
builder: (context, child) {
return CustomPaint(
size: const Size(200, 200),
painter: GradientArcPainter(
startAngle: _turnAnimation.value, gradient: borderGradient),
);
}),
),
);
}
}
class GradientArcPainter extends CustomPainter {
double startAngle;
Gradient gradient;
GradientArcPainter({required this.startAngle, required this.gradient});
@override
void paint(Canvas canvas, Size size) {
const double strokeWidth = 20.0;
final double radius = (size.width / 2);
final Offset center = Offset(size.width / 2, size.height / 2);
// Grey circle paint definition and drawing
final Paint circlePaint = Paint()
..color = const Color.fromARGB(255, 81, 78, 78)
..style = PaintingStyle.stroke
..strokeWidth = strokeWidth;
canvas.drawCircle(center, radius, circlePaint);
// gradient paint
final Paint gradientPaint = Paint()
..shader =
gradient.createShader(Rect.fromCircle(center: center, radius: radius))
..style = PaintingStyle.stroke
..strokeCap = StrokeCap.round
..strokeWidth = strokeWidth;
// arc path
final Path arcPath = Path()
..addArc(
Rect.fromCircle(center: center, radius: radius),
startAngle,
3.14 / 4,
);
//draw path with the gradient paint
canvas.drawPath(arcPath, gradientPaint);
}
@override
bool shouldRepaint(CustomPainter oldDelegate) => true;
}