Last active
February 7, 2023 08:00
-
-
Save RedBrogdon/40308e0a5f47acba46ba62f4d8be2bf4 to your computer and use it in GitHub Desktop.
DartPad Various Discs sample
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'; | |
class DiscData { | |
static final _rng = Random(); | |
double size; | |
Color color; | |
Alignment alignment; | |
DiscData() { | |
color = Color.fromARGB( | |
_rng.nextInt(200), | |
_rng.nextInt(255), | |
_rng.nextInt(255), | |
_rng.nextInt(255), | |
); | |
size = _rng.nextDouble() * 40 + 10; | |
alignment = Alignment( | |
_rng.nextDouble() * 2 - 1, | |
_rng.nextDouble() * 2 - 1, | |
); | |
} | |
} | |
void main() async { | |
runApp( | |
MaterialApp( | |
debugShowCheckedModeBanner: false, | |
home: Scaffold( | |
body: Container( | |
color: Color(0xFF15202D), | |
child: SizedBox.expand( | |
child: VariousDiscs(50), | |
), | |
), | |
), | |
), | |
); | |
} | |
class VariousDiscs extends StatefulWidget { | |
final int numberOfDiscs; | |
VariousDiscs(this.numberOfDiscs); | |
@override | |
_VariousDiscsState createState() => _VariousDiscsState(); | |
} | |
class _VariousDiscsState extends State<VariousDiscs> { | |
final _discs = <DiscData>[]; | |
@override | |
void initState() { | |
super.initState(); | |
_makeDiscs(); | |
} | |
void _makeDiscs() { | |
_discs.clear(); | |
for (int i = 0; i < widget.numberOfDiscs; i++) { | |
_discs.add(DiscData()); | |
} | |
} | |
@override | |
Widget build(BuildContext context) { | |
return GestureDetector( | |
onTap: () => setState(() { | |
_makeDiscs(); | |
}), | |
child: Stack( | |
children: [ | |
Center( | |
child: Text( | |
'Click a disc!', | |
style: TextStyle(color: Colors.white, fontSize: 50), | |
), | |
), | |
for (final disc in _discs) | |
Positioned.fill( | |
child: AnimatedAlign( | |
duration: Duration(milliseconds: 500), | |
curve: Curves.easeInOut, | |
alignment: disc.alignment, | |
child: AnimatedContainer( | |
duration: Duration(milliseconds: 500), | |
decoration: BoxDecoration( | |
color: disc.color, | |
shape: BoxShape.circle, | |
), | |
height: disc.size, | |
width: disc.size, | |
), | |
), | |
), | |
], | |
), | |
); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Thanks for the new dartpad c;
https://dartpad.dartlang.org/
Very nice)