Created
October 25, 2019 13:51
-
-
Save av/6fa529625262cdffd50d1f52ca0bb02b to your computer and use it in GitHub Desktop.
Flutter: magic, circles
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
/// Just a container for parameters | |
/// allowing to draw a circle on a [Canvas] later | |
class CircleParameters { | |
double radius; | |
Offset offset; | |
CircleParameters({@required this.radius, @required this.offset}); | |
} | |
/// Renders certain amount of circles on a [Canvas], | |
/// as per given [CircleParameters] list. | |
class Circles extends Particle with Fading { | |
final List<CircleParameters> circles; | |
/// Generates randomly positioned circles in | |
/// given count with radius between given bounds. | |
factory Circles.random({ | |
int count = 10, | |
double maxRadius = 10, | |
double minRadius, | |
}) { | |
if (minRadius == null) { | |
minRadius = maxRadius * .1; | |
} | |
return Circles( | |
circles: List<CircleParameters>.generate( | |
count, | |
(i) => CircleParameters( | |
radius: Randoms.rnd.nextDouble() * maxRadius + minRadius, | |
offset: Randoms.offsetFromSize( | |
Size(maxRadius, maxRadius), | |
), | |
), | |
), | |
); | |
} | |
Circles({this.circles}); | |
@override | |
void draw(Canvas canvas, Size size) { | |
for (var circle in circles) { | |
canvas.drawCircle( | |
circle.offset, | |
circle.radius, | |
Paint()..color = Colors.white.withOpacity(opacity), | |
); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment