Created
April 23, 2020 05:10
-
-
Save sbis04/deb03027f153fd9e849c45267e8868b9 to your computer and use it in GitHub Desktop.
Animate Polygons
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> | |
with TickerProviderStateMixin { | |
var _sides = 3.0; | |
Animation<double> animation; | |
AnimationController controller; | |
Animation<double> animation2; | |
AnimationController controller2; | |
@override | |
void initState() { | |
super.initState(); | |
controller = AnimationController( | |
vsync: this, | |
duration: Duration(seconds: 4), | |
); | |
controller2 = AnimationController( | |
vsync: this, | |
duration: Duration(seconds: 4), | |
); | |
Tween<double> _radiusTween = Tween(begin: 0.0, end: 200); | |
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(); | |
} | |
}); | |
animation2 = _radiusTween.animate(controller2) | |
..addListener(() { | |
setState(() {}); | |
}) | |
..addStatusListener((status) { | |
if (status == AnimationStatus.completed) { | |
controller2.reverse(); | |
} else if (status == AnimationStatus.dismissed) { | |
controller2.forward(); | |
} | |
}); | |
controller.forward(); | |
controller2.forward(); | |
} | |
@override | |
void dispose() { | |
controller.dispose(); | |
super.dispose(); | |
} | |
@override | |
Widget build(BuildContext context) { | |
return Scaffold( | |
appBar: AppBar( | |
title: Text('Polygons'), | |
), | |
body: SafeArea( | |
child: Column( | |
crossAxisAlignment: CrossAxisAlignment.start, | |
children: <Widget>[ | |
Expanded( | |
child: AnimatedBuilder( | |
animation: animation, | |
builder: (context, snapshot) { | |
return CustomPaint( | |
painter: ShapePainter(_sides, animation2.value, animation.value), | |
child: Container(), | |
); | |
}, | |
), | |
), | |
Padding( | |
padding: const EdgeInsets.only(left: 24.0), | |
child: Text('Sides'), | |
), | |
Slider( | |
value: _sides, | |
min: 3.0, | |
max: 10.0, | |
label: _sides.toInt().toString(), | |
divisions: 7, | |
onChanged: (value) { | |
setState(() { | |
_sides = 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