-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcustom_painter.dart
123 lines (104 loc) · 2.8 KB
/
custom_painter.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
import 'package:flutter/material.dart';
import 'dart:math' as math;
void main() => runApp(MyApp());
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'CustomPainter',
theme: ThemeData(
primarySwatch: Colors.blue,
),
home: Body(),
);
}
}
class Body extends StatefulWidget {
@override
_BodyState createState() => _BodyState();
}
class _BodyState extends State<Body> with TickerProviderStateMixin {
Animation<double> animation;
AnimationController controller;
@override
void initState() {
super.initState();
controller = AnimationController(
vsync: this,
duration: Duration(seconds: 4),
);
Tween<double> _rotationTween = Tween(begin: -math.pi, end: math.pi);
animation = _rotationTween.animate(controller)
..addListener(() {
setState(() {});
})
..addStatusListener((status) {
if (status == AnimationStatus.completed) {
controller.repeat();
} else if (status == AnimationStatus.dismissed) {
controller.forward();
}
});
controller.forward();
}
@override
void dispose() {
controller.dispose();
super.dispose();
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('CustomPainter'),
),
body: SafeArea(
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: <Widget>[
Expanded(
child: AnimatedBuilder(
animation: animation,
builder: (context, snapshot) {
return CustomPaint(
painter: ShapePainter(animation.value),
child: Container(),
);
},
),
),
],
),
),
);
}
}
class ShapePainter extends CustomPainter {
final double radians;
ShapePainter(this.radians);
@override
void paint(Canvas canvas, Size size) {
var paint = Paint()
..color = Colors.pink
..strokeWidth = 5
..style = PaintingStyle.stroke
..strokeCap = StrokeCap.round;
var path = Path();
var angle = (math.pi * 2) / 4.0;
Offset center = Offset(size.width / 2, size.height / 2);
Offset startPoint =
Offset(100 * math.cos(radians), 100 * math.sin(radians));
path.moveTo(startPoint.dx + center.dx, startPoint.dy + center.dy);
for (int i = 1; i <= 4; i++) {
double x = 100 * math.cos(radians + angle * i) + center.dx;
double y = 100 * math.sin(radians + angle * i) + center.dy;
path.lineTo(x, y);
}
path.close();
canvas.drawPath(path, paint);
}
@override
bool shouldRepaint(CustomPainter oldDelegate) {
return true;
}
}