Last active
November 9, 2023 10:23
-
-
Save nehal076/80c7af1acc7b2d570e92c7ad90fd9d42 to your computer and use it in GitHub Desktop.
Flutterista Generate - Nehal
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
/* | |
Flutter Generative Art Pattern | |
This Flutter application generates a mesmerizing generative art pattern. | |
The pattern consists of randomly colored and positioned circular elements | |
arranged in a grid. Tapping on the screen triggers a refresh, creating a new | |
arrangement of elements with different colors and positions. | |
The code uses Flutter's InkWell, AnimatedContainer, and BackdropFilter to | |
create a visually appealing and dynamic generative art experience. | |
License: MIT License | |
*/ | |
import 'dart:math'; | |
import 'dart:ui'; | |
import 'package:flutter/material.dart'; | |
void main() { | |
runApp(const MyApp()); | |
} | |
class MyApp extends StatelessWidget { | |
const MyApp({super.key}); | |
@override | |
Widget build(BuildContext context) { | |
return const MaterialApp( | |
debugShowCheckedModeBanner: false, | |
home: GenerativeArt(), | |
); | |
} | |
} | |
class GenerativeArt extends StatefulWidget { | |
const GenerativeArt({super.key}); | |
@override | |
State<GenerativeArt> createState() => _GenerativeArtState(); | |
} | |
class _GenerativeArtState extends State<GenerativeArt> { | |
Random random = Random(); | |
final List<Color> colors = [ | |
const Color(0xff158ca7), | |
const Color(0xffF5C03E), | |
const Color(0xffD63826), | |
const Color(0xfffcf068), | |
]; | |
Color pickRandomColor() { | |
return colors[random.nextInt(colors.length)]; | |
} | |
Offset getRandomOffset(rows) { | |
double by2 = rows / 2; | |
List offsets = [ | |
Offset(0, -by2), | |
Offset(-by2, 0), | |
Offset(0, by2), | |
Offset(by2, 0), | |
]; | |
return offsets[random.nextInt(offsets.length)]; | |
} | |
Gradient getGradient() { | |
return LinearGradient( | |
colors: [ | |
pickRandomColor(), | |
pickRandomColor(), | |
], | |
begin: Alignment.topCenter, | |
end: Alignment.bottomCenter, | |
); | |
} | |
@override | |
Widget build(BuildContext context) { | |
double width = MediaQuery.of(context).size.width; | |
double rows = width / (width ~/ 107.5); | |
return Scaffold( | |
body: InkWell( | |
onTap: () { | |
setState(() {}); | |
}, | |
child: Wrap( | |
children: List.generate( | |
200, | |
(index) => SizedBox( | |
width: rows, | |
child: AspectRatio( | |
aspectRatio: 1, | |
child: AnimatedContainer( | |
duration: const Duration(milliseconds: 375), | |
clipBehavior: Clip.antiAlias, | |
decoration: BoxDecoration( | |
gradient: getGradient(), | |
), | |
child: BackdropFilter( | |
filter: ImageFilter.blur( | |
sigmaX: 10, | |
sigmaY: 10, | |
tileMode: TileMode.clamp, | |
), | |
child: Transform.translate( | |
offset: getRandomOffset(rows), | |
child: Container( | |
decoration: BoxDecoration( | |
shape: BoxShape.circle, | |
gradient: getGradient(), | |
), | |
), | |
), | |
), | |
), | |
), | |
), | |
), | |
), | |
), | |
); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment