Created
January 2, 2022 14:54
-
-
Save BarryDaBee/ac37a681ea6a58c72f8afbf5e434bb89 to your computer and use it in GitHub Desktop.
Automated animated discs project inspired by Dartpad sample project
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 'package:flutter/material.dart'; | |
| import 'dart:math'; | |
| import 'dart:async'; | |
| const Color darkBlue = Color.fromARGB(255, 18, 32, 47); | |
| void main() { | |
| runApp(MyApp()); | |
| } | |
| class MyApp extends StatelessWidget { | |
| @override | |
| Widget build(BuildContext context) { | |
| return const MaterialApp( | |
| home: DiscScaffold(noOfDiscs: 40), | |
| ); | |
| } | |
| } | |
| class Disc { | |
| static final _random = Random(); | |
| final double size; | |
| final Alignment alignment; | |
| final Color color; | |
| Disc() | |
| : size = _random.nextDouble() * 40 + 10, | |
| color = Color.fromRGBO( | |
| _random.nextInt(255), | |
| _random.nextInt(255), | |
| _random.nextInt(255), | |
| _random.nextDouble(), | |
| ), | |
| alignment = Alignment( | |
| _random.nextDouble() * 2 - 1, | |
| _random.nextDouble() * 2 - 1, | |
| ); | |
| } | |
| class DiscScaffold extends StatefulWidget { | |
| final int noOfDiscs; | |
| const DiscScaffold({Key? key, this.noOfDiscs = 20}) : super(key: key); | |
| @override | |
| State<DiscScaffold> createState() => _DiscScaffoldState(); | |
| } | |
| class _DiscScaffoldState extends State<DiscScaffold> { | |
| List<Disc> discs = []; | |
| late final Timer _timer; | |
| final Duration _duration = const Duration(milliseconds: 600); | |
| void _createDiscs() { | |
| discs.clear(); | |
| for (var i = 0; i < widget.noOfDiscs; i++) { | |
| discs.add(Disc()); | |
| } | |
| } | |
| @override | |
| void initState() { | |
| _createDiscs(); | |
| _timer = Timer.periodic( | |
| Duration(milliseconds: _duration.inMilliseconds + 100), (timer) { | |
| setState(_createDiscs); | |
| }); | |
| super.initState(); | |
| } | |
| @override | |
| void dispose() { | |
| _timer.cancel(); | |
| super.dispose(); | |
| } | |
| @override | |
| Widget build(BuildContext context) { | |
| return Scaffold( | |
| backgroundColor: darkBlue, | |
| body: Stack( | |
| children: [ | |
| for (var disc in discs) | |
| Positioned.fill( | |
| child: AnimatedAlign( | |
| duration: _duration, | |
| alignment: disc.alignment, | |
| child: AnimatedContainer( | |
| decoration: BoxDecoration( | |
| shape: BoxShape.circle, | |
| color: disc.color, | |
| ), | |
| duration: _duration, | |
| height: disc.size, | |
| width: disc.size, | |
| ), | |
| ), | |
| ), | |
| ], | |
| ), | |
| ); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment