Skip to content

Instantly share code, notes, and snippets.

@sbis04
Created April 23, 2020 05:05
Show Gist options
  • Save sbis04/f6a8f21a7241d75c3e550a2bdd0f4b0f to your computer and use it in GitHub Desktop.
Save sbis04/f6a8f21a7241d75c3e550a2bdd0f4b0f to your computer and use it in GitHub Desktop.
Flutter Custom Painter Polygons
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: 'Flutter Custom Painter',
theme: ThemeData(
primarySwatch: Colors.pink,
),
home: MyPainter(),
);
}
}
class MyPainter extends StatefulWidget {
@override
_MyPainterState createState() => _MyPainterState();
}
class _MyPainterState extends State<MyPainter> {
var _sides = 3.0;
var _radius = 100.0;
var _radians = 0.0;
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('Polygons'),
),
body: SafeArea(
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: <Widget>[
Expanded(
child: CustomPaint(
painter: ShapePainter(_sides, _radius, _radians),
child: Container(),
),
),
Padding(
padding: const EdgeInsets.only(left: 16.0),
child: Text('Sides'),
),
Slider(
value: _sides,
min: 3.0,
max: 10.0,
label: _sides.toInt().toString(),
divisions: 7,
onChanged: (value) {
setState(() {
_sides = value;
});
},
),
Padding(
padding: const EdgeInsets.only(left: 16.0),
child: Text('Size'),
),
Slider(
value: _radius,
min: 10.0,
max: MediaQuery.of(context).size.width / 2,
onChanged: (value) {
setState(() {
_radius = value;
});
},
),
Padding(
padding: const EdgeInsets.only(left: 16.0),
child: Text('Rotation'),
),
Slider(
value: _radians,
min: 0.0,
max: math.pi,
onChanged: (value) {
setState(() {
_radians = value;
});
},
),
],
),
),
);
}
}
// FOR PAINTING POLYGONS
class ShapePainter extends CustomPainter {
final double sides;
final double radius;
final double radians;
ShapePainter(this.sides, this.radius, this.radians);
@override
void paint(Canvas canvas, Size size) {
var paint = Paint()
..color = Colors.teal
..strokeWidth = 5
..style = PaintingStyle.stroke
..strokeCap = StrokeCap.round;
var path = Path();
var angle = (math.pi * 2) / sides;
Offset center = Offset(size.width / 2, size.height / 2);
Offset startPoint =
Offset(radius * math.cos(radians), radius * math.sin(radians));
path.moveTo(startPoint.dx + center.dx, startPoint.dy + center.dy);
for (int i = 1; i <= sides; i++) {
double x = radius * math.cos(radians + angle * i) + center.dx;
double y = radius * math.sin(radians + angle * i) + center.dy;
path.lineTo(x, y);
}
path.close();
canvas.drawPath(path, paint);
}
@override
bool shouldRepaint(CustomPainter oldDelegate) {
return true;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment