Last active
October 23, 2020 04:03
-
-
Save RedBrogdon/4d7650278c256e6bc5c9ba0bfb4b5f35 to your computer and use it in GitHub Desktop.
Implicit animations (null safe)
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 'package:flutter/material.dart'; | |
import 'dart:math'; | |
class DiscData { | |
static final _rng = Random(); | |
late final double size; | |
late final Color color; | |
late final 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