Skip to content

Instantly share code, notes, and snippets.

@imaNNeo
Last active May 1, 2021 21:48
Show Gist options
  • Save imaNNeo/048983d14f8a1ef540acb0caf260c10f to your computer and use it in GitHub Desktop.
Save imaNNeo/048983d14f8a1ef540acb0caf260c10f to your computer and use it in GitHub Desktop.
circle_touches
import 'dart:async';
import 'dart:math';
import 'package:flutter/material.dart';
void main() {
runApp(MyApp());
}
class MyApp extends StatelessWidget {
// This widget is the root of your application.
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Demo',
theme: ThemeData(
primarySwatch: Colors.blue,
),
home: MyHomePage(),
);
}
}
const color1 = Color(0xfff4f231);
const color2 = Color(0xff04d435);
const color3 = Color(0xff0f5eff);
const color4 = Color(0xff05eeff);
const color5 = Color(0xffee036c);
const neonColors = [
color1,
color2,
color3,
color4,
color5,
];
extension <T> on List<T> {
T random() => this[Random().nextInt(this.length)];
}
class MyHomePage extends StatefulWidget {
MyHomePage() : super();
@override
_MyHomePageState createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
List<Circle> _circles = [];
@override
void initState() {
Timer.periodic(Duration(milliseconds: 16), (timer) {
updateCircles();
});
super.initState();
}
@override
Widget build(BuildContext context) {
return Scaffold(
backgroundColor: Color(0xFF021a45),
body: Listener(
onPointerUp: (pointerUpEvent) {
print('onTapUp');
setState(() {
_circles.add(
Circle(
color: neonColors.random(),
radius: 1,
position: pointerUpEvent.localPosition,
speed: Random().nextDouble() * 2.5 + 0.5,
opacity: 1.0,
),
);
});
},
child: CustomPaint(
painter: MyCustomPainter(_circles),
size: Size(
double.infinity,
double.infinity,
),
),
),
);
}
void updateCircles() {
setState(() {
final deadCircles = <int>[];
_circles = _circles.asMap().entries.map((entry) {
final index = entry.key;
final circle = entry.value;
var newOpacity = circle.opacity - (circle.speed / 200);
if (newOpacity <= 0) {
newOpacity = 0;
deadCircles.add(index);
}
return circle.copyWith(
radius: circle.radius + circle.speed,
opacity: newOpacity,
);
}).toList();
deadCircles.forEach((index) { _circles.removeAt(index); });
});
}
}
class MyCustomPainter extends CustomPainter {
final List<Circle> _showingCircles;
MyCustomPainter(this._showingCircles);
final circlePaint = Paint()
..isAntiAlias = true
..style = PaintingStyle.stroke
..strokeWidth = 6;
@override
void paint(Canvas canvas, Size size) {
_showingCircles.forEach((circle) {
circlePaint..color = circle.color.withOpacity(circle.opacity);
canvas.drawCircle(circle.position, circle.radius, circlePaint);
});
}
@override
bool shouldRepaint(covariant CustomPainter oldDelegate) => true;
}
class Circle {
final Offset position;
final Color color;
final double radius;
final double speed;
final double opacity;
Circle({
required this.position,
required this.color,
required this.radius,
required this.speed,
required this.opacity,
});
Circle copyWith({
Offset? position,
Color? color,
double? radius,
double? speed,
double? opacity,
}) {
return Circle(
position: position ?? this.position,
color: color ?? this.color,
radius: radius ?? this.radius,
speed: speed ?? this.speed,
opacity: opacity ?? this.opacity,
);
}
}
@imaNNeo
Copy link
Author

imaNNeo commented May 1, 2021

ezgif com-gif-maker (5)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment