Skip to content

Instantly share code, notes, and snippets.

@benznest
Last active July 16, 2020 03:54
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save benznest/0556195d4c830c1ff627867e63a54eff to your computer and use it in GitHub Desktop.
Save benznest/0556195d4c830c1ff627867e63a54eff to your computer and use it in GitHub Desktop.
Draw human shape.
import 'package:flutter/material.dart';
void main() {
runApp(MyApp());
}
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Drawing',
theme: ThemeData(
primarySwatch: Colors.blue,
visualDensity: VisualDensity.adaptivePlatformDensity,
),
home: MyHomePage(),
);
}
}
class MyHomePage extends StatefulWidget {
MyHomePage({Key key}) : super(key: key);
@override
_MyHomePageState createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text("Flutter Drawing"),
),
body: Center(
child: Row(mainAxisAlignment: MainAxisAlignment.center,
children: [
CustomPaint(
size: Size(200,300),
painter: MyHumanPainter(),
),
],
),
),
);
}
}
class MyHumanPainter extends CustomPainter {
MyHumanPainter();
@override
void paint(Canvas canvas, Size size) {
double W = size.width;
double H = size.height;
// draw a head.
Paint paintHeader = Paint()..color = Colors.blue[300];
Offset offset = Offset(W / 2, H / 6);
double radius = W / 4;
canvas.drawCircle(offset, radius, paintHeader);
// draw a body
Paint paintBody = Paint()..color = Colors.green[300];
Offset pBody1 = Offset(3 * (W / 8), (H / 3));
Offset pBody2 = Offset(5 * (W / 8), 2 * (H / 3));
Rect body = Rect.fromPoints(pBody1, pBody2);
canvas.drawRect(body, paintBody);
// draw a left arm.
Paint paintLeftArm = Paint()
..color = Colors.green[300]
..strokeWidth = W / 30;
Offset pLeftArm1 = Offset(4 * (W / 10), (H / 3));
Offset pLeftArm2 = Offset(2 * (W / 10), 2 * (H / 3));
canvas.drawLine(pLeftArm1, pLeftArm2, paintLeftArm);
// draw a right arm.
Paint paintRightArm = Paint()
..color = Colors.green[300]
..strokeWidth = W / 30;
Offset pRightArm1 = Offset(6 * (W / 10), (H / 3));
Offset pRightArm2 = Offset(8 * (W / 10), 2 * (H / 3));
canvas.drawLine(pRightArm1, pRightArm2, paintRightArm);
// draw a left leg.
Paint paintLeftLeg = Paint()
..color = Colors.orange[300]
..strokeWidth = W / 25;
Offset pLeftLeg1 = Offset(3 * (W / 8) + (W / 16), 2 * (H / 3));
Offset pLeftLeg2 = Offset(3 * (W / 8) + (W / 16), H);
canvas.drawLine(pLeftLeg1, pLeftLeg2, paintLeftLeg);
// draw a left foot
Paint paintLeftFoot = Paint()
..color = Colors.orange[300]
..strokeWidth = W / 25;
Offset pLeftFoot1 = Offset(2 * (W / 8) + (W / 16), H);
Offset pLeftFoot2 = Offset(3 * (W / 8) + (W / 16), H);
canvas.drawLine(pLeftFoot1, pLeftFoot2, paintLeftFoot);
// draw a right leg.
Paint paintRightLeg = Paint()
..color = Colors.orange[300]
..strokeWidth = W / 25;
Offset pRightLeg1 = Offset(4 * (W / 8) + (W / 16), 2 * (H / 3));
Offset pRightLeg2 = Offset(4 * (W / 8) + (W / 16), H);
canvas.drawLine(pRightLeg1, pRightLeg2, paintRightLeg);
// draw a right foot
Paint paintRightFoot = Paint()
..color = Colors.orange[300]
..strokeWidth = W / 25;
Offset pRightFoot1 = Offset(4 * (W / 8) + (W / 16), H);
Offset pRightFoot2 = Offset(5 * (W / 8) + (W / 16), H);
canvas.drawLine(pRightFoot1, pRightFoot2, paintRightFoot);
// draw left eye.
Paint paintLeftEye = Paint()..color = Colors.white;
Offset offsetLeftEye = Offset((W / 2) - (2 * (W / 24)), (H / 6) - (2 * (H / 36)));
double radiusLeftEye = W / 24;
canvas.drawCircle(offsetLeftEye, radiusLeftEye, paintLeftEye);
// draw right eye.
Paint paintRightEye = Paint()..color = Colors.white;
Offset offsetRightEye = Offset((W / 2) + (2*(W / 24)), (H / 6) - (2 * (H / 36)));
double radiusRightEye = W/24;
canvas.drawCircle(offsetRightEye, radiusRightEye, paintRightEye);
// draw a mouth.
Paint paintMount = Paint()
..color = Colors.white
..strokeWidth = W / 50;
Offset pLeftMount1 = Offset((W / 2) - (2 * (W / 24)), (H/6) + (3 * (H / 48)));
Offset pLeftMount2 = Offset((W / 2) + (2 * (W / 24)), (H/6) + (3 * (H / 48)));
canvas.drawLine(pLeftMount1, pLeftMount2, paintMount);
}
@override
bool shouldRepaint(MyHumanPainter oldDelegate) {
return false;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment