Skip to content

Instantly share code, notes, and snippets.

@benznest
Created July 20, 2020 09:25
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/182c37882455d389ab4212478a7bc028 to your computer and use it in GitHub Desktop.
Save benznest/182c37882455d389ab4212478a7bc028 to your computer and use it in GitHub Desktop.
drawing watermelon with Flutter Painter.
import 'dart:ui';
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: 'Watermelon',
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> {
double r = 0;
double dx = 0;
double dy = 0;
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text("Flutter Watermelon Painter"),
),
body: Column(
children: [
Expanded(
child: Center(
child: Wrap(
children: [
CustomPaint(
size: Size(250, 250),
painter: MyWaterMelonPainter(radiant: r,dx: dx,dy: dy),
),
],
),
),
),
Text(
"Radians = ${r.toStringAsFixed(2)} π",
style: TextStyle(fontSize: 22),
),
Text(
"Dx = ${dx.floor()}",
style: TextStyle(fontSize: 22),
),
Text(
"Dy = ${dy.floor()}",
style: TextStyle(fontSize: 22),
),
SizedBox(
height: 50,
),
Slider(
value: r,
min: - 2 * math.pi,
max: 2 * math.pi,
onChanged: (v) {
setState(() {
r = v;
});
},
),
Slider(
value: dx,
min: -1000,
max: 1000,
onChanged: (v) {
setState(() {
dx = v;
});
},
),
Slider(
value: dy,
min: -1000,
max: 1000,
onChanged: (v) {
setState(() {
dy = v;
});
},
),
],
),
);
}
}
class MyWaterMelonPainter extends CustomPainter {
double radiant;
double dx;
double dy;
MyWaterMelonPainter({this.radiant = 0, this.dx = 0, this.dy = 0});
@override
void paint(Canvas canvas, Size size) {
double W = size.width;
double H = size.height;
canvas.save();
canvas.rotate(radiant);
canvas.translate(dx, dy);
canvas.save();
canvas.clipRect(Rect.fromLTWH(0, 0, W, H / 2));
canvas.drawPaint(Paint()..color = Colors.green[100]);
Paint paintWaterMelonBody = Paint()..color = Color(0xff78bd53);
canvas.drawCircle(Offset(W / 2, H / 2), W / 2, paintWaterMelonBody);
Paint paintWaterMelonRind = Paint()..color = Color(0xffe23342);
canvas.drawCircle(Offset(W / 2, H / 2), (W / 2) - W / 10, paintWaterMelonRind);
canvas.restore();
canvas.save();
Paint paintSeed = Paint()..color = Color(0xff3c3b41);
double sizeSeed = W / 40;
canvas.drawCircle(Offset((W / 2) - (W / 10), (H / 2) - (H / 10)), sizeSeed, paintSeed);
canvas.drawCircle(Offset((W / 2) - (W / 8), (H / 2) - (H / 4)), sizeSeed, paintSeed);
canvas.drawCircle(Offset((W / 2) - (W / 4), (H / 2) - (H / 8)), sizeSeed, paintSeed);
canvas.drawCircle(Offset((W / 2) + (W / 4), (H / 2) - (H / 8)), sizeSeed, paintSeed);
canvas.drawCircle(Offset((W / 2) + (W / 6), (H / 2) - (H / 5)), sizeSeed, paintSeed);
canvas.drawCircle(Offset((W / 2) + (W / 14), (H / 2) - (H / 7)), sizeSeed, paintSeed);
canvas.drawCircle(Offset((W / 2) + (W / 28), (H / 2) - (H / 3.5)), sizeSeed, paintSeed);
canvas.drawCircle(Offset((W / 2) + (W / 32), (H / 2) - (H / 18)), sizeSeed, paintSeed);
canvas.restore();
canvas.restore();
}
@override
bool shouldRepaint(MyWaterMelonPainter oldDelegate) {
return true;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment