Skip to content

Instantly share code, notes, and snippets.

@TramPamPam
Created May 15, 2023 15:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save TramPamPam/94acb6a60872014628d01ebb60155fe1 to your computer and use it in GitHub Desktop.
Save TramPamPam/94acb6a60872014628d01ebb60155fe1 to your computer and use it in GitHub Desktop.
Animated discs example

Animated discs example

Created with <3 with dartpad.dev.

import 'dart:math';
import 'package:flutter/material.dart';
class DiscData {
static final _rng = Random();
final double size;
final Color color;
final Alignment alignment;
DiscData()
: size = _rng.nextDouble() * 40 + 10,
color = Color.fromARGB(
_rng.nextInt(200),
_rng.nextInt(255),
_rng.nextInt(255),
_rng.nextInt(255),
),
alignment = Alignment(
_rng.nextDouble() * 2 - 1,
_rng.nextDouble() * 2 - 1,
);
}
void main() async {
runApp(
MaterialApp(
debugShowCheckedModeBanner: false,
home: Scaffold(
body: Container(
color: const Color(0xFF15202D),
child: const SizedBox.expand(
child: VariousDiscs(50),
),
),
),
),
);
}
class VariousDiscs extends StatefulWidget {
final int numberOfDiscs;
const VariousDiscs(this.numberOfDiscs);
@override
State<VariousDiscs> 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 Stack(
children: [
const Center(
child: Text(
'Click a disc!',
style: TextStyle(color: Colors.white, fontSize: 50),
),
),
GestureDetector(
onTap: () => setState(() {
_makeDiscs();
}),
child: Stack(children: [
for (final disc in _discs)
Positioned.fill(
child: AnimatedAlign(
duration: const Duration(milliseconds: 500),
curve: Curves.easeInOut,
alignment: disc.alignment,
child: AnimatedContainer(
duration: const 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