Last active
December 15, 2020 13:58
-
-
Save singlesoup/484a457a4fe70c6b63db9314e79cd21c 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
import 'dart:math'; | |
import 'package:flutter/material.dart'; | |
void main() { | |
runApp( | |
MaterialApp( | |
home: TempScreen(), | |
), | |
); | |
} | |
class TempScreen extends StatefulWidget { | |
@override | |
_TempScreenState createState() => _TempScreenState(); | |
} | |
class _TempScreenState extends State<TempScreen> with TickerProviderStateMixin { | |
AnimationController _controller; | |
Animation<Offset> _offsetFloat; | |
int counter = 0; | |
double dx = 200; | |
Random rgn = Random(); | |
@override | |
void initState() { | |
super.initState(); | |
_controller = AnimationController( | |
vsync: this, | |
duration: const Duration(seconds: 5), | |
); | |
_offsetFloat = Tween<Offset>( | |
begin: Offset(0.0, 0.0), | |
end: Offset(0.0, 2.0)) // slide vertical transition | |
.animate(CurvedAnimation( | |
parent: _controller, | |
curve: Curves.easeInOutQuint, // matches to the rain like simulation | |
)); | |
_controller.forward(); // starts the animation | |
_controller.repeat(reverse: false); // will repeat forever | |
// below code is to stop it from repeating forever | |
// TickerFuture tickerFuture = _controller.repeat(reverse: false); | |
// tickerFuture.timeout( | |
// Duration( | |
// seconds: 5 * | |
// 3 // here the 2nd varaible decides for how many times it will repeat | |
// ), onTimeout: () { | |
// _controller.forward(from: 0); // stops at the starting position | |
// _controller.stop(canceled: true); | |
// }); | |
} | |
@override | |
void dispose() { | |
_controller.dispose(); | |
super.dispose(); | |
} | |
@override | |
Widget build(BuildContext context) { | |
MediaQueryData mediaQuery = MediaQuery.of(context); | |
return Scaffold( | |
backgroundColor: Colors.grey, | |
// appBar: AppBar( | |
// title: Text('Line'), | |
// ), | |
body: Stack( | |
children: [ | |
SlideTransition( | |
position: _offsetFloat, | |
child: CustomPaint( | |
painter: RainPainter( | |
counter: counter, | |
dx: dx, | |
), | |
child: Container(), | |
), | |
), | |
Align( | |
child: Text(' the counter is: $counter'), | |
), | |
Align( | |
alignment: Alignment.bottomCenter, | |
child: RaisedButton( | |
child: Text('Add counter'), | |
onPressed: () { | |
paintMoreLines(mediaQuery.size.width); | |
// }); | |
}), | |
), | |
], | |
), | |
); | |
} | |
void paintMoreLines(double width) { | |
setState(() { | |
counter++; | |
print('counter fnc: $counter'); | |
for (int i = 0; i < counter; i++) { | |
// if (dx < 0 || dx > width) { | |
dx = rgn.nextDouble() * width; | |
print('value of dx : $dx'); | |
// } | |
} | |
}); | |
} | |
} | |
class RainPainter extends CustomPainter { | |
final int counter; | |
final double dx; | |
RainPainter({@required this.dx, @required this.counter}); | |
@override | |
void paint(Canvas canvas, Size size) { | |
Paint _paint = Paint() | |
..color = Colors.blue | |
..strokeWidth = 8.0; | |
for (int i = 0; i < counter; i++) { | |
canvas.drawLine(Offset(dx, -45), Offset(dx, 25), _paint); | |
// print('i - $i'); | |
} | |
// the 'dy' element of offset, remains the same | |
// the 'dx' is element, which will be changed randomly | |
} | |
@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