-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathcustom_paint.dart
59 lines (48 loc) · 1.66 KB
/
custom_paint.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
import 'package:flutter/material.dart';
import 'dart:math' as Math;
class CustomPaintExample extends StatefulWidget {
CustomPaintExample({Key key}) : super(key: key);
@override
_CustomPaintExampleState createState() => _CustomPaintExampleState();
}
class _CustomPaintExampleState extends State<CustomPaintExample> {
@override
Widget build(BuildContext context) {
return Padding(
padding: EdgeInsets.all(32),
child: CustomPaint(
painter: FaceEmotionPainter(),
),
);
}
}
class FaceEmotionPainter extends CustomPainter {
@override
void paint(Canvas canvas, Size size) {
final radius = Math.min(size.width, size.height) / 2;
final center = Offset(size.width / 2, size.height / 2);
final Paint backgroundPaint = new Paint()..style = PaintingStyle.fill;
backgroundPaint..color = Colors.green;
final Paint facePaint = new Paint()
..isAntiAlias = true
..strokeWidth = 10.0
..color = Colors.white
..style = PaintingStyle.stroke;
// Draw Container
canvas.drawRect(
Rect.fromCircle(center: center, radius: radius / 0.9), backgroundPaint);
// Draw mouth
canvas.drawArc(Rect.fromCircle(center: center, radius: radius / 4), 0,
Math.pi, false, facePaint);
// Draw face
canvas.drawArc(Rect.fromCircle(center: center, radius: radius), 10.0, 20.0,
false, facePaint);
// Draw eyes
canvas.drawCircle(
Offset(center.dx - radius / 2, center.dy - radius / 4), 2, facePaint);
canvas.drawCircle(
Offset(center.dx + radius / 2, center.dy - radius / 4), 2, facePaint);
}
@override
bool shouldRepaint(CustomPainter oldDelegate) => false;
}