Created
December 25, 2020 13:22
-
-
Save imaNNeo/4bc1e6af2cf5c1bd465e72813da34169 to your computer and use it in GitHub Desktop.
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'; | |
import 'dart:math' as math; | |
class Electron { | |
double speed; | |
double size; | |
Color color; | |
double currentPositionDegree; | |
Electron({ | |
@required this.speed, | |
@required this.size, | |
@required this.color, | |
@required this.currentPositionDegree, | |
}); | |
} | |
class Orbit { | |
final List<Electron> electrons; | |
final angle; | |
Orbit({ | |
@required this.electrons, | |
@required this.angle, | |
}); | |
} | |
void main() { | |
runApp(MyApp()); | |
} | |
class MyApp extends StatelessWidget { | |
// This widget is the root of your application. | |
@override | |
Widget build(BuildContext context) { | |
return MaterialApp( | |
title: 'Flutter4Fun.com', | |
theme: ThemeData( | |
primarySwatch: Colors.yellow, | |
visualDensity: VisualDensity.adaptivePlatformDensity, | |
), | |
home: HomePage(), | |
); | |
} | |
} | |
class HomePage extends StatefulWidget { | |
const HomePage({Key key}) : super(key: key); | |
@override | |
_HomePageState createState() => _HomePageState(); | |
} | |
class _HomePageState extends State<HomePage> { | |
List<Orbit> orbits = [ | |
Orbit(electrons: [], angle: 0), | |
Orbit(electrons: [], angle: 60), | |
Orbit(electrons: [], angle: 120), | |
]; | |
Timer timer; | |
@override | |
void initState() { | |
super.initState(); | |
timer = new Timer.periodic( | |
Duration(milliseconds: 16), | |
(t) { | |
setState(() { | |
orbits.forEach((orbit) { | |
orbit.electrons.forEach((electron) { | |
electron.currentPositionDegree += electron.speed; | |
}); | |
}); | |
}); | |
}, | |
); | |
} | |
Widget build(BuildContext context) { | |
return Scaffold( | |
backgroundColor: Color(0xFF191636), | |
body: Center( | |
child: Stack( | |
children: orbits | |
.map( | |
(orbit) => Transform.rotate( | |
angle: degreesToRads(orbit.angle), | |
child: CustomPaint(painter: _OrbitPainter(orbit.electrons), size: Size(300, 100)), | |
), | |
) | |
.toList(), | |
), | |
), | |
floatingActionButton: FloatingActionButton( | |
onPressed: () { | |
final randomOrbit = orbits[math.Random().nextInt(orbits.length)]; | |
randomOrbit.electrons.add( | |
Electron( | |
speed: randomDoubleBetween(1, 3), | |
size: randomDoubleBetween(4, 8), | |
color: randomColor([ | |
Colors.purpleAccent, | |
Colors.yellowAccent, | |
Colors.cyanAccent, | |
]), | |
currentPositionDegree: 0, | |
), | |
); | |
}, | |
child: Icon(Icons.add), | |
), | |
); | |
} | |
@override | |
void dispose() { | |
timer.cancel(); | |
super.dispose(); | |
} | |
} | |
class _OrbitPainter extends CustomPainter { | |
final List<Electron> electrons; | |
_OrbitPainter(this.electrons); | |
@override | |
void paint(Canvas canvas, Size size) { | |
final center = Offset(size.width / 2, size.height / 2); | |
final rect = Rect.fromCenter(center: center, width: size.width, height: size.height); | |
canvas.drawOval( | |
rect, | |
Paint() | |
..color = Colors.white | |
..style = PaintingStyle.stroke | |
..strokeWidth = 1, | |
); | |
electrons.forEach((electron) { | |
final degree = degreesToRads(electron.currentPositionDegree); | |
final electronPos = center + | |
Offset( | |
math.cos(degree) * (size.width / 2), | |
math.sin(degree) * (size.height / 2), | |
); | |
canvas.drawCircle(electronPos, electron.size, Paint()..color = electron.color); | |
}); | |
} | |
@override | |
bool shouldRepaint(covariant CustomPainter oldDelegate) => true; | |
} | |
num degreesToRads(num deg) { | |
return (deg * math.pi) / 180.0; | |
} | |
double randomDoubleBetween(double min, double max) => Random().nextDouble() * (max - min) + min; | |
Color randomColor(List<Color> colors) { | |
return colors[Random().nextInt(colors.length)]; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment