Last active
February 6, 2021 05:07
-
-
Save singlesoup/2d0781e51aae12e09fb00ae306741dbd to your computer and use it in GitHub Desktop.
This file contains 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
/// Credits without which this wouldn't have been possible : Coding With Indy - https://www.youtube.com/watch?v=1VyDHNXFFyg | |
import 'dart:async'; | |
import 'dart:math'; | |
import 'package:flutter/material.dart'; | |
void main() { | |
runApp(MyApp()); | |
} | |
class MyApp extends StatelessWidget { | |
@override | |
Widget build(BuildContext context) { | |
return MaterialApp( | |
title: 'Flutter Demo', | |
theme: ThemeData( | |
primarySwatch: Colors.blue, | |
visualDensity: VisualDensity.adaptivePlatformDensity, | |
), | |
home: MyHomePage(title: 'RainMaker'), | |
); | |
} | |
} | |
class MyHomePage extends StatefulWidget { | |
MyHomePage({Key key, this.title}) : super(key: key); | |
final String title; | |
@override | |
_MyHomePageState createState() => _MyHomePageState(); | |
} | |
class Particle { | |
Offset position; | |
Offset original; | |
double length; | |
double speed; | |
} | |
class _MyHomePageState extends State<MyHomePage> { | |
int _counter = 0; | |
double dx = 200; | |
Random rgn = Random(); | |
Timer timer; | |
var rains = List<Particle>(); | |
@override | |
void dispose() { | |
timer?.cancel(); // disposes the timer made for animation | |
super.dispose(); | |
} | |
@override | |
Widget build(BuildContext context) { | |
MediaQueryData mediaQuery = MediaQuery.of(context); | |
return Scaffold( | |
backgroundColor: Colors.grey[800], | |
appBar: AppBar( | |
title: Text( | |
widget.title, | |
style: TextStyle(color: Colors.grey[50]), | |
), | |
), | |
body: Stack( | |
children: [ | |
CustomPaint( | |
painter: RainPainter( | |
rains: rains, | |
), | |
child: Container(), | |
), | |
Center( | |
child: Column( | |
mainAxisAlignment: MainAxisAlignment.center, | |
children: <Widget>[ | |
Text( | |
'You have pushed the button this many times:', | |
style: TextStyle(color: Colors.grey[50]), | |
), | |
Text( | |
'$_counter', | |
style: Theme.of(context).textTheme.headline4.copyWith( | |
color: Colors.grey[50], | |
), | |
), | |
], | |
), | |
), | |
], | |
), | |
floatingActionButton: FloatingActionButton( | |
onPressed: () { | |
paintMoreLines(mediaQuery.size.width); | |
}, | |
tooltip: 'Increment', | |
child: Icon(Icons.add), | |
), | |
); | |
} | |
void paintMoreLines(double width) { | |
setState(() { | |
_counter++; | |
print('counter fnc: $_counter'); | |
// animation timer | |
final fps = 50.0; | |
var frameDuration = (4000 ~/ fps); | |
timer = Timer.periodic(Duration(milliseconds: frameDuration), (timer) { | |
setState(() { | |
rains.forEach((p) { | |
p.position += Offset(0, p.speed + 20); | |
// print('dy: ${p.position.dy}'); | |
if (p.position.dy > 600) { | |
p.position = p.original; | |
} | |
}); | |
}); | |
}); | |
var xx = 0.0; | |
// create particles | |
for (var i = 0; i < _counter; i++) { | |
xx += rgn.nextDouble() * 50.0 + 12.0; | |
var dx = xx; | |
var dy = -rgn.nextDouble() * 80.0; | |
// generate the length and speed | |
var len = rgn.nextDouble() * 30.0 + 30.0; | |
var speed = rgn.nextDouble() * 3.0 + 1.0; | |
rains.add(Particle() | |
..position = Offset(dx, dy) | |
..original = Offset(dx, dy) | |
..speed = speed | |
..length = len); | |
} | |
}); | |
} | |
} | |
class RainPainter extends CustomPainter { | |
List<Particle> rains; | |
RainPainter({@required this.rains}); | |
@override | |
void paint(Canvas canvas, Size size) { | |
var center = Offset(10, size.height / 8); | |
drawRain(canvas, center); | |
} | |
var W = 600.0; | |
void drawRain(Canvas canvas, Offset center) { // custom painter method | |
var rain = Paint() | |
..color = Color(0xffdee2ff) //Colors.black | |
..strokeWidth = 10.0 | |
..style = PaintingStyle.stroke; | |
rains.forEach((p) { | |
var p2 = p.position + Offset(0, p.length); | |
canvas.drawLine(p.position + center, p2 + center, rain); | |
}); | |
} | |
@override | |
bool shouldRepaint(covariant CustomPainter oldDelegate) { | |
return true; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
RainMaker with Random color