Last active
May 1, 2021 21:48
-
-
Save imaNNeo/048983d14f8a1ef540acb0caf260c10f to your computer and use it in GitHub Desktop.
circle_touches
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: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, | |
); | |
} | |
} |
Author
imaNNeo
commented
May 1, 2021
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment